When I use eclipse to open an existing PyDev project, I find an error message like Unresolved import: smtplib. This error means the python3 built-in library smtplib can not be found and imported in python source code now. This error is because the python interpreter is not configured correctly, now I will tell you how to fix it.
1. How To Change Python Interpreter Correctly In Eclipse PyDev Project.
- Open eclipse which has installed the PyDev plugin ( How To Run Python In Eclipse With PyDev ).
- Click Eclipse —> Preferences… menu item at eclipse left top menu in macOS. If you use Windows OS, you should click Window —> Preferences menu item.
- Click PyDev —> Interpreters —> Python Interpreter menu item on the left panel. If you can not see a python interpreter configured on the right panel, then just add it. If there has one python interpreter added, maybe this interpreter is not correct, you should change it.
- Click Choose from list ( for macOS) or New —> Choose from list ( for Windows ) button on the right panel, then select the python interpreter which you need in the popup dialog, generally we choose the newest python version.
- Click the OK button, then select all the libraries that will be added in the PYTHONPATH system variable.
- Click the OK button again, now you can see the python interpreter has been added. Select the python interpreter and click the Libraries tab at the right panel bottom, you can see the libraries list that you selected.
- Now click Apply and Close button to close the python interpreter configuration panel to apply your changes.
2. How To Make Python Interpreter Change Take Effect.
- To make the python interpreter change take effect, you need to follow the below steps, otherwise, the Unresolved Import error still exists.
- Right-click the PyDev project, then click Delete menu item in the popup menu list.
- Click the OK button in the popup dialog to delete the project from eclipse. Please do not check Delete project contents on disk checkbox.
- Click eclipse menu item File —> Open Projects from File Systems…
- Click the Directory… button and browse the existing PyDev project saved directory.
- Click the Finish button to complete the PyDev project import. Now you can see the error has disappeared.
- If you want to add third party python libraries into the eclipse PyDev project, you can read the article How To Add Python Module Library In Eclipse PyDev
3. Question & Answer.
3.1 Unresolved import issue when import a python module from .py file.
- I have a python file /user/jerry/work/util/MySQLHelper.py, and this python file contains some utility functions which can operate the MySQL database. I want to invoke those functions in another python project, so I use the python sys module sys.path.append function to add the MySQLHelper module saved directory path to the new python project like below.
import sys sys.path.append("/user/jerry/work/util")
When I import the MySQLHelper module with the command import MySQLHelper in the eclipse Pydev project, it throws the error Unresolved Import Issues. My OS is Ubuntu, I think this error is not related to the OS. Can anyone tell me how to fix this error? Thanks a lot.
- If you use eclipse and Pydev, you can select your Pydev project in the eclipse left side Project Explorer panel, then click the Project —> Properties menu item on the top menu toolbar.
- Click the PyDev – PYTHONPATH item on the project properties window left side.
- Click the External Libraries tab on the window right side.
- Click the Add source folder button at the bottom of the External Libraries tab and select your python file saved folder ( in your case the folder should be /user/jerry/work/util).
- But first, you should add a __init__.py file in the source folder, then you can import the MySQLHelper module in your eclipse Pydev project.