Flask - ImportError: No module named app

前端 未结 7 1535
故里飘歌
故里飘歌 2020-12-15 16:31

First I created __init__.py

from flask import Flask

app = Flask(__name__)

Then in a separate file, in the same directory,

相关标签:
7条回答
  • 2020-12-15 16:31

    For me, export PYTHONPATH=/path/to/your/src && python app/main.py works

    0 讨论(0)
  • 2020-12-15 16:33

    Ensure to set your PYTHONPATH to the src/ directory as well. Example export PYTHONPATH="$PYTHONPATH:/path/to/your/src"

    0 讨论(0)
  • 2020-12-15 16:34

    __init__.py is imported using a directory. if you want to import it as app you should put __init__.py file in directory named app

    a better option is just to rename __init__.py to app.py

    0 讨论(0)
  • 2020-12-15 16:34

    Just rename your file to app.py and it will works.

    0 讨论(0)
  • 2020-12-15 16:41

    This is probably an error in flask application's folder structure.
    Anyone looking for a simple beginner-friendly structure for the flask project may find this helpful:

       |__movies 
         |__run.py 
         |__app     
            ├── templates
            │   └── index.html
            │   └── signup.html
            └── __init__.py
            └── routes.py
    

    Here 'movies' is the name given for the main application. It contains 'run.py' and a folder called 'app'. 'app' folder contains all necessary flask files such as 'templates' folder, '__init __.py', and 'routes.py'.

    Contents of:

    run.py:

    from app import app
    

    __init__.py:

    from flask import Flask
    
    app = Flask(__name__)
    
    from app import routes
    
    
    app.run(debug=True)
    

    routes.py:

    from app import app
    
    @app.route('/')
    @app.route('/index')
    def index():
        return "Hello, World!"
    
    0 讨论(0)
  • 2020-12-15 16:42

    Your __init__.py file needs to go in the folder named app, not the same directory as the run.py file.

    from app import app is looking in the app folder, so the __init__.py file needs to sit in there.

    0 讨论(0)
提交回复
热议问题