question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: 'module' object is not callable when importing module through __init__.py

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

import utilsimport os, json
from typing import Optional
from fastapi import FastAPI
import uvicorn
import random
from fastapi import FastAPI, Request, UploadFile, File
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
# Import module, use __init__.py to add more modules
import utils


app = FastAPI()
app.mount("/static/", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")

# this function seem to say that the function "metrics" from utils is not callable????
@app.get("/api/v1/readability/results/", response_class=HTMLResponse, tags=["Text generation & analysis"],     description="Send a given text in string and analyse multiple metrics of it. Things like readability formulas, number of syllables and more.",)
def analyse_text(request: Request, story: str):
    story = utils.add_dot(story)
    count_syllables = utils.syllable_count(story)
    sentence_1 = story.count('.')
    sentence_2 = story.count('!')
    sentence_3 = story.count('?')
    count_sentence = sentence_1 + sentence_2 + sentence_3
    count_word = len(story.split())
    word_list = story.split()
    syllable_per_word = [utils.syllable_count(i) for i in word_list]
    count_complex_words = sum(1 for x in syllable_per_word if x >= 3)
    characters = len(story)
    flesch_reading_ease_formula, flesch_grade_level_formula, gunning_fog_index, smog_index, coleman_liau_index, automated_readability_index = utils.metrics( 
        characters, count_syllables, count_word, count_complex_words, count_sentence)

    return templates.TemplateResponse("readability-results.html", 
    {"request": request, 
    "count_syllable": count_syllables,
    "count_word": count_word,
    "average_syllable_per_word": round(count_syllables / count_word, 2),
    "count_sentence": count_sentence,
    "count_complex_words": count_complex_words,
    "percentage_complex_words": round(count_complex_words / count_word * 100, 2),
    "gunning_fog_index": gunning_fog_index,
    "smog_index": smog_index,
    "coleman_liau_index": coleman_liau_index,
    "automated_readability_index": automated_readability_index,
    "flesch_grade_level_formula": flesch_grade_level_formula,
    "flesch_reading_ease_formula": flesch_reading_ease_formula
    })

Description

Hey everyone, now this one is quite interesting: I’ve looked through all the GitHub issues and stack overflow and I understand the meaning of the issue however, I do not know why I am getting the error:

I import modules from a folder called utils using init.py

# this function seem to say that the function "metrics" from utils is not callable????
@app.get("/api/v1/readability/results/", response_class=HTMLResponse, tags=["Text generation & analysis"],     description="Send a given text in string and analyse multiple metrics of it. Things like readability formulas, number of syllables and more.",)
def analyse_text(request: Request, story: str):
    story = utils.add_dot(story)
    count_syllables = utils.syllable_count(story)
    sentence_1 = story.count('.')
    sentence_2 = story.count('!')
    sentence_3 = story.count('?')
    count_sentence = sentence_1 + sentence_2 + sentence_3
    count_word = len(story.split())
    word_list = story.split()
    syllable_per_word = [utils.syllable_count(i) for i in word_list]
    count_complex_words = sum(1 for x in syllable_per_word if x >= 3)
    characters = len(story)
    flesch_reading_ease_formula, flesch_grade_level_formula, gunning_fog_index, smog_index, coleman_liau_index, automated_readability_index = utils.metrics( 
        characters, count_syllables, count_word, count_complex_words, count_sentence)

    return templates.TemplateResponse("readability-results.html", 
    {"request": request, 
    "count_syllable": count_syllables,
    "count_word": count_word,
    "average_syllable_per_word": round(count_syllables / count_word, 2),
    "count_sentence": count_sentence,
    "count_complex_words": count_complex_words,
    "percentage_complex_words": round(count_complex_words / count_word * 100, 2),
    "gunning_fog_index": gunning_fog_index,
    "smog_index": smog_index,
    "coleman_liau_index": coleman_liau_index,
    "automated_readability_index": automated_readability_index,
    "flesch_grade_level_formula": flesch_grade_level_formula,
    "flesch_reading_ease_formula": flesch_reading_ease_formula
    }) 

While runing the API call I am getting this error below:

File "/Users/hugo/Documents/GitHub/dystech-api/./main.py", line 69, in analyse_text
    flesch_reading_ease_formula, flesch_grade_level_formula, gunning_fog_index, smog_index, coleman_liau_index, automated_readability_index = utils.metrics(
TypeError: 'module' object is not callable

This is my metrics function:

def metrics(characters, syllables, wordcount, complex_words_count, sentences_count):
    flesch_reading_ease_formula = round(206.835 - 1.015 *
                                        (wordcount/sentences_count) - 84.6 * (syllables/wordcount), 2)
    flesch_grade_level_formula = round(
        0.39 * (wordcount/sentences_count) + 11.8 * (syllables/wordcount) - 15.59, 0)
    gunning_fog_index = round(
        0.4 * ((wordcount/sentences_count) + 100 * (complex_words_count/wordcount)), 2)
    smog_index = round(
        1.0430 * math.sqrt(30 * complex_words_count / sentences_count) + 3.1291, 2)
    coleman_liau_index = round(
        5.89 * (characters / wordcount) - 0.3 * (sentences_count / wordcount) - 15.8, 2)
    automated_readability_index = round(
        4.71 * (characters / wordcount) + 0.5 * (wordcount / sentences_count) - 21.43, 2)
    return flesch_reading_ease_formula, flesch_grade_level_formula, gunning_fog_index, smog_index, coleman_liau_index, automated_readability_index

I am not sure where to start since all the other functions seem to work but this one. And it’s clearly not a class so any help would be apreciated 😃

Operating System

macOS

Operating System Details

No response

FastAPI Version

0.70.0

Python Version

Python 3.9.7

Additional Context

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
oligondcommented, Nov 16, 2021

@hugo-dystech Great, just what I had suspected 😉

Have you already noticed that your utils package indeed contains a metrics module that is shadowing your utils.metrics.metrics function due to the star import?

The simplest solution should be to rename the “metrics” function, let’s call it calculate_metrics for the sake of example.

Update your call to that function in main.py accordingly,

    flesch_reading_ease_formula, flesch_grade_level_formula, gunning_fog_index, smog_index, coleman_liau_index, automated_readability_index = utils.calculate_metrics( 
        characters, count_syllables, count_word, count_complex_words, count_sentence)

and there you go.

0reactions
oligondcommented, Nov 16, 2021

Awesome, happy to help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: 'module' object is not callable - Stack Overflow
It says module object is not callable , because your code is calling a module object. A module object is the type of...
Read more >
What is "typeerror: 'module' object is not callable"
This error statement TypeError: 'module' object is not callable is raised as you are being confused about the Class name and Module name....
Read more >
TypeError: 'module' object is not callable in Python | bobbyhadz
The Python "TypeError: 'module' object is not callable" occurs when we import a module as import some_module but try to call it as...
Read more >
TypeError module object is not callable | Edureka Community
Another solution to the TypeError: 'module' object is not callable error is directly calling the function after importing required library.
Read more >
TypeError: module object is not callable [Python Error Solved]
To put it simply, the "TypeError: 'module' object is not callable" error means that modules cannot be called like functions or methods. How...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found