Fixing a Dependency Error Caused by a Polluted Global Python Environment

Fixing a Dependency Error Caused by a Polluted Global Python Environment

Hello! Today we look at a surprisingly common mystery error caused by packages you don't remember installing, and what turns out to be behind it.

The environment in this case was Windows with Python and Anaconda installed, but the same essentially applies to Linux as well.

It turned out to be a polluted global environment

The problem appears

While setting up a Python project environment and installing the required packages, an error message suddenly appeared.

pip install opencv-python==4.8.1.78

The error shown after running the command

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. 
This behaviour is the source of the following dependency conflicts.
accelerate 0.19.0 requires packaging>=20.0, which is not installed.
accelerate 0.19.0 requires psutil, which is not installed.
accelerate 0.19.0 requires torch>=1.6.0, which is not installed.

The first question

"But I never installed accelerate..."

The packages being installed were for web application development, and the machine-learning package accelerate was not among them.

So where did accelerate come from?

Investigating the cause

First, check where accelerate is located:

pip show accelerate

Result

Name: accelerate
Version: 0.19.0
Location: c:\users\[username]\appdata\roaming\python\python310\site-packages
Required-by: 

Key points

  • The Location is not the virtual environment (c:\tools\anaconda3\envs\...)
  • It is installed in the user's global environment (appdata\roaming)
  • Required-by is empty = nothing depends on it

The real story

Check all packages in the user environment

pip list --user

The result: more than 20 packages had been installed globally, a mix of packages used by various different projects.

Cause: at some point, someone (probably me...) had installed packages without activating a virtual environment

Common failure patterns

Pattern 1: Forgetting to activate the virtual environment

# Wrong: forgot to activate the virtual environment
pip install some-package

# Correct: activate the virtual environment first, then install
conda activate myenv
pip install some-package

Pattern 2: Misusing the --user option

# Wrong: installs globally
pip install --user some-package

# Correct: install inside the virtual environment
pip install some-package

Pattern 3: Wrong environment selected in the IDE

If the wrong environment is selected in VSCode or PyCharm, packages get installed somewhere you did not intend

How to fix it

Option 1: Remove only the offending packages

pip uninstall -y accelerate

Run in PowerShell

pip list --user --format=freeze | ForEach-Object { $_.split('==')[0] } | ForEach-Object { pip uninstall -y $_ }

For Command Prompt

for /f "delims==" %i in ('pip list --user --format=freeze') do pip uninstall -y %i

Lessons learned and best practices

1. Always use a virtual environment

# Anaconda
conda create -n myproject python=3.10
conda activate myproject

# Or venv
python -m venv myenv
myenv\Scripts\activate  # Windows
source myenv/bin/activate  # Linux/Mac

2. Make checking your environment a habit

# Check the current environment
where python  # Windows
which python  # Linux/Mac

# Check where installed packages live
pip list -v

3. Manage dependencies with requirements.txt

# Pin the environment
pip freeze > requirements.txt

# Reproducible environment setup
pip install -r requirements.txt

4. Keep the user environment empty

# Check periodically
pip list --user
# Ideally this is empty, or contains only a minimal set of tools

Note: pip uninstall has no --user option

A point many people get wrong

  • pip install --user → installs into the user environment (this option exists)
  • pip uninstall --user → this option does not exist
  • pip uninstall → automatically searches both the user environment and the virtual environment and removes the package

Summary

This error was caused by a dependency conflict between packages in the virtual environment and packages in the global environment.

Key takeaways

  • The error message was actually a warning, and the installation itself had succeeded
  • Global environment pollution is a common problem in team development
  • Using virtual environments correctly prevents this kind of problem
  • Checking the global environment periodically and keeping it clean matters

This incident was a solid reminder of how important virtual environment management is in Python development.
If you run into a problem like this, a good first step is to check your environment with pip show and pip list --user.

Read more