Tools & Setup
Python Packaging and Dependency Management
Sharing your Python code with others, or even just managing your own projects, requires understanding packaging and dependencies. The standard way is to use pip, the package installer for Python. You install packages with pip install package_name. To manage dependencies for a project, you create a requirements.txt file. This file lists all the packages your project needs, often with version constraints. You can generate it with pip freeze > requirements.txt. Another user can then install all dependencies with pip install -r requirements.txt. For your own packages, you need a setup.py file. This file contains metadata about your package like name, version, and dependencies. You can then install your package in development mode with pip install -e . which creates a link so that changes to your code are immediately reflected. More modern tools like Poetry and pipenv offer improved dependency resolution and virtual environment management. They automatically create virtual environments and generate lock files to ensure deterministic builds. Understanding packaging is crucial when you want to share your code on PyPI (Python Package Index) or deploy applications. A simple exercise is to take one of your projects, structure it as a package, write a setup.py, and install it in a new virtual environment to verify that it works.
4,037
Views
212
Words
1 min read
Read Time
Apr 2025
Published