0

I'm running into this behavior when I import a module in the config script I'm using to drive alembic. The script is dynamically loaded from the filesystem by a module installed in my virtualenv, which seems like the only situation where this issue happens.

Here is the stack trace as it happens:

Traceback (most recent call last):
  File "/mnt/home/dmonego/test/bin/alembic", line 8, in <module>
    load_entry_point('alembic==0.6.5', 'console_scripts', 'alembic')()
  File "/mnt/home/dmonego/test/local/lib/python2.7/site-packages/alembic/config.py", line 298, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/mnt/home/dmonego/test/local/lib/python2.7/site-packages/alembic/config.py", line 293, in main
    self.run_cmd(cfg, options)
  File "/mnt/home/dmonego/test/local/lib/python2.7/site-packages/alembic/config.py", line 279, in run_cmd
    **dict((k, getattr(options, k)) for k in kwarg)
  File "/mnt/home/dmonego/test/local/lib/python2.7/site-packages/alembic/command.py", line 125, in upgrade
    script.run_env()
  File "/mnt/home/dmonego/test/local/lib/python2.7/site-packages/alembic/script.py", line 203, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "/mnt/home/dmonego/test/local/lib/python2.7/site-packages/alembic/util.py", line 212, in load_python_file
    module = load_module_py(module_id, path)
  File "/mnt/home/dmonego/test/local/lib/python2.7/site-packages/alembic/compat.py", line 58, in load_module_py
    mod = imp.load_source(module_id, path, fp)
  File "alembic/env.py", line 19, in <module>
    from gen9db.schema import metadata
ImportError: No module named schema

And here is a pdb session running in context:

(test)dmonego@server:~/db/scripts$ alembic upgrade +1
> /mnt/home/dmonego/db/scripts/alembic/env.py(17)<module>()
-> import db
(Pdb) import db
(Pdb) import db.schema
*** ImportError: No module named schema
(Pdb) from db import schema
(Pdb) type(schema)
<type 'module'>

In other words, the script dies when it tries to use the module.submodule syntax, but the submodule exists in the module and can be imported using the "from module import submodule" syntax.

The workaround here is pretty obvious, but I'm a little surprised that this distinction exists. What is the "from a import b" syntax doing differently that could cause this problem?

Dan Monego
  • 9,637
  • 6
  • 37
  • 72
  • You can say `import db`, then use `db.schema` or `from db import schema` to use `schema`, but not a combination of the two. The former maintains the `db` namespace preceding using anything from that module, the latter dumps everything into the global (unqualified) namespace. – Cory Kramer Oct 27 '14 at 17:12
  • @Cyber You can do both, as long as it doesn't create any name conflicts. `import db` binds the `db` module to a variable named `db` in the importing namespace. `from db import schema` binds the attribute or submodule `schema` from the `db` module to a variable named `schema` in the importing namespace. These aren't in any way mutually exclusive (though they would probably be a bit confusing if used together). – Silas Ray Oct 27 '14 at 17:18
  • 2
    alembic appears to be doing its own import magic. Its legal to implement import hooks that only work with the base module and that appears to be happening here. `import db.schema` is different than `from db import schema` - in the latter case, schema could have been generated and added to the module namespace when db was imported but not appear in the sys.modules list. If my hunch is correct, you are stuck with the workaround - its how developers expect you to do it. – tdelaney Oct 27 '14 at 18:43

0 Answers0