PyCharm Project Add External Library (PyMySQL) Path Example

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.

  1. Open PyCharm, click PyCharm —> Preferences menu item.
  2. 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.
  3. Click the + button in the right panel bottom left corner to popup the Available Packages window to install a third-party library.
  4. Input the library name in the search text box ( for example PyMysql), select the library, and click the Install Package button to install it.
  5. 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.
  6. 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.

  1. After install libraries in PyCharm, you may want to know where the library is installed.
  2. In the PyCharm left project panel, select Project view and expand the External Libraries item.
  3. Then you will find the installed PyMysql library in the Python 3.6 —> site-packages folder.
  4. 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.

  1. Click PyCharm File —> Settings menu item in the top menu bar to open the project Settings window.
  2. 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.
  3. Click the + icon in the right panel to open the Available Packages window to install the external library.
  4. 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.
  5. 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.

  1. 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.
  2. 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.
  3. Put all your own python files (.py file) that contain the python module into an src folder.
  4. Right-click the src folder in the PyCharm project panel on the left side.
  5. Click the Mark Directory as —> Sources Root menu item in the popup menu list.
  6. Now the src folder will change its color to light blue.
  7. Click the PyCharm —> Preferences… menu item at PyCharm top menu bar.
  8. Expand the Build, Execution, Deployment —> Console menu item.
  9. 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.
  10. 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.
0 0 votes
Article Rating
Notify of

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

Oldest Most Voted
Inline Feedbacks
View all comments

So I don’t quite know where I’m going wrong but do the external libs usually get downloaded and install in the same directory as python or a separate one?
It appears that pycharm is looking here,
But when it installs a Module, it save to a different location?


I have a python script which imports a custom python module that wrote in a .py file by myself, the code is from MyModule import *. It runs well when I run the python script in the command line. But when I import the python script file into a PyCharm project, it shows an error that said it can not resolve the reference of the MyModule module. I am new to PyCharm, can anyone tell me how to fix this? Thanks.

Would love your thoughts, please comment.x