TypeError: 'module' object is not callable when importing module through __init__.py
See original GitHub issueFirst 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:
- Created 2 years ago
- Comments:5
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@hugo-dystech Great, just what I had suspected 😉
Have you already noticed that your
utils
package indeed contains ametrics
module that is shadowing yourutils.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,and there you go.
Awesome, happy to help!