[英]Import error with virtualenv Python but iPython works fine
I am using virtualenv and virtualenvwrapper on a Mac OS X machine. 我在Mac OS X机器上使用virtualenv和virtualenvwrapper。 My project layout is: 我的项目布局是:
/Users/mrafayaleem/Projects/imagemonster/ (project dir)
|---- imagemonster/
|---- server.py
|---- conf/
|---- settings.py
I am getting Import error on the following line in server.py: 我在server.py的以下行上遇到导入错误:
from imagemonster.conf import settings
when I run it using python imagemonster/server.py
from the project directory. 当我从项目目录使用python imagemonster/server.py
运行它时。 Running it under ipyhton
works just fine and I can't understand why this is happening. 在ipyhton
下运行它可以ipyhton
工作,我不明白为什么会这样。
Following are sys.paths for both: 以下是这两者的sys.paths:
Python: 蟒蛇:
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python27.zip
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/plat-darwin
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/plat-mac
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/plat-mac/lib-scriptpackages
/Users/mrafayaleem/.virtualenvs/imagemonster/Extras/lib/python
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/lib-tk
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/lib-old
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/site-packages
iPython: IPython的:
/Users/mrafayaleem/.virtualenvs/imagemonster/bin
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python27.zip
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/plat-darwin
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/plat-mac
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/plat-mac/lib-scriptpackages
/Users/mrafayaleem/.virtualenvs/imagemonster/Extras/lib/python
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/lib-tk
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/lib-old
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/site-packages
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/site-packages/IPython/extensions
/Users/mrafayaleem/.ipython
Can anyone please help me resolve this weird behaviour? 谁能帮我解决这个怪异行为?
This isn't due to virtualenv. 这不是由于virtualenv。 It's because of differences between python and IPython. 这是由于python和IPython之间的差异。 IPython also imports from the current working directory as well as searching in the PATHs. IPython 还会从当前工作目录中导入以及在PATH中进行搜索。 There's a similar answer in this SO thread . 在这个SO线程中有一个类似的答案。
You should cd
into the imagemonster directory, don't workon
virtualenv, and try: 您应该cd
进入imagemonster目录,不要workon
virtualenv上工作,然后尝试:
python server.py
And deal with the import error that throws. 并处理抛出的导入错误。
Coming a little too late but this might help others looking for a solution. 来得太晚了,但这可能会帮助其他人寻找解决方案。 Ipython uses both your current system path and your current project directory but when running in a virtualenv, only the current PYTHONPATH specified during installation will be used. Ipython同时使用当前系统路径和当前项目目录,但是在virtualenv中运行时,仅会使用在安装过程中指定的当前PYTHONPATH。 You have to append your current working directory to your module import declarations: 您必须将当前工作目录附加到模块导入声明中:
This is what I would do: 这就是我要做的:
import sys
sys.path.append('/Users/mrafayaleem/Projects/imagemonster/conf')
from imagemonster.conf import settings
The solution here gives a more precise solution especially when working in virtualenv https://stackoverflow.com/questions/19876993/python-module-import-relative-paths-issue 这里的解决方案提供了更精确的解决方案,尤其是在virtualenv中工作时https://stackoverflow.com/questions/19876993/python-module-import-relative-paths-issue
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.