Dinesh Ahuja
Dinesh Ahuja

Reputation: 1015

Import module in Flask

I am using python flask. I want to import a module from a different directory.

I have file called hello.py

PATH = project/app/hello.py

app = Flask(__name__)

I have another file called tables.py

PATH = project/db/migration/tables.py

app.config("Database")

So, I need to import app from hello.py

from app.hello import app
app.config("Database")

I am executing the script tables.py like,

cd project/db/migration
python3 tables.py

It is saying no module found app.hello

Upvotes: 1

Views: 2719

Answers (1)

Grey Li
Grey Li

Reputation: 12762

You need to create a __init__.py in the app folder to make it become a Python package, then you can import a module inside it with from app.hello import app. The file structure may like this:

app / 
    | __init__.py
    | hello.py 

P.S. The __init__.py's content can be empty.

Upvotes: 1

Related Questions