How to solve "ModuleNotFoundError: No module named 'six'" in Python
The error ModuleNotFoundError: No module named 'six'
in Python means that the six
package is not installed in the Python environment you're using. The six
package provides utilities for writing code that's compatible with both Python 2 and Python 3. While Python 2 is no longer supported, some older libraries still depend on six
.
This guide shows how to fix this error by installing six
.
Understanding the Error
The ModuleNotFoundError
means that when your Python code executes import six
, the interpreter can not find a module named six
. This almost always means the six
package is not installed in the Python environment you're using.
Solution 1: Install six
with pip
(Most Common)
The standard way to install six
is using pip
, Python's package installer:
Basic Installation
Open your terminal (or command prompt on Windows) and run:
pip install six
Or, if you're sure you're using Python 3:
pip3 install six
Using python -m pip
(If pip
Isn't in Your PATH)
If the pip
command doesn't work, try:
python -m pip install six # Use with your system's default Python
python3 -m pip install six # Explicitly use Python 3
- Using python -m pip ensures that you're using the pip associated with your Python installation.
Windows-Specific Considerations (py
launcher)
On Windows, the py
launcher is often helpful:
py -m pip install six
Permissions Issues (sudo
or --user
)
On Linux/macOS, you might need sudo
for system-wide installation (but virtual environments are strongly preferred):
sudo pip3 install six # System-wide install - use with caution
- Using
sudo
installs the package globally.
Alternatively, install into your user's site-packages:
pip install six --user # Install for the current user only
- The
--user
installs packages in your user directory, which doesn't require administrator privileges.
Solution 2: Use a Virtual Environment (Highly Recommended)
Always use virtual environments for Python projects. They isolate project dependencies and avoid conflicts.
Creating a Virtual Environment
python3 -m venv venv # Create an environment named "venv"
# OR: python -m venv venv
# OR: py -m venv venv (Windows)
Activating the Environment
-
macOS / Linux:
source venv/bin/activate
-
Windows (Command Prompt):
venv\Scripts\activate.bat
-
Windows (PowerShell):
venv\Scripts\Activate.ps1
- If this doesn't work on Windows (Powershell), you might need to run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
- If this doesn't work on Windows (Powershell), you might need to run:
After activation, your command prompt shows the environment name (e.g., (venv)
). pip install
now installs into this environment only.
Installing six
Inside the Environment
With the virtual environment activated:
pip install six # Installs into the active environment
Troubleshooting
If you've installed six
but still get ModuleNotFoundError
:
-
IDE Configuration (VS Code, PyCharm): Your IDE may be using a different interpreter.
- VS Code:
Ctrl+Shift+P
(orCmd+Shift+P
on macOS), "Python: Select Interpreter", choose the correct one (your virtual environment). - PyCharm:
File > Settings > Project > Python Interpreter
(orPyCharm > Preferences > Project > Python Interpreter
on macOS).
- VS Code:
-
Conflicting Installations/Multiple Python Versions: If you have multiple Python versions without using virtual environments, you may have installed
six
for the wrong one. Use virtual environments to avoid this. -
Incorrect File/Variable Names: Never name your Python files
six.py
. This conflicts with the installed library. Similarly, don't create a variable namedsix
before importing the module. -
Restarting Your IDE/Kernel: Sometimes (especially in Jupyter), you need to restart the kernel or IDE after installation.
-
Reinstalling As a last resort, you may have to reinstall the package:
pip uninstall six
pip install six
Using six
(Illustrative)
Once installed, you can import and use six
:
import six
# Example (using six.moves for compatibility):
if six.PY2:
print("Running on Python 2")
else:
print("Running on Python 3")
print(six.__version__) # Check the installed version
Installing six
in Specific Environments
Anaconda
conda install -c conda-forge six
Jupyter Notebook
!pip install six
The exclamation mark executes shell commands in a notebook cell.
Conclusion
The ModuleNotFoundError: No module named 'six'
error is fixed by installing the six
package using pip
.
- The best practice is to use a virtual environment for each project to isolate dependencies.
- If you encounter issues, double-check your IDE's Python interpreter settings and ensure you haven't created conflicting filenames or variable names.