The warning is often shown when pip installing a package with sudo or in a Docker container that doesn’t have a user added.
Virtual Environments — Why and When to Use Them
I strongly recommend using a virtual environment for each Python project. A virtual environment is an isolated environment: it has its own Python interpreter, its own pip, and its own site-packages directory.
Benefits:
- Dependency isolation: Different projects may require different versions of the same library. A virtual environment ensures they don’t interfere with each other.
- Clean system: Your global Python installation remains uncluttered by project-specific packages.
- Easy dependency management: With
pip freeze, you can always generate a snapshot of all packages your project depends on — ideal for sharing or deploying.
Typical workflow with virtual environments:
# 1. Create the virtual environment (replace “myenv” with your chosen name)
python3 -m venv myenv
# 2. Activate it:
# On Linux/Mac:
source myenv/bin/activate
# On Windows:
myenv\Scripts\activate
# 3. Install packages as needed with pip
pip install <package_name>
# 4. After you’ve installed everything, generate requirements.txt
pip freeze > requirements.txt
# 5. Later (or on another machine), recreate the same environment:
pip install -r requirements.txt
It’s also good practice not to include the virtual environment folder itself in version control (e.g. by adding it to .gitignore), only the requirements.txt
When requirements.txt Isn’t Enough — Some Caveats & Best Practices
While pip freeze > requirements.txt is simple and works well for many scenarios, there are some caveats:
- It captures all packages installed in the environment — including indirect dependencies that you may not explicitly use. This can make the dependency list bloated.
- If you rely on non-standard installation options (e.g. custom indices, Git dependencies, flags),
pip freezedoes not preserve them. This can lead to issues when someone else tries to reproduce your environment. - For more robust dependency management — especially in larger or collaborative projects — it’s often better to use a more advanced tool (like poetry, or pip-tools) that supports locking and capturing install options.
Conclusion
pip — the standard package manager for Python — offers all the tools you need to install, upgrade, uninstall, and list packages. When combined with virtual environments, it gives you a powerful and clean way to manage dependencies per project.
For simple projects or quick scripts, it might be enough to use basic commands and requirements.txt. But for bigger applications — especially if shared among multiple developers — invest a bit of time in proper dependency management to avoid headaches later.