Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: cannot import name 'app' #3

Open
ybgirgin3 opened this issue Jul 24, 2020 · 4 comments
Open

ImportError: cannot import name 'app' #3

ybgirgin3 opened this issue Jul 24, 2020 · 4 comments

Comments

@ybgirgin3
Copy link

ybgirgin3 commented Jul 24, 2020

mail_services.py cannot import app. I tried some much thing for this relative importing but nothing happend still getting this error

@IvanLjubicic
Copy link

I believe it is a circular dependency issue.

like in this one

https://stackoverflow.com/questions/17845366/importerror-cannot-import-name/17845428

I am dealing with it now.

@wadi-it-bc
Copy link

wadi-it-bc commented Nov 24, 2020

The probable workaround, I'm not very good at describing the solution, but I'll try:
Implement most of the app structure as done in https://github.com/miguelgrinberg/flasky

So we're trying to avoid the circular reference by:

  • creating a new set up for the flask app
    • app.py won't be used any more
    • we'll implement create_app() as a function to define the app
  • creating a new start up file to actually run the app by calling the create_app function

Steps:

  • create and use __init__.py in your movie-bag folder to set up the flask app
    • move the code in app.py to __init__.py (double underscores before and after)
    • also use the create_app set up as shown in the flasky project, but without the additional parameters, just use
      def create_app():
      app = Flask(__name__)
  • remove (or rename) app.py: you won't be using it anymore
  • edit mail_services.py: in stead of from app import app use from movie-bag import current_app
  • you probably have to set FLASK_APP environment variable to 'movie-bag.py' for flask to get hold of the current_app as defined in __init__.py
    • linux: export $FLASK_APP='movie-bag.py'
    • windows (I think): SET FLASK_APP='movie-bag.py'
  • run the app: python flasky.py (or in this case something like python movie-bag.py or python run.py)
    • this will include the actual call to the create_app in the previously created __init__.py
    • this file will be placed outside the movie-bag folder (1 level higher)

Don't get distracted by other code in this flasky project (although it's a great project to learn from), focus on getting the same structure.
This worked for me.
Good luck.

@chuck-tz
Copy link

chuck-tz commented Dec 9, 2020

@wadi-it-bc Could you please explain a bit what the two-sided double asterisks current_app do?

@wadi-it-bc
Copy link

@chuck-tz Argh, I made a typo, sorry!
I removed the asterisks 😳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants