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

When i develop a python program in eclipse pydev project, i need to import a package pymongo into this python program like 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.


eclipse --- project --- properties menu item

This is because my project used python interpreter do 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.
    eclipse --- project --- properties menu item
  2. In the popup dialog, click PyDev – Interpreter/Gramma menu in left panel. Then you can see interpreter drop down list in right panel. You can click the list to select the python interpreter which has installed pymongo library.
    eclipse --- project --- properties --- pydev interpreter
  3. If you want to add new python interpreter, you can click the Click here to configure an interpreter not listed link below the python interpreter drop down list in above window, then click Open interpreter preferences page button to open the Python Interpreters configuration window.
    eclipse - project - proterties - python interpreter - click here to configure a interpreter not listed
  4. You can also click Eclipse —> Preferences menu item to open the eclipse preferences window.
    eclipse --- preferences menu item
  5. Then click PyDev —> Interpreters —> Python Interpreter menu item in above popup window’s left side, then you can see Python Interpreters list in right side. There list all python interpreters that has been added in the eclipse.
    eclipse - preferences - add python interpreter
  6. Click Browse for python/pypy exe button in top right to open Select interpreter dialog. Input an Interpreter Name, and click Browse button to select Interpreter Executable file path( your python virtual environment python executable file ).
    eclipse - preferences - add python interpreter
  7. Then click OK button to close the dialog. Click Apply and Close button to close the Python Interpreters configuration window.
READ :   How To Run Python Script .py File In Jupyter Notebook .ipynb File And IPython

2. Select Python Interpreter For Eclipse Pydev Project.

After you add python interpreter in eclipse successfully, you can now select your pydev project used python interpreter as below.

  1. Click Project —> Properties menu item at eclipse top menu bar.
    eclipse --- project --- properties menu item
  2. Click PyDev – Interpreter/Gramma menu in popup window left panel. Then you can select the newly added python interpreter in right panel Interpreter drop down list.
    eclipse --- project --- properties --- pydev interpreter
  3. Now you can import pymongo library in your python source code.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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