paint-brush
Learn Python Poetry and Start Using It Nowby@pratikpathak
219 reads

Learn Python Poetry and Start Using It Now

by Pratik PathakFebruary 13th, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

We will learn about Python Poetry Examples. This is called package dependency issues. Though requirements.txt is used to solve dependencies in some ways, still, it’s not perfect. Sometimes, packages written in requirements.txt become obsolete which is why when you try to install them it throws an error.
featured image - Learn Python Poetry and Start Using It Now
Pratik Pathak HackerNoon profile picture

Time to Ditch Requirements.txt now! Start Using Poetry. Python Poetry Example

Have you ever run into an issue where you wrote a program and created a “requirements.txt” file and wrote down all the required packages in the requirements.txt file, but when you tried to move the project from one system to another, your program did not work anymore? Even after installing all the packages from requirements.txt still, it’s not working


We will learn about Python Poetry Examples.


This is called package dependency issues. Though requirements.txt is used to solve dependencies in some ways, still, it’s not perfect. Sometimes, packages written in requirements.txt become obsolete which is why when you try to install them it throws an error.


So how to solve package dependency issues? To solve such type of issue, Poetry came into the picture.


Table of Contents

  • What is Poetry?
  • How to Install Poetry Python?
    • Install Poetry using PIP
    • Install Poetry on Windows
    • Install Poetry on Linux
  • Let’s make our life easier with Poetry Python
  • Create a Python project by using Poetry Python
    • What is pyproject.toml in Poetry?
      • [tool.poetry] table in poetry pyproject.toml
      • [tool.poetry.dependencies] section in poetry pyproject.toml
      • [build-system] section in poetry prproject.toml
  • Handling Virtual Environment Using Poetry
    • How to activate Virtual Environment using Poetry
    • How to Delete Virtual Environment Using Poetry
  • Let’s start building the Project
  • Creating the Driver Code main.py
    • Install Packages using Poetry Add Command
    • Install Dev Package using Poetry Add Command
    • Remove Package using Poetry Remove Command
    • How to format code using a Black Python Formatter?
  • What are the poetry lock files?
  • Add Poetry to existing Project.
    • Create pyproject.toml using the existing Requirements.txt file
    • Create Requirements.txt using existing poetry.lock file
      • Poetry command to create Requirements.txt file
  • Download Premium Poetry Cheatsheet for FREE👇
  • Conclusion

What Is Poetry?

Poetry is a package dependencies management tool for Python that easily solves all these issues. It’s a bundle with all things ready to go. You don’t need to worry about the virtual environment, tracking down packages, separating dev and required packages, etc. It’s all taken care of by Poetry.


Today, most Python developers have started using Poetry. It has become a basic necessity of Python projects. You will soon learn why it’s so important; keep following the tutorial.

How to Install Poetry Python?

Install Poetry using PIP

This is the simplest and easiest method to install poetry, and this method works for Windows, Linux, and MacOS. Make sure you already have pip installed.

pip install poetry

Install Poetry on Windows

This method only works for the Windows operating system. Just open Powershell (Not CMD) and type this command.

