In this post, I'm going to provide a TL;DR to Poetry for Python package and virtual environment management.

Pip and Virtual Environments

If you've worked with Python for network automation, chances are that you've used Pip and virtual environments.

If not, and you are new to Python:

Pip is a package management system for installing and managing python packages.
Virtual environments provide isolated Python environments that allow you to manage dependencies for specific projects.

Using Pip and virtual environments for package management and project isolation has been the standard way of doing things for a few years now. The typical workflow being:

# create virtual environment
$ python -m venv .venv

# activate/enter virtual environment
$ source .venv/bin/activate

# install package
$ pip install requests pynetbox

# save dependencies
$ cd my_project 
$ pip freeze > requirements.txt

# see installed packages (including dependencies)
$ cat requirements.txt                                                       
certifi==2022.12.7
charset-normalizer==3.0.1
idna==3.4
pynetbox==7.0.0
requests==2.28.2
urllib3==1.26.14

This is all well and good, but there are a few issues. Such as:

  • To activate your virtual environment, you need to know where it is.
  • You need one tool for package management and one tool for creating your virtual environments.
  • Pip does not provide "easy" visibility into the dependency tree. I.e. what dependencies are installed for which packages? Which is helpful to know for troubleshooting dependency conflicts. Sure, you could use pipdeptree, but this adds yet another tool.

Poetry

Poetry overcomes these caveats by providing a single tool that performs package and virtual environment management in a modern and easy-to-use way.

Here's a quick overview:

# install poetry
https://python-poetry.org/docs/#installation

# initialize poetry for your project
$ cd my_project
$ poetry init 

# install packages (equivalent to pip install ...) 
poetry add requests pynetbox

# virtual environment is automatically created and activated
$ poetry env list                                        
my-project-5eOS5hU3-py3.8 (Activated)

# show dependency tree
$ poetry show --tree                                                          
pynetbox 7.0.0 NetBox API client library
└── requests >=2.20.0,<3.0
    β”œβ”€β”€ certifi >=2017.4.17
    β”œβ”€β”€ charset-normalizer >=2,<4
    β”œβ”€β”€ idna >=2.5,<4
    └── urllib3 >=1.21.1,<1.27
requests 2.28.2 Python HTTP for Humans.
β”œβ”€β”€ certifi >=2017.4.17
β”œβ”€β”€ charset-normalizer >=2,<4
β”œβ”€β”€ idna >=2.5,<4
└── urllib3 >=1.21.1,<1.27

# to manually enter your virtual environment 
poetry shell

Great, right? Poetry provides a ton of other features, such as dependency groups, and the ability to build and publish packages.

That wraps up today's post. Poetry has certainly become my go-to tool when developing with Python, and I hope you find it just as useful.

Ready to Master Network Automation? Start Your Journey Today!
Our membership provides:
  • Full deep-dive course library (inc. Batfish, pyATS, Netmiko)
  • Code repositories inc. full course code, scripts and examples
  • 24x7 multi-vendor labs (Arista, Cisco, Juniper)
  • Private online community
  • Live monthly tech sessions
  • Access to tech session library

Join Now ➜