Using Poetry Dependency Management tool in Python
Last Updated :
01 May, 2022
In this article, we are going to study the poetry dependency management tool in python which will help you to manage the libraries of your next project so it will be easy to download, install, and set up your project.
What is Poetry
Poetry is a python dependency management tool to manage dependencies, packages, and libraries in your python project. It will help you to make your project more simple by resolving dependency complexities in your project and managing to install and update for you. To solve messy situations, Poetry comes here for us with only one pyproject.toml file to manage all the dependencies. In other words, poetry uses pyproject.toml to replace setup.py, requirements.txt, setup.cfg, MANIFEST.in, and Pipfile.
Features of Poetry
- Dependency Management Tool.
- It makes the project more simple by resolving dependency complexities
- Poetry will help us to structurize our project
- Commands to manage, setup, run and deploy a project
Advantages of Poetry
- Admin has total control over dependencies (locked dependency file, no auto-updates)
- Keeping the dependency version compatible with the project.
- Easy to install and set up a new virtual environment.
- Simple file structure.
- Easy to add a new dependency to a project.
- Easy to access and understandable project metadata from pyproject.toml
Poetry Installation:
On Linux/bash on windows:
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
On windows PowerShell
(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -
Using pip
To install poetry on your local system. You can also install it using pip which is not recommended method
pip install poetry
To verify if poetry is installed or not:
poetry --version
Activating virtual environment
Poetry creates its own virtual environment at the time of installation so that it won't mess with other programs in your system. You can use it as a virtual environment which will be present in your project directory {cache-dir}/virtualenvs}. To activate the virtual environment in your project you simply need to type this command :
poetry shell
This will create a new shell environment if you don't want that you can simply activate it on your own terminal using the following command :
For Linux
source {path_to_venv}/bin/activate
For Windows
source {path_to_venv}\Scripts\activate.bat
To deactivate the virtual environment you can use a command.
exit
Starting a new Project :
To create a new project you can use the following command:
poetry new new-project
This will create a new project with the name new project you can name it as you want. This will create a new directory with the name new project and the directory structure inside the new project will be like this:
Â
The project structure is like this.
- pyproject.toml: It is the most important file that contains project metadata and other information like project dependencies and developer dependencies with their versions. As we add new dependencies to our project it gets added here. This file should not be edited explicitly.
- README.rst: It is a file that can be edited by the project admin and should contain information about what is this project and how to use it .etc.
- tests: folder contains unit tests related to different units/functions of the program.
- The project folder contains another folder with the same name as the parent directory which contains code files of the program.
Adding New Dependencies
For adding a new module to your project you can use the following command, i.e. If we want to add two new modules namely pygame and pyautogui to our project then we can simply use these commands to add them to our project.
$ poetry add pygame
$poetry add pyautogui
This will add them in your pyproject.toml with the latest version. After this your pyproject.toml should look like this:
 Installing Dependencies
Now, if you are using other projects which use poetry as a management system, you will need to run this command it will install all required dependencies. This should start the process of installation and install all of them with the specified version.
$ poetry install
Running Python scripts
We have created a simple main.py file inside the project, now run your python scripts using the following command below:
$ poetry run python new_project/main.py
Example 1:
Create a new file with main.py.
Python3
# where main.py contains
print("Hello World")
 Output:
main.py
Example 2:Â
In this program we are going to use various dependencies which are time and plyer to add those dependencies to our project simply use the following commands:
$ poetry add plyer datetime
Create a new file with notifier.py.
Python3
from plyer import notification
import time
def inputtime():
hour = int(input("hours of interval :"))
minutes = int(input("Mins of interval :"))
seconds = int(input("Secs of interval :"))
time_interval = seconds+(minutes*60)+(hour*3600)
return time_interval
def notify():
notification.notify(
title = "Time is Up",
message="Enter description here" ,
timeout=5
)
if __name__ == '__main__':
time_interval = inputtime()
time.sleep(time_interval)
notify()
Run Program
$ poetry run python notifier/notifier.py
Output:
notifier.py
Â
Similar Reads
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced
Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions
Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Enumerate() in Python
enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Data Types
Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Python Introduction
Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Lists
In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s
6 min read