Dependency Management in Python
Introduction
Poetry is a dependency management and packaging tool in Python, aiming to improve how you define, install, and manage project dependencies.
- Installation: You can install Poetry through its custom installer script or using package managers. The recommended way is to use their installer script to ensure you get the latest version.
- Creating a New Project: Use
poetry new <project-name>
to create a new project with a standard layout. - Adding Dependencies: Add new dependencies directly to your project using
poetry add <package>
. Poetry will resolve the dependencies and update thepyproject.toml
andpoetry.lock
files. - Installing Dependencies: Running
poetry install
in your project directory will install all dependencies defined in yourpyproject.toml
file.
Poetry Example
Setting Up a New Project
To create a new project named example_project
with Poetry and manage its dependencies:
|
|
- This command creates a new directory named
example_project
with some initial files, including apyproject.toml
file for configuration. - The
pyproject.toml
file is what is the most important here. This will orchestrate your project and its dependencies. For now, it looks like this:
|
|
- we are allowing any version of Python 3 that is greater than
3.7.0
. - If you want to use Poetry only for dependency management but not for packaging, you can use the non-package modei:
|
|
Add and Install Dependencies
Suppose your project depends on requests
for making HTTP requests and pytest
for testing. To add these:
|
|
- The
--dev
flag indicates thatpytest
is a development dependency, not required for production.
To remove a package,
|
|
Running the command below will install all dependencies listed in your pyproject.toml
:
|
|
- This also creates a virtual environment for your project if it doesn’t already exist.
Run Your Project
Run directly
To run a script or start your project within the Poetry-managed virtual environment:
|
|
This command ensures that python
and any other commands are run within the context of your project’s virtual environment, using the correct versions of Python and all dependencies.
Entry Point
In Poetry, an entry point is a way to specify which Python script should be executed when your package is run. This is particularly useful for creating command-line applications or defining executable scripts that can be run directly from the command line after your package is installed.
To define an entry point in a Poetry project, you use the [tool.poetry.scripts]
section in your pyproject.toml
file. This section allows you to map a command name to a Python function, which will be executed when the command is run.
Edit your pyproject.toml
file to include the [tool.poetry.scripts]
section. This is where you define your entry point. Add the following lines to the pyproject.toml
file:
|
|
- Here,
greet-app
is the command name, andgreet_app.cli:main
specifies themain
function in thecli.py
module inside thegreet_app
package.
Shell
Using Virtual Environment Shell: If you frequently run commands, you might find it convenient to activate the Poetry-managed virtual environment shell:
|
|
This will activate the virtual environment, and you can run your command without prefixing it with poetry run:
|
|
To exit this new shell type exit
. To deactivate the virtual environment without leaving the shell use deactivate
.
Init Method
Instead of creating a new project, Poetry can be used to ‘initialise’ a pre-populated directory. To interactively create a pyproject.toml
file in directory pre-existing-project
:
|
|
Then, install it by
|
|
To see the information about the project
|
|
-p
: prints a path of the virtual environment
By setting a config, you can generate a virtual environment in your projects:
|
|
Use with Git
Poetry automatically creates a .gitignore
file for you. It should include entries to ignore the virtual environment directory (.venv
if you use Poetry’s built-in virtual environment management).
Whenever you add or update dependencies, make sure to commit the pyproject.toml
and poetry.lock
files:
To clone your project:
|
|