Issues

Issue #87 resolved

ImportError with --autogenerate

Josef Assad
created an issue

I've got a very simple sqlalchemy project all living in 1 file (for now), including my Base object. I am interested in autogenerating migrations for this project.

As the tutorial specifies, I have imported the project file in env.py and set target_metadata to point at Base.metadata in the imported module. It is giving me an ImportError on the module name, however. To clarify, all my code is in foo.py. env.py has two lines looking like:

from foo import Base
target_metadata = Base.metadata

Then when I run:

alembic revision --autogenerate -m "add initial revision"

I get:

ImportError: No module named foo

What am I doing wrong, if anything?

Comments (3)

  1. Mike Bayer repo owner

    OK this is not really any kind of "bug" in alembic, if you have more issues along this line it's probably best if you post on the mailing list at https://groups.google.com/group/sqlalchemy-alembic .

    the short answer is "alembic" is a console script, not unlike if you just ran "python" by itself, which then imports the alembic library, which then runs your env.py. So within env.py, if you want to import some other library, such as your "foo", your .py file has to be importable as a module. The quickest path to this is to just set the PYTHONPATH environment variable to point to the directory where your foo.py is present, or in your env.py, to manipulate sys.path to have this location. The more comprehensive way to go is to use virtualenv, where you'd install your foo.py as an application with "python setup.py develop", which implies you'd have a setup.py and such.

    an overview of how to have a .py file importable as a module is at http://docs.python.org/2/tutorial/modules.html .

  2. Josef Assad reporter

    OH I see, that makes a lot of sense. What I have is structurally a module, but it wasn't anywhere env.py knew to look in. Thanks for your patient and prompt response!

  3. Log in to comment