Have you ever been into an issue where you wrote a program you 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 system your program did not work anymore, even after installing all the packages from requirements.txt still it’s not working. We will learn Python Poetry Examples
This is called package dependencies 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.
![Python Poetry Examples](https://pratikpathak.com/wp-content/uploads/2024/01/Poetry-1.gif)
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:
Let’s make our life easier with Poetry Python
Now 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 but 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
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 in 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 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 <pratikpathak200@gmail.com>"]
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 section is 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, 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
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
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:
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 it 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.
Final pyproject.toml will look something like this –
[tool.poetry]
name = "wifipwd"
version = "0.1.0"
description = ""
authors = ["Pratik Pathak <pratikpathak200@gmail.com>"]
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, Lets 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 `Black` python package for code formatting, Let format our code using black.
How to format code using Black python formatter?
It’s very easy to format our code using “Black” just run the below command.
black main.py
black wifipwd
You will get output something like this
All done! ✨ ???? ✨
1 file reformatted.
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 <pratikpathak200@gmail.com>, 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 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`
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
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.
![image](https://pratikpathak.com/wp-content/uploads/2024/01/image.png)
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
Full project GitHub Link: Github
Download Premium Poetry Cheatsheet for FREE????
[newsletter_signup_form id=3]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.