Managing Python packages the right way and not suffer with "Pip environment pollution".
👋 Hey, I'm Ajay Kumar Joshi – a Python & JavaScript enthusiast passionate about breaking down complex programming concepts into simple, digestible lessons.
I created PythonJS to help developers, students, and professionals learn Python and JavaScript the easy way—through byte-sized lessons, real-world examples, and a structured approach to coding.
🚀 Follow along as I simplify tough topics, share coding insights, and build a community of lifelong learners.
🔗 Explore more at pythonjs.org
Managing Python packages the right way
"Pip environment pollution/Python environment pollution" refers to the situation where multiple versions of the same package are installed in different environments, such as in different virtual environments or in the global environment, and this can cause conflicts and issues when running the code.
To avoid pip environment pollution, it is recommended to use a virtual environment for each project and install the required packages in the virtual environment. This will ensure that the packages are isolated from the global environment and other virtual environments, so there will be no conflicts or issues caused by having multiple versions of the same package installed.
To create a virtual environment and install packages in it using pip, you can use the following steps:
- Install the
virtualenvpackage using the following command:
pip install virtualenv
- Create a new virtual environment for your project using the following command:
virtualenv /path/to/my/env
- Activate the virtual environment using the following command:
source /path/to/my/env/bin/activate
- Install the required packages in the virtual environment using the following command:
pip install <package-name>
- When you are done working on the project and want to deactivate the virtual environment, use the following command:
deactivate
By using a virtual environment for each project, you can avoid pip environment pollution and ensure that your projects are isolated from each other and from the global environment. This can help prevent conflicts and issues caused by having multiple versions of the same package installed.
