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