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 to the PyCharm project step by step, it is different from adding external libraries in eclipse PyDev.

1.PyCharm Add External Library Steps.

  1. This example will demo the PyCharm import library process step by step, it uses the PyCharm macOS version, and the PyCharm Windows or Linux version is similar.
  2. Open PyCharm, click PyCharm —> Preferences menu item.
  3. Then click Project —> Project Interpreter menu item in the left panel. And select the python version that your current python project is using on the right side Project Interpreter drop-down list.
  4. Click the + button in the right panel bottom left corner to popup the Available Packages window to install a third-party library.
  5. Input the library name in the search text box ( for example PyMysql), select the library, and click the Install Package button to install it.
  6. When the installing libraries in the PyCharm project process are completed successfully, you can see the third-party library in the project interpreter package list. Click the OK button to save the changes.
  7. 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 PyCharm External Libraries Saved Location.

  1. After installing external 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 the related library packages. Please note that you should check the Install to user’s site-packages directory(/home/zhaosong/.local) checkbox, otherwise you can not find the library package in the 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 the PyCharm editor will be fixed now.

5. How To Fix Unable To Import Error When Use Pylint To Analyse Python Source Code?

5.1 Question(2022/05/22).

  1. I use PyLint to analyze my python project’s source code. My OS is windows, and I use PyCharm.
  2. In my python project, there is a python file in the project subfolder directory.
  3. In the file, it will import the outer module that is defined in the ( saved in the parent folder of the file).
  4. When I execute my python program, there is no error.
  5. But when I run Pylint on the file to analyze it, it throws the error message F0401: Unable to import ‘outer’. How can I fix this issue?

5.2 Answer1.

  1. You can follow the below two methods to fix this issue.
  2. Add the parent folder path ( the saved folder path ) in the PYTHONPATH environment variable’s value.
  3. Or you can include the parent folder path by editing the Pylint file ~/.pylintrc like below.
    init-hook='import sys; sys.path.append("< the parent folder path >")'
  4. If your Pylint version is older and uses the [General] section name in the ~/.pylintrc file, you should change it to [MASTER].
  5. If you use VSCode as the python source code file editor, using the Pylint hook is better than editing the PYTHONPATH. If you do want to set the PYTHONPATH variable, you can set it in the .env file.
  6. But edit the file ~/.pylintrc works on macOS and Linux, I do not know whether it works on Windows or not.
  7. If you change the PYTHONPATH environment variable value, do not forget to export it with the command export PYTHONPATH, then your Pylint can find the parent folder.

5 thoughts on “PyCharm Project Add External Library (PyMySQL) Path Example”

  1. Hi,
    I have a similar issue to those above, but not exact. I am using PyCharm for general work, but also use Jupyter and Spyder. What i am having difficulty in determining, is how to add my .local/lib/python3.10/site-packages to PyCharm as the path to look for. I have attempted to install using Python Packages tab using the From Disk option, but that fails as there is not a etc. I want to install libraries using pip from the Linux command line, to use with Jupyter, Spyder and PyCharm, and not have to add duplicates for every PyCharm project. I will change Projects depending on my work. Is it possible to point to /local libraries as installed by Linux command line pip ?
    Thanks and regards,

  2. 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?

    1. Your PYTHONPATH environment variable’s value defines the path where the external python libraries will be installed. You can also add your custom folder path in the PYTHONPATH environment variable’s value and save your python module files in the folder. Then you can import the python module into other python source files to use.

  3. 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

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