java PyDev for Eclipse - 解决 Python 依赖项(未解析的导入)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14054111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PyDev for Eclipse - Resolve Python dependencies (unresolved imports)
提问by Shengjie
I am using PyDev for Eclipse as my IDE and pip as my package management tool, running virtualenv.
我使用 PyDev for Eclipse 作为我的 IDE,使用 pip 作为我的包管理工具,运行 virtualenv。
Every time I want to use/include some new libraries or new dependencies in my project, I add them into the pip-requiresfile. The dependencies are installed in my virtual environment with no problem after running pip install -r pip-requires
.
每次我想在我的项目中使用/包含一些新库或新依赖项时,我都会将它们添加到pip-requires文件中。运行后,依赖项安装在我的虚拟环境中没有问题pip install -r pip-requires
。
My Env:
我的环境:
- Ubuntu 12.04
- PyDev for Eclipse 2.7.1.2012100913
- Python 2.7.3
- Eclipse indigo
- Ubuntu 12.04
- Eclipse 2.7.1.2012100913 的 PyDev
- 蟒蛇 2.7.3
- 日蚀靛蓝
Update: My Pydev Setup:
更新:我的 Pydev 设置:
Questions:
问题:
- eclipse/pydevknows nothing about the new libraries being added in my virtual env. I had to manually add the .egg source folder into eclipse project's PYTHONPATH one by one in order to resolve all the unresolved imports which was very annoying! Can the python dependencies be resolved in a easier manner?
- eclipse/pydev对在我的虚拟环境中添加的新库一无所知。我不得不手动将 .egg 源文件夹一一添加到 eclipse 项目的 PYTHONPATH 中,以解决所有未解析的导入,这很烦人!能否以更简单的方式解决 python 依赖项?
Update:
更新:
- I found that if I use virtual env
Env/bin/python
as the interpreter as the screenshot indicates, some of the basic modules are not resolved in eclipse, eg.import copy, import json
etc. should I use default/usr/bin/python
as interpreter and just add virtual envsite-package
to the PYTHONPATH? so that I can resolve both standard python libraries(eg. json) and my own project dependencies?
- 我发现如果我使用虚拟环境
Env/bin/python
作为解释器,如截图所示,一些基本模块在 eclipse 中没有解析,例如。import copy, import json
等等,我应该使用默认/usr/bin/python
作为解释器,然后将虚拟环境添加site-package
到 PYTHONPATH 吗?这样我就可以同时解析标准 python 库(例如 json)和我自己的项目依赖项?
采纳答案by jinghli
Make sure your system PYTHONPATH include the site-packages folder when you choose python interpreter from your virtualenv. Just like the snapshot.
当您从 virtualenv 中选择 python 解释器时,请确保您的系统 PYTHONPATH 包含 site-packages 文件夹。就像快照一样。
Then you don't need to add them one by one into PYTHONPATH. You will need to restart eclipse (Refresh doesn't work). New models will be added.
那么你就不需要将它们一一添加到 PYTHONPATH 中。您将需要重新启动 eclipse(刷新不起作用)。将添加新模型。
Update:
I checked the PYTHONPATH in the virtualenv. I found there are some other paths in the sys.path. They are the system default python installed location. These path includes the basic models, like copy
and json
.
更新:我检查了 virtualenv 中的 PYTHONPATH。我发现 sys.path 中还有一些其他路径。它们是系统默认的 python 安装位置。这些路径包括基本模型,例如copy
和json
。
PS: My environment is Max OSX 10.8. Please check the output of your own platform by print sys.path
PS:我的环境是Max OSX 10.8。请检查您自己平台的输出print sys.path
/Users/username/.virtualenvs/test/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/Users/username/.virtualenvs/test/lib/python2.7/site-packages/pip-1.1-py2.7.egg
/Users/username/.virtualenvs/test/lib/python27.zip
/Users/username/.virtualenvs/test/lib/python2.7
/Users/username/.virtualenvs/test/lib/python2.7/plat-darwin
/Users/username/.virtualenvs/test/lib/python2.7/plat-mac
/Users/username/.virtualenvs/test/lib/python2.7/plat-mac/lib-scriptpackages
/Users/username/.virtualenvs/test/lib/python2.7/lib-tk
/Users/username/.virtualenvs/test/lib/python2.7/lib-old
/Users/username/.virtualenvs/test/lib/python2.7/lib-dynload
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Users/username/.virtualenvs/test/lib/python2.7/site-packages
I think this is the requirement of virtualenv. You need to add these paths into pydev system PYTHONPATH. Then you can use these basic modules.
我认为这是 virtualenv 的要求。您需要将这些路径添加到 pydev 系统 PYTHONPATH 中。然后就可以使用这些基本模块了。
回答by Jordan Jambazov
From your questions I understood that you're manually adding the Python Egg packages to the path. Instead of doing this, you can just go to the Project Settings window, then open "PyDev - PYTHONPATH", navigate to the panel called "External Libraries" and add the wholesite-packages
folder of your virtual environment, usually /path/to/virtualenv/lib/site-packages/
. This way you won't add the Eggs one by one for each packages. When you install a new package, just go to the Eclipse window and if the import is still not resolved, select the project and press the F5
key, to refresh the project.
从您的问题中我了解到您正在手动将 Python Egg 包添加到路径中。而不是这样做,您只需转到“项目设置”窗口,然后打开“PyDev - PYTHONPATH”,导航到名为“外部库”的面板并添加虚拟环境的整个site-packages
文件夹,通常是/path/to/virtualenv/lib/site-packages/
. 这样你就不会为每个包裹一个一个地添加鸡蛋。当你安装一个新的包时,只需要进入Eclipse窗口,如果导入仍然没有解决,选择项目并按F5
键,刷新项目。
Hopefully, that should work for you. Good luck.
希望这对你有用。祝你好运。
回答by Anshuman Bhaduri
This is how I go about setting up PyDev with a virtualenv:
这就是我如何使用 virtualenv 设置 PyDev:
- Create a virtualenv at
/home/anshumanb/.virtualenvs/myvenv
- In Eclipse, Window > Preferences > PyDev > Interpreter - Python, I add a new interpreter. For the interpreter executable, I point it to the virtualenv's Python i.e.
/home/anshumanb/.virtualenvs/myenv/bin/python
- In the "Select folders to be added to PYTHONPATH" screen, I select all the paths starting with
/home/anshumanb/.virtualenvs/myvenv
AND/usr/lib/python2.7
/usr/lib/python2.7/plat-linux2
/usr/lib/python2.7/lib-tk
and nothing else.
(See thisfor why I select/usr/lib/python2.7
,/usr/lib/python2.7/plat-linux2
and/usr/lib/python2.7/lib-tk
) - When I'm back at the Interpreter - Python screen in Eclipse preferences, I hit Apply then OK
- 创建一个 virtualenv 在
/home/anshumanb/.virtualenvs/myvenv
- 在 Eclipse 中,Window > Preferences > PyDev > Interpreter - Python,我添加了一个新的解释器。对于解释器可执行文件,我将它指向 virtualenv 的 Python ie
/home/anshumanb/.virtualenvs/myenv/bin/python
- 在“选择要添加到 PYTHONPATH 的文件夹”屏幕中,我选择了所有以
/home/anshumanb/.virtualenvs/myvenv
AND开头的路径/usr/lib/python2.7
/usr/lib/python2.7/plat-linux2
/usr/lib/python2.7/lib-tk
,没有其他选择。
(请参阅此处了解我为什么选择/usr/lib/python2.7
,/usr/lib/python2.7/plat-linux2
和/usr/lib/python2.7/lib-tk
) - 当我回到 Eclipse 首选项中的 Interpreter - Python 屏幕时,我点击 Apply 然后点击 OK
After this, whenever I install any packages into the virtualenv, I go back to the Interpreter - Python screen, hit Apply, select the interpreter that points to my virtualenv (and the one I just installed packages into) and hit Apply. PyDev then picks up any new packages installed in the virtualenv.
此后,每当我将任何软件包安装到 virtualenv 中时,我都会返回解释器 - Python 屏幕,点击应用,选择指向我的 virtualenv(以及我刚刚安装软件包的那个)的解释器,然后点击应用。PyDev 然后选择安装在 virtualenv 中的任何新包。
Note: I'm using PyDev 2.7.1 and Python 2.7.3 but on an Arch Linux box with Eclipse 4.2.1. But I don't think it'll make any difference.
注意:我使用的是 PyDev 2.7.1 和 Python 2.7.3,但在带有 Eclipse 4.2.1 的 Arch Linux 机器上。但我认为这不会有任何区别。