When I use PyCharm to develop a Python example, I need to import a library ( PyMySQL: A MySQL database Python driver library. ) into the Python project path, so that PyCharm can find the library files when coding and executing source code. But how to install and import libraries in PyCharm? This article will tell you how to add external libraries into the PyCharm project step by step, it is different from adding external libraries in eclipse PyDev.
1.PyCharm Add External Library Steps.
This example will demo the PyCharm import library process step by step, it uses the PyCharm macOS version, the PyCharm Windows or Linux version is similar.
- Open PyCharm, click PyCharm —> Preferences menu item.
- Then click Project —> Project Interpreter menu item in the left panel. And select the python version that your current python roject is used on the right side Project Interpreter drop-down list.
- Click the
+button in the right panel bottom left corner to popup the Available Packages window to install a third-party library.
- Input the library name in the search text box ( for example PyMysql), select the library, and click the Install Package button to install it.
- When the install libraries in the PyCharm project process complete successfully, you can see the third-party library in the project interpreter package list. Click the OK button to save the changes.
- If you want to add external libraries for a new project in PyCharm, you can click File —> Preferences for New Projects menu item to open a popup window, then click Project Interpreter menu item in the left panel, and install libraries in PyCharm project like above.
2. Installed Libraries In PyCharm Saved Location.
- After install libraries in PyCharm, you may want to know where the library is installed.
- In the PyCharm left project panel, select Project view and expand the External Libraries item.
- Then you will find the installed PyMysql library in the Python 3.6 —> site-packages folder.
- Now you can import and use the PyMysql library in your Python code edited with PyCharm.
3. Install External Libraries In PyCharm Project On Linux Ubuntu.
- Click PyCharm File —> Settings menu item in the top menu bar to open the project Settings window.
- Then click Project: <project name> —> Project Interpreter menu item in the left panel to list the current project interpreter and the installed external libraries on the right side.
- Click the
+icon in the right panel to open the Available Packages window to install the external library.
- Search and select related library package. Please note, you should check the Install to user’s site packages directory(/home/zhaosong/.local) checkbox, otherwise you can not find the library package in Project panel —> External Libraries.
- Now you can use the external library package in your Django code and find it in the Project panel —> External Libraries —> Python 3.6 —> site-packages.
4. How To Add Custom Python Module ( wrote in your own .py file) To PyCharm PYTHONPATH To Fix Unresolved Reference Issue.
- When you import a custom python module that is written in your own .py file in PyCharm, you may encounter the Unresolved Reference Issue. Now I will tell you how to fix it.
- It is very easy to add your own python module to the PyCharm PYTHONPATH environment variable. Then you can reference your custom python module in the PyCharm project without error. Please follow the below steps.
- Put all your own python files (.py file) that contain the python module into an src folder.
- Right-click the src folder in the PyCharm project panel on the left side.
- Click the Mark Directory as —> Sources Root menu item in the popup menu list.
- Now the src folder will change its color to light blue.
- Click the PyCharm —> Preferences… menu item at PyCharm top menu bar.
- Expand the Build, Execution, Deployment —> Console menu item.
- Click the Python Console menu item under the above menu item. And check the Add content roots to PYTHONPATH and Add source roots to PYTHONPATH checkbox. Click the OK button to save the changes.
- Click the PyCharm File —> Invalidate Caches/Restart… menu item to restart the PyCharm IDE, and the unresolved reference issue error in PyCharm editor will be fixed now.