Last modified: Oct 17, 2024 By Alexander Williams
How To Fix ModuleNotFoundError: No module named 'psutil' in Python
When you encounter the ModuleNotFoundError: No module named 'psutil' error in Python, it means the psutil package isn't installed in your Python environment. This guide will help you resolve this common error quickly and easily.
For more information about similar Python errors, check out our guide on How To Solve ModuleNotFoundError: No module named in Python.
What is psutil?
psutil (Python System and Process Utilities) is a cross-platform library for retrieving information about running processes and system utilization (CPU, memory, disks, network, sensors) in Python.
Understanding the Error
The error appears when you try to import psutil in your code:
import psutil
Traceback (most recent call last):
File "your_script.py", line 1, in
import psutil
ModuleNotFoundError: No module named 'psutil'
How to Fix the Error
1. Using pip (Recommended Method)
The simplest way to install psutil is using pip:
# For Windows
pip install psutil
# For Linux/macOS
pip3 install psutil
2. System-specific Installation
For Linux users, you might need to install additional dependencies:
# Ubuntu/Debian
sudo apt-get install python3-psutil
# Fedora
sudo dnf install python3-psutil
# CentOS/RHEL
sudo yum install python3-psutil
Verifying the Installation
To verify that psutil is installed correctly, open Python's interactive shell and try:
import psutil
print(psutil.__version__)
Common Usage Examples
Here's a simple example to monitor system resources:
import psutil
# CPU usage
cpu_percent = psutil.cpu_percent(interval=1)
print(f"CPU Usage: {cpu_percent}%")
# Memory usage
memory = psutil.virtual_memory()
print(f"Memory Usage: {memory.percent}%")
# Disk usage
disk = psutil.disk_usage('/')
print(f"Disk Usage: {disk.percent}%")
Troubleshooting Tips
If you still encounter issues after installation:
- Try upgrading pip:
pip install --upgrade pip
- Use a virtual environment to avoid conflicts
- Check if you have proper permissions
- Make sure you're using the correct Python version
Virtual Environment Installation
For a clean installation, use a virtual environment:
# Create virtual environment
python -m venv myenv
# Activate virtual environment
# Windows
myenv\Scripts\activate
# Linux/macOS
source myenv/bin/activate
# Install psutil
pip install psutil
Conclusion
Resolving the ModuleNotFoundError: No module named 'psutil' is straightforward with the following steps:
- Install psutil using pip or your system's package manager
- Verify the installation in Python's interactive shell
- Use a virtual environment if needed
- Check for any system-specific requirements
Remember to use the appropriate installation command for your operating system and Python version. If you continue to experience issues, ensure you have the necessary permissions and dependencies installed on your system.