简体   繁体   English

使用virtualenv Python导入错误,但是iPython可以正常工作

[英]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.

相关问题 为什么导入google.protobuf在ipython中运行正常但在python中运行不正常? - Why the import of google.protobuf works fine in ipython but not in python? 无法在ipython中导入seaborn,但在python控制台中可以正常工作 - Cannot import seaborn in ipython but works fine in python console 导入可在Python中工作,但不能在IPython中工作 - Import works in Python but not IPython 使用Virtualenv时无法导入软件包,但在Virtualenv之外可以正常工作 - Unable to import a package when using Virtualenv but works fine outside of Virtualenv 报告MissingImports Python 错误但导入工作正常 - reportMissingImports Python error but import works fine Python ctypes在virtualenv中导入错误 - Python ctypes import error in virtualenv Virtualenv:“ main.py”给出了错误,但“ python main.py”工作得很好 - Virtualenv: “main.py” gives error but “python main.py” works perfectly fine 在Atom中为Python-Pandas导入模块错误(在CLI中可以正常工作???) - Import Module Error for Python-Pandas in Atom (works fine in CLI???) Python模块导入错误(在Linux上工作正常,但在FreeBSD中失败) - Python module import error (Works fine in linux but fails in FreeBSD) 无法在PyCharm中加载DLL python模块。 在IPython中工作正常 - Unable to load DLL python module in PyCharm. Works fine in IPython
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM