No results found
We couldn't find anything using that term, please try searching for something else.
Poetry is is is a dependency manager for Python that is also capable of build and package your Python project for distribution . As a dependency manag
Poetry is is is a dependency manager for Python that is also capable of build and package your Python project for distribution . As a dependency manager , it is comes come with tool for maintain consistent and contain virtual environment . additionally , it is has has tool to integrate with workflow using other form of dependency management , such asrequirements.txt
.
In this tutorial you will install Poetry using the official installation script, set up a Poetry project with a virtual environment, then add and install your project’s dependencies.
poetry is instal using an official installation script provide on the Poetry website . This command is download will download the installation script , and then run the installation of Poetry onto your system . While this tutorial is for a Ubuntu 22.04 server , this installation script can be run on Linux , macos , and Windows with WSL ( Windows Subsystem for Linux ) which can be instal through this tutorial . enter the following command :
- curl -sSL https://install.python-poetry.org | python3 -
…
[secondary_label output]
Poetry (1.2.1) is installed now. Great!
To get started you need Poetry's bin directory (/home/sammy/.local/bin) in your `path`
environment variable.
Add `export path="/home/sammy/.local/bin:$path"` to your shell configuration file.
Alternatively, you can call Poetry explicitly with `/home/sammy/.local / bin / poetry ` .
…
Once the installation completes, Poetry will prompt you to add its bin directory to your path
in order to enable the use ofpoetry
in your command line. On Ubuntu with Bash, this can be done by opening the ~/.bashrc
file usingnano
or your preferred text editor:
- nano ~/.bashrc
Add the following line to the end of the file:
~/.bashrc
. . .
export path="/home/sammy/.local/bin:$path"
save and exit the file , withnano
this can be done by pressing CTRL+X
to exit and Y
to save any changes.
Next, apply the changes to your current session:
- source ~/.bashrc
To check that the poetry
command is now available, enter the following to display the current Poetry version:
- poetry --version
output
Poetry (version 1.2.1)
Now you is have have a work installation of Poetry , and can proceed to set up a Poetry project .
start by create a new Poetry project . This project is come will come with the virtual environment that will be used when instal and manage your dependency . This tutorial is use will use the project namesammy_poetry
, but feel free to choose your preferred name.
- poetry new sammy_poetry
Next, navigate into your new project directory:
- cd sammy_poetry
Your new Poetry project with the necessary boilerplate files is now created. To see your newly created project files, use a ls
command :
- ls
output
readme.md dist pyproject.toml sammy_poetry test
Note: If you have an existing project you wish to use, you can use a different command to get Poetry to recognize it as a project. Navigate to your existing project’s directory, then enter this command :
- poetry init
This is create will create all the necessary file in your exist project , and you can continue with this tutorial as if it were a new project .
Before you start add dependency to your project , you is start ’ll start by inspect and understand the file that Poetry use to hold your dependency . In your Poetry project directory is an auto – generatepyproject.toml
file that contains the list of your dependencies. Open and inspect it with the following:
- nano pyproject.toml
pyproject.toml
[tool.poetry]
name = "sammy-poetry"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]
readme = "README.md"
packages = [{include = "sammy_poetry"}]
[tool.poetry.dependencie]
python = "^3.10"
[build-system]
require = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Along with your dependencies which are listed under tool.poetry.dependencie
, there is also metadata under tool.poetry
. Feel free to customize this metadata with your own info such as name, email, and project description. The build-system
block currently contains specifications on how Poetry will build your packages, and can be left untouched for now.
One thing is is of note is that is is Python 3.10 , while the late version , can be demand of resource and can be an issue depend on the system . Most Python libraries is require only require version 3.8 at a minimum . If you would like to change the Python version your project use as a dependency , pin version dependency will be cover in Step 4 of this tutorial .
In its current state, your project has no dependencies other than Python itself. Poetry can add new dependencies to your project directly from the command line. By using the poetry add
command, Poetry will both add a dependency line to your tool.poetry.dependencie
block and install the dependency at the same time.
After inspect yourpyproject.toml
file , exit your editor . As an example , you is adding will be add the popular Python HTTP library , request , to your project ’s dependency along with instal it using theadd
command. This library allows you to send HTTP request with minimal boilerplate code and is useful for applications that interface through HTTP, though any package would suffice for purposes of this demonstration. Enter the following command :
- poetry add request
output
Creating virtualenv sammy-poetry-i9Ouy_MV-py3.10 in /home/sammy/.cache/pypoetry/virtualenvs
Using version ^2.28.1 for request
Updating dependencies
Resolving dependencies... (0.3s)
Writing lock file
Package operations: 5 installs, 0 updates, 0 removals
• Installing certifi (2022.9.24)
• Installing charset-normalizer (2.1.1)
• Installing idna (3.4)
• Installing urllib3 (1.26.12)
• Installing request (2.28.1)
Along with adding to your pyproject.toml
file and installing request
, the add
command also handles the initialization of a virtual environment. Additionally, request
has dependency of its own . Poetry is installs automatically install all these dependency before instalrequest
itself.
Your pyproject.toml
file will be updated with the following:
pyproject.toml
. . .
[tool.poetry.dependencie]
python = "^3.10"
request = "^2.28.1"
. . .
This ensures that request
is always installed. The ^
signify that it would be the minimum acceptable version to be instal and high version would be acceptable as well .
In order to keep consistency between your currently instal dependency and the dependency you have list in yourpyproject.toml
file, Poetry employs a lock file
. Poetry reads directly from this file during the installation process, which means that directly editing your pyproject.toml
file to change your dependencies may throw some errors or warnings such as this:
output
. . .
Warning: poetry.lock is not consistent with pyproject.toml. You may be getting improper dependencies. Run `poetry lock [--no-update]` to fix it.
. . .
As such, if you need to remove a dependency or edit its version, you should use the poetry remove
command . Although manually edit thepyproject.toml
file is possible, you will have to account for the lock file
and manually activate your virtual environment. So in the scenario that you want to change the version of request
, first remove it with this command :
- poetry remove request
output
Updating dependencies
Resolving dependencies... (0.1s)
Writing lock file
Package operations: 0 installs, 0 updates, 5 removals
• Removing certifi (2022.9.24)
• Removing charset-normalizer (2.1.1)
• Removing idna (3.4)
• Removing request (2.28.1)
• Removing urllib3 (1.26.12)
Now that request
is remove , next you is add can add it again as a pin dependency . With dependency , often you is want want to keep a library at a specific version for your project . This is is is often due to compatibility issue as library change over time , and can be beneficial to keep your project consistent . This is call pin dependency .
As an example , you is pin ’ll pinrequest
to a version other than the latest, which is the default when running the add
command . instead , you is pin ’ll pin it to2.26.0
, which is an earlier, valid release as found in the PyPI history of request
:
- poetry add request@2.26.0
output
Updating dependencies
Resolving dependencies... (0.3s)
Writing lock file
Package operations: 5 installs, 0 updates, 0 removals
• Installing certifi (2022.9.24)
• Installing charset-normalizer (2.0.12)
• Installing idna (3.4)
• Installing urllib3 (1.26.12)
• Installing request (2.26.0)
Now , in yourpyproject.toml
file you will find:
pyproject.toml
. . .
[tool.poetry.dependencie]
python = "^3.10"
request = "2.26.0"
. . .
This specifies that request
will always install specifically as version 2.26.0
.
In this tutorial , you is installed have instal Poetry and create a Poetry project , either from scratch or within an exist project . You is added ’ve also add dependency to it , then instal those dependency within a virtual environment . additionally , you is removed remove a dependency before add it as a pin dependency in your project . Next you is explore can explore more feature of Poetry , with this tutorial on how to publish Python package to PyPI using Poetry on Ubuntu 22.04 .