Master Python Dependency Management: Pip, Pipdeptree, Pip‑autoremove & Conda
This guide explains how to reliably manage Python project dependencies and environments using pip tools (pip list, pip freeze, pipdeptree, pip‑autoremove) and Conda (installation, virtual environments, environment export/import, IDE integration), helping you avoid common pitfalls as projects scale.
Many developers initially resist Python because its environment and dependency management feel chaotic compared to Node's npm, Java's Maven/Gradle, etc. While simple scripts can be assembled quickly, larger projects suffer from hidden dependencies, unclear environment setup, and difficult reproducibility.
Based on Pip
The basic goals of a dependency manager are to:
Quickly configure project dependencies and set up the development environment.
Clearly show which third‑party packages a project uses and their dependency trees.
Allow easy addition, removal, and resolution of dependencies.
These goals are achievable with the Pip toolchain.
Quick environment configuration (pip)
List installed packages with pip list:
$ pip list
Package Version
---------- -------------------
certifi 2020.6.20
pip 19.3.1
setuptools 44.0.0.post20200106
wheel 0.36.2Install a package (e.g., Flask) and see the updated list:
$ 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.2Record the exact set of dependencies with pip freeze and save to requirements.txt:
$ pip freeze > requirements.txt
$ cat requirements.txt
certifi==2020.6.20
click==7.1.2
Flask==1.1.2
itsdangerous==1.1.0
Jinja2==2.11.3
MarkupSafe==1.1.1
Werkzeug==1.0.1Re‑install the same environment elsewhere using pip install -r requirements.txt.
Explicit dependency tree (pipdeptree)
While pip list and pip freeze show what is installed, they do not reveal which packages depend on which. Install pipdeptree to visualize the tree:
$ 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]
setuptools==44.0.0.post20200106
wheel==0.36.2This makes it clear that Jinja2 is required by Flask, preventing accidental removal.
Dependency cleanup (pip‑autoremove)
Uninstalling a package with pip uninstall flask -y removes Flask but leaves its dependencies behind. Use pip‑autoremove to clean them up:
$ pip install pip-autoremove
$ pip-autoremove flask -y
$ pipdeptree
certifi==2020.6.20
pip-autoremove==0.9.1
pipdeptree==2.0.0
pip==19.3.1
setuptools==44.0.0.post20200106
wheel==0.36.2The environment is now tidy.
Based on Conda
While Pip works well for single‑project environments, Python’s global interpreter makes it hard to isolate multiple projects. Conda provides a language‑agnostic virtual‑environment system that can manage Python, R, Java, Node, Ruby, and even system tools.
Package, dependency and environment management for any language—Python, R, Ruby, Lua, Scala, Java, JavaScript, C/C++, FORTRAN Conda is an open‑source package and environment manager that runs on Windows, macOS, and Linux. It quickly installs, runs, and updates packages and their dependencies, and easily creates, saves, loads, and switches between environments.
Installation
Choose Miniconda (lightweight) over Anaconda (full scientific stack). Download the installer, run it, and follow the prompts. After installation, the conda command becomes available (you may need to source ~/.bashrc or disable auto‑activation with conda config --set auto_activate_base false).
Environment operations
Create and activate a clean Python 2.7 environment named frida:
$ conda create -n frida python=2.7 -y
$ conda activate fridaList environments and packages:
(frida) $ conda env list
# conda environments:
base /home/user/miniconda3
frida * /home/user/miniconda3/envs/frida
(frida) $ conda listNote that conda list shows both Python packages and non‑Python dependencies, unlike pip list.
Deactivate the environment with conda deactivate (may need to run twice if nested).
Dependency management
Export the current environment to a YAML file and recreate it elsewhere:
(frida) $ conda env export > environment.yaml
(frida) $ conda env create -f environment.yamlIDE integration
Conda environments can be directly linked to IDEs, simplifying configuration and debugging.
Some reflections
Is Conda convenient for virtual environments of other languages? Yes – you can create isolated environments for Java, Node, Ruby, etc., and install language‑specific packages (e.g., conda install openjdk=8.0.152 -y).
How to find packages supported by Conda? Use conda search package_name or browse anaconda.org .
Should Python packages be installed with Conda or Pip? For pure Python libraries, Pip is preferred for simplicity and uniformity. For cross‑language stacks or pre‑built binaries (e.g., TensorFlow), Conda can be advantageous.
References
Anaconda vs Miniconda
Official Conda documentation
pip‑deptree
pip‑autoremove
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
