Skip to content

Project Dependency Management

pip

Python pip Package Management

Basic pip Commands

bash
# Install packages
pip install requests
pip install numpy pandas matplotlib

# Install a specific version
pip install requests==2.28.1
pip install "requests>=2.25.0,<3.0.0"

# Install from requirements.txt
pip install -r requirements.txt

# Uninstall a package
pip uninstall requests

# Upgrade a package
pip install --upgrade requests
pip install -U requests

# List installed packages
pip list
pip freeze

# Show package information
pip show requests

# Search for a package
pip search requests  # Note: This feature has been disabled

# Check for outdated packages
pip list --outdated

# Generate requirements.txt
pip freeze > requirements.txt

requirements.txt File

txt
# requirements.txt example
# Web Frameworks
Flask==2.3.3
Django==4.2.4

# Data Processing
numpy==1.24.3
pandas==2.0.3
matplotlib==3.7.2

# Network Requests
requests==2.31.0
urllib3==2.0.4

# Database
SQLAlchemy==2.0.20
psycopg2-binary==2.9.7

# Testing Frameworks
pytest==7.4.0
pytest-cov==4.1.0

# Development Tools
black==23.7.0
flake8==6.0.0
mypy==1.5.1

# Optional Dependencies
# redis==4.6.0
# celery==5.3.1

pip Configuration File

ini
# pip.conf (Linux/macOS) or pip.ini (Windows)
[global]
index-url = https://pypi.org/simple/
trusted-host = pypi.org
timeout = 60
retries = 3

[install]
user = false
no-deps = false

[uninstall]
yes = false

venv and conda

Python Virtual Environments

venv Virtual Environment

bash
# Create a virtual environment
python -m venv myenv
python3 -m venv myenv

# Activate the virtual environment
# Windows
myenv\Scripts\activate

# macOS/Linux
source myenv/bin/activate

# Deactivate the virtual environment
deactivate

# Delete the virtual environment
rm -rf myenv  # Linux/macOS
rmdir /s myenv  # Windows

# Install packages in the virtual environment
pip install requests numpy

# Check virtual environment information
which python  # Linux/macOS
where python  # Windows

conda Environment Management

bash
# Create a conda environment
conda create -n myenv python=3.9
conda create -n myenv python=3.9 numpy pandas

# Activate the environment
conda activate myenv

# Deactivate the environment
conda deactivate

# List all environments
conda env list
conda info --envs

# Remove an environment
conda env remove -n myenv

# Export an environment
conda env export > environment.yml

# Create an environment from a file
conda env create -f environment.yml

# Install packages
conda install numpy pandas
conda install -c conda-forge jupyter

# List installed packages
conda list

environment.yml File

yaml
# environment.yml example
name: myproject
channels:
  - conda-forge
  - defaults
dependencies:
  - python=3.9
  - numpy=1.24.3
  - pandas=2.0.3
  - matplotlib=3.7.2
  - jupyter
  - pip
  - pip:
      - requests==2.31.0
      - flask==2.3.3

poetry

Python Poetry Dependency Management

Installation and Initialization

bash
# Install Poetry
curl -sSL https://install.python-poetry.org | python3 -

# Initialize a new project
poetry new myproject
cd myproject

# Initialize in an existing project
poetry init

# Add dependencies
poetry add requests
poetry add numpy pandas
poetry add --dev pytest black

# Install dependencies
poetry install

# Run scripts
poetry run python main.py
poetry run pytest

# Activate the virtual environment
poetry shell

pyproject.toml File

toml
# pyproject.toml example
[tool.poetry]
name = "myproject"
version = "0.1.0"
description = "My Python project"
authors = ["Your Name <your.email@example.com>"]
readme = "README.md"
packages = [{include = "myproject"}]

[tool.poetry.dependencies]
python = "^3.9"
requests = "^2.31.0"
numpy = "^1.24.3"
pandas = "^2.0.3"

[tool.poetry.group.dev.dependencies]
pytest = "^7.4.0"
black = "^23.7.0"
flake8 = "^6.0.0"
mypy = "^1.5.1"

[tool.poetry.group.test.dependencies]
pytest-cov = "^4.1.0"
pytest-mock = "^3.11.1"

[tool.poetry.scripts]
start = "myproject.main:main"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

npm

JavaScript npm Package Management

Basic npm Commands

bash
# Initialize a new project
npm init
npm init -y

# Install packages
npm install lodash
npm install express mongoose

# Install a specific version
npm install lodash@4.17.21

# Install from package.json
npm install

# Uninstall a package
npm uninstall lodash

# Upgrade a package
npm update lodash

# List installed packages
npm list
npm ls --depth=0

# Show package information
npm view lodash

# Search for a package
npm search lodash

# Check for outdated packages
npm outdated

# Run scripts
npm run start
npm test

package.json File

json
{
  "name": "myproject",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "express": "^4.18.2",
    "lodash": "^4.17.21",
    "mongoose": "^7.5.0"
  },
  "devDependencies": {
    "eslint": "^8.48.0",
    "jest": "^29.6.4",
    "nodemon": "^3.0.1",
    "prettier": "^3.0.3"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

.npmrc File

# .npmrc example
registry=https://registry.npmjs.org/
save-exact=true

yarn

JavaScript Yarn Package Management

Basic Yarn Commands

bash
# Initialize a new project
yarn init
yarn init -y

# Install packages
yarn add lodash
yarn add express mongoose

# Install dev dependencies
yarn add -D jest prettier

# Install from package.json
yarn install

# Uninstall a package
yarn remove lodash

# Upgrade a package
yarn upgrade lodash
yarn upgrade --latest

# List installed packages
yarn list --depth=0

# Show package information
yarn info lodash

# Run scripts
yarn start
yarn test

pnpm

JavaScript pnpm Package Management

Basic pnpm Commands

bash
# Initialize a new project
pnpm init

# Install packages
pnpm add lodash
pnpm add express mongoose

# Install dev dependencies
pnpm add -D jest prettier

# Install from package.json
pnpm install

# Uninstall a package
pnpm remove lodash

# Upgrade a package
pnpm up lodash

# List installed packages
pnpm list --depth 0

# Show package information
pnpm info lodash

# Run scripts
pnpm start
pnpm test

Comparison

FeaturePython (pip)Python (Poetry)JS (npm)JS (Yarn)JS (pnpm)
Config Filerequirements.txtpyproject.tomlpackage.jsonpackage.jsonpackage.json
Lock FileNo (pip freeze)poetry.lockpackage-lock.jsonyarn.lockpnpm-lock.yaml
Install Commandpip installpoetry installnpm installyarn installpnpm install
Add Dependencypip install pkgpoetry add pkgnpm install pkgyarn add pkgpnpm add pkg
Uninstallpip uninstall pkgpoetry remove pkgnpm uninstall pkgyarn remove pkgpnpm remove pkg
Virtual Envvenv/condaBuilt-inn/a (node_modules)n/a (node_modules)n/a (node_modules)

Summary

  • Python: Use pip with requirements.txt for simple projects. Use Poetry for modern dependency management and packaging.
  • JavaScript: npm is the default. Yarn and pnpm offer performance and feature improvements.

Exercises

  1. Create a requirements.txt and install packages.
  2. Initialize a project with Poetry and add dependencies.
  3. Initialize a project with npm and add express.

Next Steps

Now you know how to manage dependencies. Next, we'll learn about type systems.