Fundamentals 13 min read

Managing Python Project Dependencies with pip and Conda

This article explains how to manage Python project dependencies using pip and conda, covering basic pip commands, dependency trees with pipdeptree, clean removal with pip‑autoremove, and creating, activating, and exporting isolated environments with Conda, including practical examples and best‑practice recommendations.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Managing Python Project Dependencies with pip and Conda

Many developers initially resist using Python because its environment and dependency management feel chaotic compared to Node.js (npm), Java (Maven/Gradle), or other languages that provide standardized project files. However, with the right toolchain, Python can also support clean, reproducible projects.

The basic goals of dependency management are to quickly configure project dependencies, clearly see which third‑party packages are required and their dependency trees, and easily add or remove packages.

Quick configuration (pip)

To preview installed packages in the current environment, 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>

After installing a package such as Flask with pip install flask , the list expands to include its indirect dependencies.

<code>$ 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 later reinstall them using pip install -r requirements.txt .

Explicit project dependencies (pipdeptree)

While pip list or pip freeze show which packages are present, they do not reveal the dependency graph. Install pipdeptree to visualize it:

<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]
setuptools==44.0.0.post20200106
wheel==0.36.2</code>

This makes it clear that Jinja2 is a transitive dependency of Flask, preventing accidental removal.

Project dependency governance (pip‑autoremove)

Uninstalling a top‑level package with pip uninstall flask -y leaves its dependencies behind. Use pip‑autoremove to clean them up:

<code>$ pip install flask
$ pip install pip‑autoremove
$ pip‑autoremove flask -y
$ pipdeptree
certifi==2020.6.20
pip‑autoremove==0.9.1
pipdeptree==2.0.0
  - pip [required: >=6.0.0, installed: 19.3.1]
setuptools==44.0.0.post20200106
wheel==0.36.2</code>

The environment is now tidy.

Based on Conda

pip works well for single‑project environments, but Python’s global interpreter makes it hard to isolate multiple projects. Conda provides true virtual environments that can also manage packages for other languages.

Conda is an open‑source package and environment manager that runs on Windows, macOS, and Linux. It can install, run, and update packages for Python, R, Ruby, Lua, Scala, Java, JavaScript, C/C++, and FORTRAN.

Installation

Download Miniconda (the lightweight installer) and run the script. After installation, your .bashrc contains initialization code. Reload the shell with source ~/.bashrc to enable the (base) environment.

Environment operations

Create a clean Python 2.7 environment named frida and activate it:

<code>$ conda create -n frida python=2.7 -y
... 
$ conda activate frida</code>

List environments with conda env list and view installed packages with conda list . Unlike pip list , conda list also shows non‑Python dependencies.

Deactivate the environment (may require two conda deactivate calls if nested).

Dependency management

Export the current environment to a YAML file:

<code>(frida) $ conda env export > environment.yaml
... (contents of environment.yaml) ...</code>

Recreate the environment elsewhere with conda env create -f environment.yaml .

IDE integration

Conda environments can be directly selected in many IDEs, simplifying project setup.

Some reflections

Conda can host environments for other languages, e.g., creating a Java 8 environment with conda create -n java8 followed by conda install openjdk=8.0.152 -y . It can also include Node, Ruby, and common utilities like curl or wget, making it valuable for cross‑language projects.

When a package is pure Python, prefer pip install for simplicity; use conda install for cross‑language stacks or heavyweight distributions such as TensorFlow.

Pythondependency managementCondapipVirtual Environments
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.