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.
- 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.
- 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 project is using 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 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.
- 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.
- After installing external 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 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.
- 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 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).
- I use PyLint to analyze my python project’s source code. My OS is windows, and I use PyCharm.
- In my python project, there is a python file inner.py in the project subfolder directory.
__init__.py TestApp.py outer.py subfolder\ __init__.py inner.py
- In the inner.py file, it will import the outer module that is defined in the outer.py ( saved in the parent folder of the inner.py file).
- When I execute my python program, there is no error.
- But when I run Pylint on the inner.py file to analyze it, it throws the error message F0401: Unable to import ‘outer’. How can I fix this issue?
5.2 Answer1.
- You can follow the below two methods to fix this issue.
- Add the parent folder path ( the outer.py saved folder path ) in the PYTHONPATH environment variable’s value.
- Or you can include the parent folder path by editing the Pylint file ~/.pylintrc like below.
[MASTER] init-hook='import sys; sys.path.append("< the parent folder path >")'
- If your Pylint version is older and uses the [General] section name in the ~/.pylintrc file, you should change it to [MASTER].
- 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.
- But edit the file ~/.pylintrc works on macOS and Linux, I do not know whether it works on Windows or not.
- 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.
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 setup.py 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,
Shadders.
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?
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.
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.
I have written how to fix the unresolved reference issue that you encountered. Please see section 4 in this article.