(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py -


If the above command doesn’t work. then only try this.

(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | python -

Install Poetry on Linux

Just run the below command in the terminal, and it will automatically install the poetry.

curl -sSL https://install.python-poetry.org | python3 -


Now, confirm the installation of poetry by running the command poetry --version


Also Read:

Docker Commands with example

Let’s Make Our Life Easier With Poetry Python

Now, that we have successfully installed poetry in our system, you can verify the installation of poetry by running the command poetry --version . In daily programmers’ lives, we just create a Python file and directly start writing the Python program in it. Though it’s not wrong, it’s not the best way to initialize a Python project.


You should at least create a folder to save your .py files, a readme.md file and lastly requirements.txt file.


Let’s understand all the Poetry commands by creating a small project that utilizes all the poetry commands. The name of our project will be “wifiPwd.” Here, I’m going to create a small Python program that will show the password of saved wifi networks in your system.


Full project GitHub Link: Github

Create a Python Project By Using Poetry Python

Poetry can create a Boilerplate template for your project; you just need to use poetry new the command to create a new project. By using this command, poetry will create all the necessary files required in a project.

poetry new wifiPwd


This command will create a folder named wifiPwd, and inside the folder, you will see a list of files; we will go through each of them individually. The folder structure looks like this:

D:\PROJECTS\WIFIPWD
│   pyproject.toml
│   README.md
│
├───tests
│       __init__.py
│
└───wifipwd
        __init__.py

Tip! You can use the “Tree” command to view the folder structure


README.md file is an instruction file that contains documentation of Installation, usage, required packages, etc. tests.init.py contains some tests for your project; you can use “pytest” to add test cases to your project.


wifipwd.init.py is the heart of your project; inside wifipwd, you can add all your Python files or codes; the main code lies here**.**


.pyproject.toml is a file that manages the dependencies of your project.


The first file we see is “pyproject.toml” file. Let’s inspect the “pyproject.toml” file

What Is pyproject.toml in Poetry?

Pyproject.toml is a configuration file made in toml. You can learn more about toml from here. It’s a configuration file better than .ini, .cfg, and .json, and it’s easily readable for humans. pyproject.toml file is used to track down all Python dependencies. The content of pyproject.toml file looks like this.

[tool.poetry]
name = "wifipwd"
version = "0.1.0"
description = ""
authors = ["Pratik Pathak <[email protected]>"]
readme = "README.md"

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

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

You can see the file is divided into 3 sections encapsulated in []; these sections are known as tables. Let’s go through each section one by one.

[tool.poetry] table in poetry pyproject.toml

tool.poetry table tells us about the project. It gives a general idea of the project.

  • name – name of the project
  • version – version of your project
  • description – quick info about the project
  • authors – name of authors who contributed
  • readme – link to the readme.md file

[tool.poetry.dependencies] section in poetry pyproject.toml

This is the most important section pyproject.toml. This section tracks down all the dependencies required by the project. In layman’s terms, this is the replacement of requirements.txt . We will keep an eye on this file and mostly on this section while going through the tutorial.


We will observe this section whenever we add or remove the packages. Currently, you can see only one dependency python = “^3.9” which means the Python version starts from 3.9

[build-system] section in poetry prproject.toml

This section defines the build tools. This section is used to build the packages. It is utilized by the poetry build command.

Handling Virtual Environment Using Poetry

The next step is to create a virtual environment for our project. There are multiple ways to create a virtual environment; the most popular ones are “virtualenv” and “venv.” You don’t need to create a Python virtual environment by yourself.


Why do we need a virtual environment in Python?


This is to manage multiple versions of packages and isolate the project. For a particular type of project, we needed a particular version specific to the package which conflicts with existing updated packages. To solve this, it’s best to create a Isolated Virtual Environment, and install the required packages in it.


To create a virtual environment using poetry, use the command:

poetry use env python


This command will create a virtual environment for you to use and automatically activate it. When you run use env the command, you will see something like this

Using virtualenv:
C:\Users\pratik\AppData\Local\pypoetry\Cache\virtualenvs\wifipwd-s1RAI5FV-py3.9


Pro Tip!: In place of “Python,” you can give the path of the Python interpreter. By using this tip, you can create a virtual environment using different versions of Python. Example.

poetry use env C:\Users\pratik\AppData\Local\Programs\Python\Python39\python.exe

You can use poetry env list command to list down all the virtual env; you can create multiple that use different Python versions.

How to Activate Virtual Environment Using Poetry

You can easily activate Virtual Environment using the command

poetry shell


Pro Tip!: If you have not created the virtual environment, the “poetry shell” command will create one for you and activate it.

How to Delete Virtual Environment Using Poetry

To delete the virtual environment, use the command

poetry env remove envName

If you don’t know the name of the env, you can run poetry env list command to get the env name.


Also Read:

Docker Run Command example the ultimate cheatsheet

Let’s Start Building the Project

Creating the Driver Code main.py

Let’s create a Python file named “main.py” in the root folder. The project entry point will be from here. Before writing the code, let’s install all required packages beforehand.


We will use “PyInquirer” to create an interactive terminal. Let’s install PyInquirer using poetry add command.

Install Packages Using Poetry Add Command

poetry add pyinquirer

This command will fetch the PyPi registry. Download the packages from here, and install them in your system. This will also update the pyproject.toml file; it will add Pyinquirer into dependencies.

Now, we need a tool for formatting Python code.


We will use the “Black” package for formatting our code. But this is not mandatory for the project, it’s a dev dependency, We want to format the code for us easily, it has no purpose in production which’s why it’s a dev dependency

Install Dev Package Using Poetry Add Command

poetry add black --group dev


This will install “black” on your virtual environment, and poetry will add “black” as a dev dependency in pyproject.toml

Remove Package Using Poetry Remove Command

poetry remove packageName


This will remove the package from the environment as well as from pyproject.toml


First thing first, Step 0 of our program will be to run the command.

poetry install

This will validate the pyproject.toml file, and then it will install all the dependencies in the file.


After running “poetry install,” you will see output something like this.

Creating virtualenv wifipwd-s1RAI5FV-py3.9 in C:\Users\pratik\AppData\Local\pypoetry\Cache\virtualenvs
Resolving dependencies... (0.1s)

Installing the current project: wifipwd (0.1.0)


Did you notice? It says “Installing the current project: wifipwd (0.1.0)”, what does it mean? It means Poetry is treating the whole project as a package. Now, you can use import wifipwd it to import the project functionality directly.


The Final pyproject.toml will look something like this –

[tool.poetry]
name = "wifipwd"
version = "0.1.0"
description = ""
authors = ["Pratik Pathak <[email protected]>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.9"
pyinquirer = "^1.0.3"

[tool.poetry.group.dev.dependencies]
black = "^23.12.1"

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

Now, let’s go back to main.py file…


Here, we have to take input from the user in our example wifi name, and then, we will just show the output.


The main.py file will look something like this

import subprocess
from PyInquirer import prompt
from wifipwd import wifiPassword

data = (
    subprocess.check_output(["netsh", "wlan", "show", "profiles"])
    .decode("utf-8")
    .split("\n")
)
profiles = [i.split(":")[1][1:-1] for i in data if "All User Profile" in i]

question = [
    {
        "type": "list",
        "name": "wifi",
        "message": "Choose the wifi whose password you want to see",
        "choices": profiles,
    }
]
answer = prompt(question)
wifiName = answer["wifi"]
results = wifiPassword(wifiName)

try:
    print("{:<30}|  \033[92m{:<}\033[00m".format(wifiName, results))
except IndexError:
    print("{:<30}|  {:<}".format(wifiName, ""))

Did you notice? We used “from wifipwd import wifiPassword.” We are treating the whole project like a package, so let’s write the “wifiPassword” function. wifiPassword function will be located at “wifipwd > init.py.”


Now, we will add wifiPassword() function. Let’s edit “wifipwd > init.py.” The final “init.py” will look like

import subprocess

def wifiPassword(name):
    try:
        results = (
            subprocess.check_output(
                ["netsh", "wlan", "show", "profile", name, "key=clear"]
            )
            .decode("utf-8")
            .split("\n")
        )
    except subprocess.CalledProcessError:
        print("{:<30}|  {:<}".format(name, "ENCODING ERROR"))

    results = [b.split(":")[1][1:-1] for b in results if "Key Content" in b]
    try:
        return results[0]
    except IndexError:
        return ""

The Project is almost completed. The last step will be to format the code. We have already installed the `Black` Python package for code formatting. Let’s format our code using black.

How to Format Code Using a Black Python Formatter?

It’s very easy to format our code using “Black;” just run the below command.

black main.py


Pro Tip! You can also format the whole folder by providing the folder name for example.

black wifipwd


You will output something like this

All done! ✨ 🍰 ✨
1 file reformatted.


Note: If you get any error, make sure you are inside the virtual environment; use “poetry shell” to activate the virtual environment. If it still not working, update the black by using “poetry add black@latest.”


Now, to make sure everything is okay and running, run “poetry install” one more time. After making sure everything is working smoothly let’s lock the dependencies using poetry.

What Are the Poetry Lock Files?

Lock files are like a freezing point; they lock the packages and make sure every developer has exactly the same packages this ensures smooth synchronization between developers.


Poetry automatically creates a lock file as soon as you run poetry install; you can also use the below command to create the “poetry.lock” file.

poetry lock

Whenever we run the command poetry install, poetry first reads the “poetry.lock” file, and then it reads the “pyproject.toml” file.

Add Poetry to Existing Project.

You may have some other project that does not use poetry beforehand, and it is dependent on the requirements.txt file. You can add poetry to your project by running the command

poetry init


It will open an interactive terminal. Just fill out all the details, and you are done.

>>poetry init                                                                                                                                  

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

Package name [wifipwd]: 
Version [0.1.0]:  
Description []:  
Author [Pratik Pathak <[email protected]>, n to skip]:  
License []:  
Compatible Python versions [^3.12]:  3.9

Would you like to define your main dependencies interactively? (yes/no) [yes] no
Would you like to define your development dependencies interactively? (yes/no) [yes] no
Generated file

This will create a pyproject.toml file for you, and you are ready to go 🏃‍♂️.

Create pyproject.toml Using the Existing Requirements.txt file

If you have an existing requirements.txt file and want to move to poetry, you can easily do it by using the command

poetry add `cat requirement.txt`


Tip! If you want to add packages as dev dependencies, use the command.

poetry add `cat requirement.txt` --group dev

Create Requirements.txt Using Existing poetry.lock File

You may be wondering why we need the requirements.txt file when we already have poetry.lock/pyproject.toml file. The answer is simple: to make it compatible.


When you try to deploy your project online, the platform provider uses the “requirements.txt” file.

Poetry command to create Requirements.txt file

poetry export --output requirements.txt

Note: Make sure you have already installed the “poetry-plugin” package. Run the command “pip install poetry-plugin” to install the package


This command will create the requirements.txt file using poetry.lock file. But wait, this is not a normal requirements.txt file. Let’s inspect the poetry-generated requirements.txt file.


When you look closely, you will observe that the poetry requirements.txt file contains two more fields, first “python_version” and second some hash value. These values ensure that the same package should get installed when we run pip install -r requirements.txt


If you like our project, don’t forget to give a star ⭐ to the repo.


Full project GitHub Link: GitHub

Conclusion

We learned about Poetry using a Python poetry example. We have gone through all the poetry commands one by one. Also, we have seen how to create requirements.txt using poetry. Poetry can be used to manage the dependencies; it’s a stop solution for everything. It also manages a virtual environment. With Poetry, you don’t need any other tool; you no longer have to maintain requirements.txt manually.