Why Default pip Matters
When you have multiple Python installations on your machine—such as the one provided by your operating system and another from Anaconda—running pip install package_name can lead to confusion. The system might use the OS-level pip instead of the Anaconda pip. This results in packages being installed in a location that is not accessible when you activate your conda environment. Consequently, scripts running within your conda environment will fail with ModuleNotFoundError because the dependencies were never actually added to the active environment.
By forcing the system to use Anaconda's pip, you guarantee that every package you install via pip lands in the correct place: inside your current conda environment. This simplifies dependency management and ensures reproducibility across different machines or CI/CD pipelines that rely on specific package versions managed by conda.
Verifying Your Current Setup
Before making changes, it is crucial to understand which pip is currently being executed. Open your terminal and run the following command:
which pip
On Linux or macOS, this will return the path to the executable. On Windows, use where pip. If the output points to a directory like /usr/bin/pip or C:\Python39\Scripts\pip.exe, then you are not using the Anaconda version. You should see a path containing anaconda3 or miniconda3 if Anaconda is already correctly positioned in your PATH. If it does not, proceed with the steps below to rectify the order.
Configuring PATH on Linux and macOS
The most robust way to set Anaconda's pip as the default is to modify your shell configuration file. For Bash users, this is typically ~/.bashrc; for Zsh users (common on newer macOS versions), it is ~/.zshrc.
- Open your terminal and edit the appropriate config file using a text editor like
nanoorvim. - Add the following line at the end of the file, replacing the path with your actual Anaconda installation directory if necessary:
export PATH="/home/your_username/anaconda3/bin:$PATH"
This command prepends the Anaconda bin directory to the existing PATH. Because the shell searches directories from left to right, the first pip it finds will be the one in the Anaconda folder. After saving the file, reload your configuration by running source ~/.bashrc (or source ~/.zshrc). Verify the change by running which pip again; it should now point to the Anaconda directory.
Configuring PATH on Windows
Windows handles environment variables differently. You need to adjust the user or system PATH variables through the GUI.
- Search for "Environment Variables" in the Start menu and select "Edit the system environment variables".
- Click on the "Environment Variables..." button at the bottom.
- In the "User variables" section (recommended) or "System variables", find the entry named
Pathand click "Edit". - Add a new entry for your Anaconda installation path, typically something like
C:\Users\YourName\Anaconda3\ScriptsorC:\Users\YourName\Anaconda3\Library\mingw-w64\bindepending on your setup. Ensure this entry appears above any other Python paths in the list. - Click OK to save all changes. Restart any open command prompts or PowerShell windows for the changes to take effect.
Alternative: Using Conda-Forge Pip Integration
If you prefer not to manipulate global PATH settings, you can rely on conda's built-in ability to manage pip packages. When you create a new environment, you can explicitly tell conda to use its own pip channel. However, for most users, simply ensuring the PATH is correct is the most straightforward solution. It avoids the complexity of managing separate package sources and reduces the likelihood of "environment pollution" where packages are scattered across multiple locations.
For those interested in how package installation interacts with integrated development environments, you might also want to check out Does Installing Flask Python Package within PyCharm Create Duplicate Entries on My Hard Drive? to understand how IDEs handle these paths.
Frequently Asked Questions
What happens if I don't fix the default pip?
If you do not fix the default pip, packages installed via pip install may go into your system Python directory rather than your active conda environment. This leads to errors when you try to import those libraries in your conda-based projects because they are physically located elsewhere.
Can I use conda to install pip packages instead?
Yes, you can use conda install --channel conda-forge package_name for many common libraries. However, not all packages are available on conda-forge, so knowing how to configure pip remains essential for accessing the full breadth of the Python ecosystem.
How do I switch back to the system pip if needed?
To temporarily use the system pip, you can specify the full path to the system pip executable directly in your command, such as /usr/bin/pip install package. Alternatively, you can remove the Anaconda path from your temporary session by unsetting the modified PATH variable, though this is rarely necessary for casual usage.
Does this affect Jupyter Notebooks?
Jupyter notebooks usually detect kernels based on the environment they were launched from. If you launch Jupyter from an activated conda environment, it will use the pip-installed packages from that environment regardless of the global default. However, ensuring the default pip is correct helps prevent confusion when debugging kernel issues locally.
Is it safe to delete the old pip?
No, you should not delete the system pip unless you are certain no other applications depend on it. Removing it can break system tools that rely on Python. Instead, just ensuring Anaconda's pip comes first in the PATH is sufficient and safer.