Skip to content

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

@hugo-dystech

Description

@hugo-dystech

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions