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

models don't import from default PYTHONPATH of "." #797

Closed
zzzeek opened this issue Feb 20, 2021 · 3 comments
Closed

models don't import from default PYTHONPATH of "." #797

zzzeek opened this issue Feb 20, 2021 · 3 comments
Labels
bug Something isn't working migration environment

Comments

@zzzeek
Copy link
Member

zzzeek commented Feb 20, 2021

given a structure:

./alembic/env.py
./myapp/models.py
./myapp/__init__.py

with env.py:

from myapp.models import Base

target_metadata = Base.metadata

and a local virtual environment. If I run from ., I can import the app:

$ .venv/bin/python
Python 3.8.3 (default, May 23 2020, 16:34:37) 
[GCC 9.3.1 20200408 (Red Hat 9.3.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from myapp import models
>>> 

however running alembic seems to not honor this:

$ .venv/bin/alembic  revision -m "rev1" --autogenerate
Traceback (most recent call last):
  File ".venv/bin/alembic", line 8, in <module>
    sys.exit(main())
  File "/home/classic/dev/test/.venv/lib/python3.8/site-packages/alembic/config.py", line 559, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/home/classic/dev/test/.venv/lib/python3.8/site-packages/alembic/config.py", line 553, in main
    self.run_cmd(cfg, options)
  File "/home/classic/dev/test/.venv/lib/python3.8/site-packages/alembic/config.py", line 530, in run_cmd
    fn(
  File "/home/classic/dev/test/.venv/lib/python3.8/site-packages/alembic/command.py", line 212, in revision
    script_directory.run_env()
  File "/home/classic/dev/test/.venv/lib/python3.8/site-packages/alembic/script/base.py", line 481, in run_env
    util.load_python_file(self.dir, "env.py")
  File "/home/classic/dev/test/.venv/lib/python3.8/site-packages/alembic/util/pyfiles.py", line 97, in load_python_file
    module = load_module_py(module_id, path)
  File "/home/classic/dev/test/.venv/lib/python3.8/site-packages/alembic/util/compat.py", line 182, in load_module_py
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "alembic/env.py", line 20, in <module>
    from myapp.models import Base
ModuleNotFoundError: No module named 'myapp'

that seems pretty unnecessary so let's fix that.

@zzzeek zzzeek added requires triage New issue that requires categorization bug Something isn't working migration environment and removed requires triage New issue that requires categorization labels Feb 20, 2021
@zzzeek
Copy link
Member Author

zzzeek commented Feb 20, 2021

so, I'm going to append the path at the end, and hopefully this doesnt cause any issues. I have a feeling there may have been some concerns with this in the past, but I can't remember what they are. So let's put it out and then if there's a problem, then we'll know, and we can change this behavior to be a config file thing. But I think this should work the way it is because we normally load env.py relative to the current path with the default alembic.ini config that has a relative path.

@sqla-tester
Copy link
Collaborator

Mike Bayer has proposed a fix for this issue in the master branch:

Implement sys_path_prepend option https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/2601

@pmlk
Copy link

pmlk commented Feb 20, 2021

Just speculating about possible past concerns of adding "." to sys.path:

There may be a conflict between the installed alembic package and the ./alembic/ folder ("python package") when alembic was initialized with alembic init alembic as described in the docs. It may at least cause some confusion.
I know there is no ./alembic/__init__.py file so technically ./alembic/ wouldn't be a python package. However, maybe the __init__.py file needs to be added in order to fix this issue?
Maybe it would be more advisable to initialize alembic with another folder name, say alembic init migrations? Should that be reflected in the docs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working migration environment
Projects
None yet
Development

No branches or pull requests

3 participants