PyDev for Eclipse - 解决 Python 依赖项(未解决的导入)

2022-01-16 00:00:00 python pydev eclipse-plugin java m2eclipse

我使用 PyDev for Eclipse 作为我的 IDE 和 pip 作为我的包管理工具,运行 virtualenv.

I am using PyDev for Eclipse as my IDE and pip as my package management tool, running virtualenv.

每次我想在我的项目中使用/包含一些新库或新依赖项时,我都会将它们添加到 pip-requires 文件中.运行 pip install -r pip-requires 后,依赖项安装在我的虚拟环境中没有问题.

Every time I want to use/include some new libraries or new dependencies in my project, I add them into the pip-requires file. The dependencies are installed in my virtual environment with no problem after running pip install -r pip-requires.

我的环境:

  • Ubuntu 12.04
  • PyDev for Eclipse 2.7.1.2012100913
  • Python 2.7.3
  • 日蚀靛蓝

更新:我的 Pydev 设置:

问题:

  • eclipse/pydev 对添加到我的虚拟环境中的新库一无所知.我不得不手动将 .egg 源文件夹一一添加到 Eclipse 项目的 PYTHONPATH 中,以解决所有未解决的导入问题,这非常烦人!能否以更简单的方式解决 python 依赖项?
  • eclipse/pydev knows 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?

更新:

  • 我发现如果我使用虚拟环境 Env/bin/python 作为屏幕截图所示的解释器,则某些基本模块在 eclipse 中无法解析,例如.import copy, import json 等我应该使用默认的 /usr/bin/python 作为解释器,只需将虚拟环境 site-package 添加到蟒蛇路径?这样我就可以解决标准python库(例如json)和我自己的项目依赖项?
  • 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 env site-package to the PYTHONPATH? so that I can resolve both standard python libraries(eg. json) and my own project dependencies?

推荐答案

当您从 virtualenv 中选择 python 解释器时,请确保您的系统 PYTHONPATH 包含 site-packages 文件夹.就像快照一样.

Make sure your system PYTHONPATH include the site-packages folder when you choose python interpreter from your virtualenv. Just like the snapshot.

那么你就不需要将它们一一添加到 PYTHONPATH 中了.您将需要重新启动 Eclipse(刷新不起作用).将添加新模型.

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.

更新:我检查了 virtualenv 中的 PYTHONPATH.我发现 sys.path 中还有其他一些路径.它们是系统默认的 python 安装位置.这些路径包括基本模型,如 copyjson.

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.

PS:我的环境是 Max OSX 10.8.请通过print sys.path

PS: My environment is Max OSX 10.8. Please check the output of your own platform by 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

我认为这是 virtualenv 的要求.您需要将这些路径添加到 pydev 系统 PYTHONPATH 中.然后就可以使用这些基本模块了.

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.

相关文章