When you develop a python application with the eclipse PyDev plugin, you always need to use some third-party python module libraries. This article will just tell you how to add library in python eclipse project and how to use the library in your python source code.
1. Install Python Module Library.
Before you can configure and use a third-party python module, you need to install it first. You can do it follow the below steps.
- Open a terminal and execute the command
pip -help, this will list the hep doc for pip command.
pip listcommand in the terminal, it will list all the installed module libraries.
- If your needed python module does not exist in the list, run
pip install module_nameto install it.
pip install PyPDF2
- If you want to see the detailed information about the installed module ( for example the module installed directory ), run
pip show module_namecommand.
sh-3.2# pip show PyPDF2 Name: PyPDF2 Version: 1.26.0 Summary: PDF toolkit Home-page: http://mstamy2.github.com/PyPDF2 Author: Mathieu Fenniak Author-email: [email protected] License: UNKNOWN Location: /Users/zhaosong/anaconda3/lib/python3.6/site-packages Requires: Required-by:
- Although you have installed the python module, if you do not add the module library in the PYTHONPATH environment variable, when you execute the python application, it will throw error messages like below.
Traceback (most recent call last): File "/Users/zhaosong/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/com/dev2qa/example/file/PDFExtract.py", line 9, in <module> import PyPDF2 ModuleNotFoundError: No module named 'PyPDF2'
2. Add Python Module Library In Eclipse PyDev Project.
This is something like add jar libraries in an eclipse java project.
- First, you need to add the PyDev plugin in eclipse, you can read the article How To Run Python In Eclipse With PyDev.
- Create a PyDev eclipse project.
- Right-click the PyDev project name in the left package explorer panel. And click the Properties menu item in the popup menu list.
- Select PyDev – PYTHONPATH item in popup window left panel. Then click the External Libraries tab in the right panel.
- Click Add source folder button to open a dialog. And browse to the folder where the python module library exists. Then click Apply and Close button to make it effective.
- If you do not know where is the library installed folder, you can run the command pip show python-library-name to get its installed directory from the Location output like below.
>pip show easygui Name: easygui Version: 0.98.2 Summary: EasyGUI is a module for very simple, very easy GUI programming in Python. EasyGUI is different from other GUI generators in that EasyGUI is NOT event-driven. Instead, all GUI interactions are invoked by simple function calls. Home-page: https://github.com/robertlugg/easygui Author: easygui developers and Stephen Ferg Author-email: [email protected] License: BSD Location: c:\users\zhaosong\anaconda3\envs\python_example\lib\site-packages Requires: Required-by:
- Now when you import the python module in your python source code, the eclipse editor will automatically prompt the module name for you.
2. Add Python Module Library In Eclipse Globally.
The above method will just add the python module library in the special PyDev project, if you create another PyDev project, you need to add the module lib in the new PyDev project again.
But we can add the python module library globally in all the eclipse PyDev projects. So that when you create a new PyDev project, it can use the python module lib directly in python source code, no more project settings are needed.
- Open eclipse and click Eclipse —> Preferences… menu item in macOS, or Window —> Preferences in windows.
- Select PyDev —> Interpreters —> Python Interpreter menu item in Preferences window left panel.
- Click the Libraries tab in the right panel. Click the New Folder button to browse and select the python module library folder.
- After that, you can see the module library folder has been added under the System Libs node.
- Click Apply and Close button to make it effective.
- Now you can create a new PyDev project, and when you expand the project —> python —> System Libs node, you can see the newly added python module library in it.
- How To Run Python In Eclipse With PyDev.
- How To Install Python Library ( such as Pandas ) In PyCharm, PyDev Eclipse
3 thoughts on “How To Add Library In Python Eclipse Project”
I run the command pip install django and pip install selenium to install python django and selenium library successfully, and I find them have been added in the folder /anaconda3/lib/python3.6/site-packages. And I write a python script which use both django and selenium libraries, and the python script runs well when I run it in command line. But when I run the python script in an eclipse pydev project, it show me an error message like ImportError: cannot import name ‘http_cookies’. And I am sure I had add the django and selenium library in the PYTHONPATH follow this article. And I think the python http_cookies module should be installed by the pip install selenium or pip install django command. But why this error happened, I need your help, thanks.
You saved me!
On the Mac, I found that the libs were in a hidden folder, eg:
These are invisible until you click Shift + Command + “.”
Another way is to use the Preferences-PyDev-Python Interpreter and click on “Manage with PIP” in the bottom area. For some reason, Eclipse/PyDev/Windows insists on building, say pyaudio, rather than using a wheel so you have to install C++ Version 14 which seems to mean installing a large chunk of Visual Studio.
I swear, Python development is vastly harder than Java or C++ and it’s only that my Linux box is even more ancient than my Windows machine… I assume that the Linux installers automatically take care of secondary requirements.