ImportError with --autogenerate

Issue #87 resolved
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 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 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. Michael 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 .

    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 So within, 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 is present, or in your, to manipulate sys.path to have this location. The more comprehensive way to go is to use virtualenv, where you'd install your as an application with "python develop", which implies you'd have a and such.

    an overview of how to have a .py file importable as a module is at .

  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 knew to look in. Thanks for your patient and prompt response!

  3. Log in to comment