Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyDev for Eclipse - Resolve Python dependencies (unresolved imports)

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

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.

My Env:

  • Ubuntu 12.04
  • PyDev for Eclipse 2.7.1.2012100913
  • Python 2.7.3
  • Eclipse indigo

Update: My Pydev Setup:

enter image description here

Questions:

  • 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?

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 env site-package to the PYTHONPATH? so that I can resolve both standard python libraries(eg. json) and my own project dependencies?
like image 705
Shengjie Avatar asked Dec 27 '12 11:12

Shengjie


People also ask

How to remove PyDev from Eclipse?

To uninstall you should either go on and remove the org. python. pydev* from eclipse/plugins and eclipse/features or you can go to help > software updates > manage configuration, choose the PyDev plugin and disable it (after that, to completely remove it, you must restart Eclipse and only then select it for uninstall).

Why PyDev is not showing in Eclipse?

If you have Eclipse installed, but it does not have the PyDev plug-in installed for whatever reason, PyDev is missing from the list of Eclipse preferences. Follow the instructions in the PyDev plug-in manually. Select the latest version of Python 3 installed on your computer, and press .

Which version of Eclipse supports PyDev?

PyDev requires Java 11 and Eclipse 4.6 (Neon) in order to run and only supports Python 2.6 onwards.


2 Answers

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

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.

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: 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

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.

like image 101
jinghli Avatar answered Oct 07 '22 17:10

jinghli


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 whole site-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.

Hopefully, that should work for you. Good luck.

like image 22
Jordan Jambazov Avatar answered Oct 07 '22 17:10

Jordan Jambazov