Managing Python Project Environments and Dependencies with pip and Conda
This article explains how to use pip and Conda to configure, list, freeze, and clean up Python project dependencies, manage virtual environments, and handle cross‑language packages, providing practical commands and tools for reliable environment and dependency management.
Many developers initially resist Python because its project environment management appears chaotic compared to Node.js (npm) or Java (Maven/Gradle). While Python often feels like a script language lacking standardized dependency files, proper tooling can make Python projects clean, reproducible, and maintainable.
Based on pip
The basic goals of dependency management are to quickly set up a project, know the full dependency tree, and add or remove packages easily. pip and its ecosystem provide these capabilities.
Quick environment configuration (pip)
To view installed packages, run pip list :
<code>$ pip list
Package Version
---------- -------------------
certifi 2020.6.20
pip 19.3.1
setuptools 44.0.0.post20200106
wheel 0.36.2
</code>Install additional packages, e.g., Flask:
<code>$ pip install flask
$ pip list
Package Version
---------- -------------------
certifi 2020.6.20
click 7.1.2
Flask 1.1.2
itsdangerous 1.1.0
Jinja2 2.11.3
MarkupSafe 1.1.1
pip 19.3.1
setuptools 44.0.0.post20200106
Werkzeug 1.0.1
wheel 0.36.2
</code>Record the exact versions with pip freeze > requirements.txt and reinstall later using pip install -r requirements.txt .
Explicit dependency tree (pipdeptree)
While pip list or pip freeze show packages, they do not reveal which package depends on which. The pipdeptree tool visualizes the dependency tree:
<code>$ pip install pipdeptree
$ pipdeptree
certifi==2020.6.20
Flask==1.1.2
- click [required: >=5.1, installed: 7.1.2]
- itsdangerous [required: >=0.24, installed: 1.1.0]
- Jinja2 [required: >=2.10.1, installed: 2.11.3]
- MarkupSafe [required: >=0.23, installed: 1.1.1]
- Werkzeug [required: >=0.15, installed: 1.0.1]
pipdeptree==2.0.0
- pip [required: >=6.0.0, installed: 19.3.1]
</code>Dependency cleanup (pip-autoremove)
Uninstalling a package with pip uninstall leaves its dependencies behind. pip-autoremove removes a package and any orphaned dependencies:
<code>$ pip install pip-autoremove
$ pip-autoremove flask -y
</code>The environment is now clean.
Based on Conda
pip works well for single‑project environments, but Python’s global site‑packages make it hard to isolate multiple projects. Conda provides true virtual environments that can also manage packages for other languages.
Installation
Choose Miniconda for a lightweight installer. After installing, initialize Conda in the shell (e.g., source ~/.bashrc ) and optionally disable auto‑activation of the base environment:
<code>conda config --set auto_activate_base false
</code>Environment operations
Create and activate a Python 2.7 environment named frida :
<code>$ conda create -n frida python=2.7 -y
$ conda activate frida
</code>List environments and packages:
<code>(frida) $ conda env list
# conda environments:
#
base /home/user/miniconda3
frida * /home/user/miniconda3/envs/frida
(frida) $ conda list
# packages in environment /home/user/miniconda3/envs/frida:
# Name Version Build Channel
_libgcc_mutex 0.1 main
ca-certificates 2021.4.13 h06a4308_1
...</code>Export the environment to a YAML file and recreate it elsewhere:
<code>(frida) $ conda env export > environment.yaml
$ conda env create -f environment.yaml
</code>IDE integration
Conda environments can be directly selected in IDEs, simplifying project setup.
Some Thoughts
Conda can also manage non‑Python languages, e.g., creating a Java 8 environment:
<code>$ conda create -n java8
$ conda activate java8
$ conda install openjdk=8.0.152 -y
</code>For pure Python packages, pip remains the preferred installer; for cross‑language or complex stacks (e.g., TensorFlow), Conda may be advantageous.
References
anaconda‑vs‑miniconda, Conda official docs, pip‑deptree, pip‑autoremove.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.