When your Python project relies on external packages, you need to make sure you’re using the right version of each package. After an update, a package might not work as it did before the update. A dependency manager like Python Poetry helps you specify, install, and resolve external packages in your projects. This way, you can be sure that you always work with the right dependency version on every machine.
In this tutorial, you’ll learn how to:
- Start a new Poetry project
- Add Poetry to an existing project
- Use the
pyproject.toml
file - Pin dependencies
- Install dependencies with
poetry.lock
- Execute basic Poetry CLI commands
Using Poetry will help you start new projects, maintain existing ones, and master dependency management. You’ll be prepared to work with pyproject.toml
files, which will be the standard for defining build requirements in Python projects.
To complete this tutorial and get the most out of it, you should have a basic understanding of virtual environments, modules and packages, and pip
.
While this tutorial focuses on dependency management, Poetry can also help you with building and packaging projects. If you want to share your work, then you can even publish your Poetry project to the Python Packaging Index (PyPI).
Free Bonus: Click here to get access to a free 5-day class that shows you how to avoid common dependency management issues with tools like Pip, PyPI, Virtualenv, and requirements files.
Take Care of Prerequisites
Before diving into the nitty-gritty of Python Poetry, you’ll take care of some prerequisites. First, you’ll read a short overview of terminology you’ll encounter in this tutorial. Next, you’ll install Poetry itself.
Relevant Terminology
If you’ve ever used an import
statement in one of your Python scripts, then you’ve worked with modules. Some of these modules might have been Python files you wrote on your own. Others could have been built-in modules, like datetime. However, sometimes what Python provides isn’t enough. That’s when you might turn to external, packaged modules. When your Python code relies on external modules, you can say that these packages are dependencies of your project.
You can find packages that aren’t part of the Python standard library in PyPI. Before seeing how this works, you need to install Poetry on your system.
Python Poetry Installation
To use Poetry in your command line, you should install it system-wide. If you just want to try it out, then you can install it into a virtual environment using pip
. But you should try this method with caution because Poetry will install its own dependencies, which can conflict with other packages you’re using in your project.
The recommended way to install Poetry is by using the official install-poetry
script. You can either download and run this Python file manually or select your operating system below to use the appropriate command:
PS C:\> (Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py -UseBasicParsing).Content | python -
If you’re on Windows, then you can use the Invoke-Webrequest
cmdlet with the -UseBasicParsing
option to download the content of a requested URL to the standard output stream (stdout). With the pipe character (|
), you’re handing over the output to the standard input stream (stdin) of python
. In this case, you’re piping the content of install-poetry.py
to your Python interpreter.
Note: Some users report errors on Windows 10 when they use the PowerShell command.
$ curl https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | python3 -
With curl
, you’re outputting the content of a requested URL to the standard output stream (stdout). By using a Unix pipeline with the pipe character (|
), you’re handing over the output to the standard input stream (stdin) of python3
. In this case, you’re piping the content of install-poetry.py
to your Python interpreter.
Note: If you’re on macOS, then you may get an ssl.SSLCertVerificationError
. This error can occur if you don’t have the default root certificates for the SSL module installed. You can install them by running a command script in your Python folder:
$ open "/Applications/Python 3.9/Install Certificates.command"
Depending on your installed Python version, the specific path to the Python interpeter might be different. In that case, you need to adjust the path in the above command accordingly.
After you run the command, the curl
command above should work without any errors.
In the output, you should see a message that the installation is complete. You can run poetry --version
in your terminal to see if poetry
works. This command will display your current Poetry version. If you want to update Poetry, then you can run poetry self update
.
Get Started With Python Poetry
With Poetry installed, it’s time to see how Poetry works. In this section, you’ll learn how to start a fresh Poetry project and how to add Poetry to an existing project. You’ll also see the project structure and inspect the pyproject.toml
file.
Create a New Poetry Project
You can create a new Poetry project by using the new
command and a project name as an argument. In this tutorial, the project is called rp-poetry
. Create the project, and then move into the newly created directory:
$ poetry new rp-poetry
$ cd rp-poetry
By running poetry new rp-poetry
, you create a new folder named rp-poetry/
. When you look inside the folder, you’ll see a structure:
rp-poetry/
│
├── rp_poetry/
│ └── __init__.py
│
├── tests/
│ ├── __init__.py
│ └── test_rp_poetry.py
│
├── README.rst
└── pyproject.toml
Read the full article at https://realpython.com/dependency-management-python-poetry/ »
[ Improve Your Python With ๐ Python Tricks ๐ – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
from Real Python
read more
No comments:
Post a Comment