使用virtualenv,让它工作[我认为],但没有运气尝试导入模块。 在文件makotest.py中,我有:
from mako.template import Template
位于我的主文件夹,virtualenv在文件夹:venv
安装mako就好了,pip冻结:
Mako==1.0.0
MarkupSafe==0.23
argparse==1.2.1
wsgiref==0.1.2
在shell中,我看到(venv)所以它应该正常工作?
我运行了一个syspath并激活了它并获得了:
'/home/username'
'/home/username/venv/lib/python2.7'
'/home/username/venv/lib/python2.7/plat-x86_64-linux-gnu'
'/home/username/venv/lib/python2.7/lib-tk'
'/home/username/venv/lib/python2.7/lib-old'
'/home/username/venv/lib/python2.7/lib-dynload'
'/usr/lib/python2.7'
'/usr/lib/python2.7/plat-x86_64-linux-gnu'
'/usr/lib/python2.7/lib-tk'
'/home/username/venv/local/lib/python2.7/site-packages'
'/home/username/venv/lib/python2.7/site-packages'
在该site-packages文件夹中,是一个带有template.py/pyc和init文件的'mako'文件夹。
但每次运行'python makotest.py'时,都会收到导入错误。 完全没有想法,请帮忙〜
使用追溯更新
Traceback (most recent call last):
File "makotest.py", line 1, in <module>
from mako.template import *
File "/home/username/venv/makotest.py", line 1, in <module>
from mako.template import *
ImportError: No module named template
答案 0 :(得分:1)
你的问题缺乏完整的python回溯,所以我只能根据我的理解回答这个问题。
你的cwd(以及pythonpath)包含一个mako.py文件的事实是你被绊倒的地方。尝试重命名,然后首先删除主目录中的mako.pyc,然后重新运行它。
基本上python试图从你的mako.py文件而不是你的站点包中的mako.py目录导入。
请参阅下面的示例:
-bash-4.1$ bin/pip freeze
#...
mock==1.0.1
#..
-bash-4.1$ bin/python
Python 2.7.3 (default, Apr 10 2013, 09:39:41)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from mock import patch
in mock.py
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mock.py", line 3, in <module>
from mock import patch
ImportError: cannot import name patch
>>>
-bash-4.1$ mv mock.py mock2.py
-bash-4.1$ rm mock.pyc
-bash-4.1$ bin/python
Python 2.7.3 (default, Apr 10 2013, 09:39:41)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from mock import patch
>>> patch
<function patch at 0x7f8276a4a7d0>