Leveraging Poetry for Efficient Virtual Environment Management

In the ever-evolving landscape of software development, managing dependencies and creating a controlled environment for project execution is crucial. Python developers often find themselves juggling with virtual environments to isolate project dependencies, ensuring consistency and reproducibility across different setups. While there are several tools available for this task, one standout option that combines simplicity with power is Poetry.

Understanding Poetry

Poetry is not just a literary form; it’s also a Python packaging and dependency management tool. It streamlines the process of defining and installing project dependencies, making the management of virtual environments a seamless experience. To embark on this journey, let’s explore how Poetry can be employed for managing a virtual environment, taking Ansible as a case study.

Setting the Stage

Firstly, create a directory for your project and navigate into it:

$ mkdir ansible-project
$ cd ansible-project

Next, initiate a Poetry project within the directory:

$ poetry init

This command will prompt you to provide details about your project, such as its name, version, and dependencies.


This command will guide you through creating your pyproject.toml config.

Package name [ansible-project]:  
Version [0.1.0]:  
Description []:  
Author [Luca Berton <[email protected]>, n to skip]:  
License []:  
Compatible Python versions [^3.12]:  

Would you like to define your main dependencies interactively? (yes/no) [yes] 
You can specify a package in the following forms:
  - A single name (requests): this will search for matches on PyPI
  - A name and a constraint (requests@^2.23.0)
  - A git url (git+https://github.com/python-poetry/poetry.git)
  - A git url with a revision (git+https://github.com/python-poetry/poetry.git#develop)
  - A file path (../my-package/my-package.whl)
  - A directory (../my-package/)
  - A url (https://example.com/packages/my-package-0.1.0.tar.gz)

Package to add or search for (leave blank to skip): 

Would you like to define your development dependencies interactively? (yes/no) [yes] 
Package to add or search for (leave blank to skip): 

Generated file

[tool.poetry]
name = "ansible-project"
version = "0.1.0"
description = ""
authors = ["Luca Berton <[email protected]>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.12"


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


Do you confirm generation? (yes/no) [yes]
Join 50+ hours of courses in our exclusive community

Adding Dependencies

With the project initialized, add Ansible to the list of dependencies using the poetry add command:

$ poetry add ansible

Poetry will handle the installation of Ansible and any additional dependencies required.

Creating virtualenv ansible-project-wMMHKn40-py3.12 in /Users/lberton/Library/Caches/pypoetry/virtualenvs
Using version ^8.6.1 for ansible

Updating dependencies
Resolving dependencies... Downloading https://files.pythonhosted.org/packages/ec/1a/610693ac4ee14fcdf2d9bf3c493370e4f2ef7ae2e19217d7a237ff42367d/packaging-23.2-py3-none-any.whl (Resolving dependencies... Downloading https://files.pythonhosted.org/packages/62/d5/5f610ebe421e85889f2e55e33b7f9a6795bd982198517d912eb1c76e1a53/pycparser-2.21-py2.py3-none-any.wResolving dependencies... Downloading https://files.pythonhosted.org/packages/62/d5/5f610ebe421e85889f2e55e33b7f9a6795bd982198517d912eb1c76e1a53/pycparser-2.21-py2.py3-none-any.wResolving dependencies... Downloading https://files.pythonhosted.org/packages/96/06/4beb652c0fe16834032e54f0956443d4cc797fe645527acee59e7deaa0a2/PyYAML-6.0.1-cp310-cp310-macosx_1Resolving dependencies... Downloading https://files.pythonhosted.org/packages/96/06/4beb652c0fe16834032e54f0956443d4cc797fe645527acee59e7deaa0a2/PyYAML-6.0.1-cp310-cp310-macosx_1Resolving dependencies... Downloading https://files.pythonhosted.org/packages/96/06/4beb652c0fe16834032e54f0956443d4cc797fe645527acee59e7deaa0a2/PyYAML-6.0.1-cp310-cp310-macosx_1Resolving dependencies... Downloading https://files.pythonhosted.org/packages/96/06/4beb652c0fe16834032e54f0956443d4cc797fe645527acee59e7deaa0a2/PyYAML-6.0.1-cp310-cp310-macosx_1Resolving dependencies... Downloading https://files.pythonhosted.org/packages/96/06/4beb652c0fe16834032e54f0956443d4cc797fe645527acee59e7deaa0a2/PyYAML-6.0.1-cp310-cp310-macosx_1Resolving dependencies... Downloading https://files.pythonhosted.org/packages/96/06/4beb652c0fe16834032e54f0956443d4cc797fe645527acee59e7deaa0a2/PyYAML-6.0.1-cp310-cp310-macosx_1Resolving dependencies... (2.5s)

Package operations: 10 installs, 0 updates, 0 removals

  • Installing pycparser (2.21)
  • Installing cffi (1.16.0)
  • Installing markupsafe (2.1.3)
  • Installing cryptography (41.0.5)
  • Installing jinja2 (3.1.2)
  • Installing packaging (23.2)
  • Installing pyyaml (6.0.1)
  • Installing resolvelib (1.0.1)
  • Installing ansible-core (2.15.6)
  • Installing ansible (8.6.1)

Writing lock file

Verifying the Installation

Once the installation is complete, you can verify the Ansible version within the virtual environment. Start the Poetry shell:

$ poetry shell

Inside the shell, check the Ansible version:

$ ansible --version

This will display information about the installed Ansible version, its configuration file location, module search path, and other relevant details.

Spawning shell within /Users/lberton/Library/Caches/pypoetry/virtualenvs/ansible-project-wMMHKn40-py3.12
lberton@Lucas-MacBook-Pro ansible-project % emulate bash -c '. /Users/lberton/Library/Caches/pypoetry/virtualenvs/ansible-project-wMMHKn40-py3.12/bin/activate'
(ansible-project-py3.12) lberton@Lucas-MacBook-Pro ansible-project % ansible --version
ansible [core 2.15.6]
  config file = None
  configured module search path = ['/Users/lberton/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/lberton/Library/Caches/pypoetry/virtualenvs/ansible-project-wMMHKn40-py3.12/lib/python3.12/site-packages/ansible
  ansible collection location = /Users/lberton/.ansible/collections:/usr/share/ansible/collections
  executable location = /Users/lberton/Library/Caches/pypoetry/virtualenvs/ansible-project-wMMHKn40-py3.12/bin/ansible
  python version = 3.12.0 (main, Oct  2 2023, 12:03:24) [Clang 15.0.0 (clang-1500.0.40.1)] (/Users/lberton/Library/Caches/pypoetry/virtualenvs/ansible-project-wMMHKn40-py3.12/bin/python)
  jinja version = 3.1.2
  libyaml = True

Poetry Shell Magic

By using poetry shell, you activate a virtual environment specific to your project, isolating its dependencies from the global Python environment. This ensures that your project’s dependencies do not interfere with other projects or the system-wide Python installation.

The output of the ansible --version command also provides insight into the Python version, module locations, and executable locations specific to the virtual environment created by Poetry.

Conclusion

Poetry not only simplifies dependency management but also enhances the overall development experience by providing a clean and isolated virtual environment for your projects. It eliminates the hassle of manual environment setup, making the process more efficient and developer-friendly.

As the Python ecosystem continues to evolve, tools like Poetry become invaluable for maintaining order in the ever-expanding landscape of dependencies. Embrace the poetic simplicity of Poetry in managing your virtual environments, and let the verses of dependency resolution and isolation harmonize with the symphony of your development workflow. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza