当我在配置脚本中导入模块时,我遇到了这种行为,我用它来驱动alembic。该脚本是由我的virtualenv中安装的模块从文件系统动态加载的,这似乎是发生此问题的唯一情况。
以下是堆栈跟踪:
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
这是在上下文中运行的pdb会话:
(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'>
换句话说,脚本在尝试使用module.submodule语法时会死亡,但子模块存在于模块中,可以使用&#34; from module import submodule&#34;语法。
这里的解决方法非常明显,但我对这种区别存在感到有些惊讶。什么是&#34;来自进口b&#34;语法有何不同可能会导致此问题?