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?