Importing app when using Alembic raises ImportError

2024/10/15 19:23:07

I am trying to study how to use alembic in flask, I want to import a method in flask app:

tree .
.
├── README.md
├── alembic
│   ├── README
│   ├── env.py
│   ├── env.pyc
│   ├── script.py.mako
│   └── versions
│       ├── 8f167daabe6_create_account_table.py
│       └── 8f167daabe6_create_account_table.pyc
├── alembic.ini
├── app
│   ├── __init__.py
│   ├── main
│   │   ├── __init__.py
│   │   ├── errors.py
│   │   ├── forms.py
│   │   └── views.py
│   ├── models.py
│   └── templates
│       ├── 404.html
│       ├── 500.html
│       ├── base.html
│       ├── index.html
│       └── user.html
├── config.py
├── data.sqlite
├── manage.py
└── requirements.txt

in app/__init__.py:

def create_app(config_name):app = Flask(__name__)

I want to import create_app in env.py:

from app import create_app

but the error shows as below when I run the command alembic upgrade head:

  File "alembic/env.py", line 5, in <module>from app import create_app
ImportError: No module named app

Any idea for this?

Answer

I guess you are trying to run

python env.py

In this case, your app directory is not in PYTHONPATH.

solution 1

Run the app from parent dir:

python alembic/env.py

solution 2

Set the PYTHONPATH before running the app

PYTHONPATH=/path/to/parent/dir python env.py

edit

I read about alembic. As @mrorno said, just set the PYTHONPATH before running alembic:

PYTHONPATH=. alembic upgrade head
https://en.xdnf.cn/q/69245.html

Related Q&A

Git add through python subprocess

I am trying to run git commands through python subprocess. I do this by calling the git.exe in the cmd directory of github.I managed to get most commands working (init, remote, status) but i get an err…

How to unread a line in python

I am new to Python (2.6), and have a situation where I need to un-read a line I just read from a file. Heres basically what I am doing.for line in file:print linefile.seek(-len(line),1)zz = file.readli…

typeerror bytes object is not callable

My code:import psycopg2 import requests from urllib.request import urlopen import urllib.parse uname = " **** " pwd = " ***** " resp = requests.get("https://api.flipkart.net/se…

How to inverse lemmatization process given a lemma and a token?

Generally, in natural language processing, we want to get the lemma of a token. For example, we can map eaten to eat using wordnet lemmatization.Is there any tools in python that can inverse lemma to a…

NameError: name N_TOKENS is not defined

I am new on Python and just got around to install PyCharm for Windows. Downloaded some sample code from Skype for testing their SkypeKit API. But... As soon as I hit the debug button, I get this: (I ha…

Moving Spark DataFrame from Python to Scala whithn Zeppelin

I created a spark DataFrame in a Python paragraph in Zeppelin.sqlCtx = SQLContext(sc) spDf = sqlCtx.createDataFrame(df)and df is a pandas dataframeprint(type(df)) <class pandas.core.frame.DataFrame&…

How do I efficiently do a bulk insert-or-update with SQLAlchemy?

Im using SQLAlchemy with a Postgres backend to do a bulk insert-or-update. To try to improve performance, Im attempting to commit only once every thousand rows or so:trans = engine.begin()for i, rec in…

How to pass variables from javascript to python in Jupyter?

As I understand it, I should be able to print the variable foo in the snippet below. from IPython.display import HTML HTML(<script type="text/javascript">IPython.notebook.kernel.execute…

SVR Model --Feature Scaling - Expected 2D array, got 1D array instead

I am trying to understand what is wrong with the code below. I know that the Y variable is 1D array and expected to be 2D array and need to reshape the structure but that code was working previously fi…

How to find the version of jupyter notebook from within the notebook

I wish to return the version of Jupyter Notebook from within a cell of a notebook. For example, to get the python version, I run: from platform import python_version python_version()or to get the panda…