How To Change Python Interpreter In Eclipse Pydev Project To Use Different Python Virtual Environment Library

When I develop a python program in the eclipse Pydev project, I need to import a package pymongo into this python program like the below source code.

import pymongo

if __name__ == '__main__':
    pass

But it shows an error message Unresolved import: pymongo in the source code, you can see this error message when you move your mouse over the red line in eclipse python source code.

This is because my eclipse project used a python interpreter that does not contain pymongo library. So I have two options to fix this error.

  1. Option 1: Install pymongo library in the project used python interpreter.
  2. Option 2: Use another python virtual environment ( which has installed pymongo library ) as the project’s python interpreter. This article will focus on this option.

1. Add New Python Interpreter In Eclipse Steps.

  1. Click Project —> Properties menu item at eclipse top menu bar. You can change the settings for this single project through this menu item.
  2. In the pop-up dialog, click the PyDev – Interpreter/Gramma menu in the left panel. Then you can see the interpreter drop-down list in the right panel. You can click the list to select the python interpreter which has installed the pymongo library.
  3. If you want to add a new python interpreter, you can click the Click here to configure an interpreter not listed link below the python interpreter drop-down list in the above window, then click the Open interpreter preferences page button to open the Python Interpreters configuration window.
  4. You can also click Eclipse —> Preferences ( on macOS ) or Window —> Preferences ( on Windows ) menu item to open the eclipse preferences window.
  5. Then click PyDev —> Interpreters —> Python Interpreter menu item in above popup window’s left side, then you can see the Python Interpreters list on the right side. There list all python interpreters that have been added to the eclipse.
  6. Click Browse for python/pypy exe button in the top right to open Select interpreter dialog. Input an Interpreter Name, and click the Browse button to select Interpreter Executable file path( your python virtual environment python executable file ( python.exe for Windows or python for macOS, Linux ) ).
  7. Then click the OK button to close the dialog. Click Apply and Close button to close the Python Interpreters configuration window.

2. Select Python Interpreter For Eclipse Pydev Project.

  1. After you add python interpreter in eclipse successfully, you can now select your Pydev project used python interpreter as below.
  2. Click Project —> Properties menu item at eclipse top menu bar.
  3. Click PyDev – Interpreter/Gramma menu in popup window left panel. Then you can select the newly added python interpreter from the right panel Interpreter drop-down list.
  4. Now you can import the python pymongo library into your python source code without error.

3. Question & Answer.

3.1 How to effectively manage python interpreters and virtual environments in the eclipse pydev projects.

  1. I have several eclipse pydev projects, and each project uses its own python interpreter. The reason for this is because the python version or python libraries are not the same for different eclipse pydev projects. But as there is more and more python projects, it is very hard to manage so many python interpreters with different python virtual environment. is there a way to easily manage all those python interpreters or make all the eclipse python project use the same one python interpreter?
  2. In eclipse pydev, you can only make the python interpreter eclipse-wide. If you switch between multiple eclipse pydev projects, you have to switch to the correct python interpreter also. This can make the eclipse pydev project isolated in multiple pydev projects. It can also reduce the influence between them.
  3. If all your eclipse pydev projects use the same python version, and the only difference is the installed python libraries. You can create a base python interpreter and make all the eclipse pydev projects use that python interpreter as default.
  4. Then you can configure the python libraries for each eclipse pydev project in the project properties PYTHONPATH, you can read the article How To Add Library In Python Eclipse Project to learn more.

References

  1. How To Manage Anaconda Environments.
  2. How To Start Jupyter Notebook In Anaconda Python Virtual Environment.
  3. How To Install Python Django In Virtual Environment.
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x