Home > OS >  Flask Circular Error: Error while finding module specification for 'flask.__main__'
Flask Circular Error: Error while finding module specification for 'flask.__main__'

Time:08-27

Like others, I am working on a Flask application using Michael Grinberg's great book. Being more on the newbie side of things, I may have picked a path fraught with danger. For those who might be familiar with Flask Web Development (https://github.com/miguelgrinberg/flasky). I am trying to do two things:

  • Use a later version of packages (See generated requirements.txt list below).
  • Get tagged version 8h of his sample code running under later versions of packages and Python 3.9

I believe my issue "might be" related to trying to use later package versions and Python 3.9. When I try to run this version of the app, I get the following error:

C:\Users\jgriffin010\PycharmProjects\Utility-Edge\UE-BE\venv\Scripts\python.exe: 
Error while finding module specification for 'flask.__main__' 
(ImportError: cannot import name 'current_app' from partially initialized module 'flask' (most likely due to a circular import) 
(C:\Users\jgriffin010\PycharmProjects\Utility-Edge\UE-BE\venv\lib\site-packages\flask\__init__.py))

I am noticing that these errors are coming out of my ##venv directory## instead of the project code itself. Could this error be caused because I have tried to use later versioned Python packages and Python 3.9?

Any thoughts on the specific error or trying to use later packages is appreciated.

Installed Packages

alembic==1.8.1
auth==0.5.3
blinker==1.5
certifi==2022.6.15
cffi==1.15.1
charset-normalizer==2.1.1
click==8.1.3
colorama==0.4.5
cryptography==37.0.4
dnspython==2.2.1
dominate==2.7.0
eventlet==0.33.1
falcon==3.1.0
Flask==2.2.2
Flask-Bootstrap==3.3.7.1
Flask-Login==0.6.2
Flask-Mail==0.9.1
Flask-Migrate==3.1.0
Flask-Moment==1.0.4
Flask-SQLAlchemy==2.5.1
Flask-WTF==1.0.1
greenlet==1.1.2
gunicorn==20.1.0
idna==3.3
importlib-metadata==4.12.0
itsdangerous==2.1.2
Jinja2==3.1.2
jwt==1.3.1
Mako==1.2.1
MarkupSafe==2.1.1
mongoengine==0.24.2
packaging==21.3
pycparser==2.21
PyJWT==2.4.0
pymongo==4.2.0
pyparsing==3.0.9
requests==2.28.1
six==1.16.0
SQLAlchemy==1.4.40
urllib3==1.26.11
visitor==0.1.3
Werkzeug==2.2.2
WTForms==3.0.1
zipp==3.8.1

Directory Structure

|   .gitignore
|   config.py
|   flasky.py
|   LICENSE
|   README.md
|   requirements.txt
|   tree.txt
|           
 ---app
|   |   email.py
|   |   models.py
|   |   __init__.py
|   |   
|    ---auth
|   |       forms.py
|   |       views.py
|   |       __init__.py
|   |       
|    ---main
|   |   |   errors.py
|   |   |   forms.py
|   |   |   views.py
|   |   |   __init__.py
|   |   |   
|    ---static
|   |       favicon.ico
|   |       
|    ---templates
|   |   |   404.html
|   |   |   500.html
|   |   |   base.html
|   |   |   index.html
|   |   |   
|   |    ---auth
|   |   |   |   change_email.html
|   |   |   |   change_password.html
|   |   |   |   login.html
|   |   |   |   register.html
|   |   |   |   reset_password.html
|   |   |   |   unconfirmed.html
|   |   |   |   
|   |   |   \---email
|   |   |           change_email.html
|   |   |           change_email.txt
|   |   |           confirm.html
|   |   |           confirm.txt
|   |   |           reset_password.html
|   |   |           reset_password.txt
|   |   |           
|   |   \---mail
|   |           new_user.html
|   |           new_user.txt

templates/init.py

from flask import Flask
from flask_bootstrap import Bootstrap
from flask_mail import Mail
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from config import config

bootstrap = Bootstrap()
mail = Mail()
moment = Moment()
db = SQLAlchemy()

login_manager = LoginManager()
login_manager.login_view = 'auth.login'


def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    bootstrap.init_app(app)
    mail.init_app(app)
    moment.init_app(app)
    db.init_app(app)
    login_manager.init_app(app)

    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint, url_prefix='/auth')

    return app

flasky.py

import os
import click
from flask_migrate import Migrate
from app import create_app, db
from app.models import User, Role

app = create_app(os.getenv('FLASK_CONFIG') or 'default')
migrate = Migrate(app, db)

@app.shell_context_processor
def make_shell_context():
    return dict(db=db, User=User, Role=Role)


@app.cli.command()
@click.argument('test_names', nargs=-1)
def test(test_names):
    """Run the unit tests."""
    import unittest
    if test_names:
        tests = unittest.TestLoader().loadTestsFromNames(test_names)
    else:
        tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)

CodePudding user response:

I think it might be due to your module named 'main'. Have you tried to change it?

CodePudding user response:

It turned out that I had an inconsistency in the project setup within PyCharm. I basically cleared all the caches, and built up a new venv environment and the issue disappeared. My lesson learned to consider cache clearing when troubleshooting and in my situation re-setup the venv side of things.

  • Related