How to Solve "ModuleNotFoundError: No module named 'psutil'" in Python
The error ModuleNotFoundError: No module named 'psutil'
in Python indicates that the psutil
library, which provides information on running processes and system utilization, is not installed in your current Python environment.
This guide provides a complete guide to installing psutil
, troubleshooting common installation problems, and configuring popular IDEs (VS Code, PyCharm) and Jupyter Notebook for use with psutil
.
Installing psutil
(The Basic Solution)
The standard way to install psutil
is using pip
:
pip install psutil
-
Multiple Python Versions: If you have multiple Python versions, use
pip3
(or a specific version likepip3.9
):pip3 install psutil
-
Permissions Errors: If you get permission errors:
sudo pip3 install psutil # Linux/macOS (Use with caution!)
pip install psutil --user # Install for current user onlyUsing virtual environments (explained below) is the best way to avoid permission problems.
-
python -m pip
: Ifpip
isn't directly in yourPATH
, use:python -m pip install psutil # Or python3 -m pip, or py -m pip
-
Conda: If you are using Anaconda, you should use the following command to install
psutil
:conda install -c conda-forge psutil
Verifying the Installation
After installing, verify that psutil
is accessible:
import psutil
print(psutil.cpu_times())
for x in range(3):
psutil.cpu_percent(interval=1)
- This code will import the library, and call some methods on it to confirm that it has been correctly installed.
If this script runs without a ModuleNotFoundError
, psutil
is installed. If you still get an error, continue to troubleshooting.
Troubleshooting ModuleNotFoundError
Virtual Environments (Essential)
Always use virtual environments:
- Create:
python3 -m venv venv
(orpython -m venv venv
,py -m venv venv
) - Activate:
- Linux/macOS:
source venv/bin/activate
- Windows (cmd):
venv\Scripts\activate.bat
- Windows (PowerShell):
venv\Scripts\Activate.ps1
- If you get errors in PowerShell try running
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
.
- If you get errors in PowerShell try running
- Linux/macOS:
- Install:
pip install psutil
(inside the activated environment).
Your terminal prompt should change (e.g., (venv) $
).
Multiple Python Versions:
- Check:
python --version
(orpython3 --version
) - Correct
pip
: Usepip
,pip3
,pip3.x
, orpython -m pip
to match your intended Python version.
IDE Configuration (VS Code, PyCharm)
Configure your IDE to use the correct interpreter/environment.
Jupyter Notebook
Install within a notebook cell:
!pip install psutil
Ensure your Jupyter kernel uses the right environment.
Naming Conflicts
Never name your files psutil.py
.
Reinstalling psutil
pip uninstall psutil
pip install psutil
Installation Instructions for Specific Environments
Windows:
- Open Command Prompt or PowerShell.
- Use
pip install psutil
(orpy -m pip install psutil
).
macOS / Linux:
- Open your Terminal
- Use
pip3 install psutil
Anaconda:
- Use
conda install -c conda-forge psutil
Jupyter Notebook (within a cell):
!pip install psutil
Conclusion
The ModuleNotFoundError: No module named 'psutil'
error is usually easy to resolve.
The key steps are to install psutil
using pip, always use virtual environments, and ensure your IDE or Jupyter Notebook is using the correct Python environment where psutil
is installed.
By following these steps, you can use psutil
to monitor system and process information in your Python programs.