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.