Introduction
Dependency management in Python is an important component of software development because it entails managing the libraries and packages required by a project. Proper dependency management ensures that the necessary dependencies are installed, conflicts are avoided, and the project is reproducible across different environments.
Concepts
Dependencies
Dependencies are external libraries or modules that a project relies on to function. These can range from standard libraries included with Python to third-party packages available on repositories like PyPI (Python Package Index).-
Dependency Management Tools
Include tools such as:- pip: The default package installer for Python.Uses requirements.txt to list dependencies and their versions.
- virtualenv: Creates isolated Python environments to manage dependencies for different projects independently.
- venv: Provides similar functionality to virtualenv but is built into Python.
- conda: A package manager and environment management system that supports multiple languages.
- poetry: A modern tool for dependency management and packaging.Uses pyproject.toml to specify project configurations and dependencies.
Poetry
Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. Poetry offers a lockfile to ensure repeatable installs, and can build your project for distribution.
Pyproject.toml file
pyproject.toml
is a configuration file used by packaging tools such as poetry.
example
[tool.poetry] name = "poetry-test" version = "0.2.0" description = "" authors = ["None"] readme = "README.md" [tool.poetry.dependencies] python = "^3.12" requests = "^2.30.0" [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api"
Installation
Poetry requires Python 3.8+
- Linux,macOS
curl -sSL https://install.python-poetry.org | python3 -
- Windows(Poweshell)
(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py -
- Add Poetry to your PATH(Environment Variables)
Add the following paths to PATH variable depending on your system.
$HOME/.local/bin
on Unix.
%APPDATA%\Python\Scripts
on Windows.
poetry --version
: verify that poetry is installed correctly and added to $PATH.
Usage
poetry new <package name>
: starts a new Python project by creating a new directory with a standard Python project structure.
new-package new-package __init__.py tests __init__.py pyproject.toml README.md
poetry init
: Initializes a directory by prompting you to provide details about your project and its dependencies interactively.
poetry config virtualenvs.in-project true
: create virtual environments inside the project's directory.Poetry by default will create a virtual environment under {cache-dir}/virtualenvs.
poetry shell
: activate the virtual environment created.
poetry add <django>
: adds a dependency to your project.This will add django to your pyproject.toml file and install django.
# Allow >=2.0.0, <3.0.0 versions i.e minor version poetry add requests^2.0.0 # Allow >=2.0.0, <2.1.0 versions i.e patch versions poetry add requests~2.0.0 # Allow only a specific version poetry add requests==2.0.0 | poetry add requests@2.0.0
poetry add --dev <pytest>
: add dependencies only needed in development.Pytest will be added as a development dependency.
poetry remove <package name>
: removes a package from the current list of installed packages.
poetry show
: list all the available packages.
poetry show <package name>
: list details about a specific package.
poetry version
: shows the current version of the project.
poetry version <patch|minor|major>
: bumps the version of the project and writes to pyproject.toml.
poetry list
: displays all the available Poetry commands.
poetry install
: reads the pyproject.toml file from the current project, resolves the dependencies, and installs them.
Commands
Importing existing requirements.txt file to poetry
pyproject file
Top comments (0)