Open In App

Using Poetry Dependency Management tool in Python

Improve
Improve
Like Article
Like
Save
Share
Report

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:

Poetry Dependency Management tool in Python

 

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:

Poetry Dependency Management tool in Python

 

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:

Poetry Dependency Management tool in Python

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:

Poetry Dependency Management tool in Python

notifier.py

 



Last Updated : 01 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads