Set up a poetry environment running on PyCharm (2023)

Set up a poetry environment running on PyCharm (1)

poetry landscape

A quick and easy guide to managing and packaging Python dependencies

Poemsis a wonderful open source tool that can make it easier for you to set up your environment and configure your dependencies.

In this blog I take a small look at the possibilities of poetry and the power that lies within it. I will also walk you through the technical process of setting up the Poetry environment in PyCharm.

Poetry has several advantages over some existing packaging and dependency management tools (seepoetry introductionja poetic apology). Perhaps the main reason for poetry's growing popularity can be found in the following quote from the Introduction to Poetry:

Poemsis a tool for installing dependencies, building and packaging Python packages. You only need one file for this: the new,standardized pyproject.toml.

In other words, poetry usedpyproject.tomlsubstituteconfiguration.py,Requirements.txt,Konfiguration.cfg,MANIFEST.injPip-Datei.

There are many different configuration options for using Poetry to manage Python packages and dependencies. The one that I will present in this blog is the configuration that I found the most comfortable and that meets all my needs.

follows the poemDocumentationto install Poetry according to your system environment.

For PyCharm versions >=2021.3 Poetry is already integrated without manual plugin installation.

For PyCharm versions < 2021.3 we install the required plugin for PyCharm. Below is the list of Poetry versions for the different JetBrains IDEs.

After clicking the link above, a pop-up window will appear in the IDE with instructions on how to install the plugin.

We are ready to create our new Poetry environment! In this example I create a package calledassessmentsused for both training and maintenance of our text classification model that extracts quality of life information in apartment complexes from tenant reviews (Haber e Waks, 2021 See more).

Poetry new reviews

The result of this step is thepyproject.tomlOffice hours:

[tool.poetry]
name="comments"
version="0.1.0"
Description = ""
autores = ["hai.rozencwajg <EMAIL>"]

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

[tool.poetry.dev-dependencias]
Pytest = "^5.2"

[Bausystem]
needed="["core-poetry>=1.0.0"]
build-backend = "poetry.core.masonry.bee"

My installed Python version is3.7.9, and the Python version at the[tool.poetry.dependencies]the section is^3.7, that is, Python versions equal to or greater than3.7it is smaller than4Execution.

To read more about symbols in poetry, like the cursor (^) and additional version restrictions, seePoetry dependency specificationManual.

Next, let's create a Poetry environment for this project by running the following command:

Poem installation

This will create a local virtual environment that will be used to run the project based on the settings listed inpyproject.tomloffice hours.

(Video) How to use Poetry to Manage Python Dependencies and Publish Packages

The locale was created in the default Poetry locale path~/Biblioteca/Caches/pypoetry/virtualenvs.

Set up a poetry environment running on PyCharm (2)

After clicking onAdd interpreterIn PyCharm, the existing interpreter was automatically found and the Poetry virtual environment was automatically activated for this project.

similarly famousInstall pipePoetry command also supports easy installation of Python packages with PoetryaddDomain.

poetry adds google-cloud-storage

After running the above, a new line was added topyproject.tomlFile documenting the version for theGoogle cloud storagePackage.

[tool.poetry]
name="comments"
version="0.1.0"
Description = ""
autores = ["hai.rozencwajg <EMAIL>"]
[tool.poetry.dependencies]
python="^3.7"
Google cloud storage = "^2.4.0"

[tool.poetry.dev-dependencias]
Pytest = "^5.2"

[Bausystem]
needed="["core-poetry>=1.0.0"]
build-backend = "poetry.core.masonry.bee"

aside from thatpoem.lockfile was created. This file keeps track of the installed versions of packages and their dependencies as indicated in the following log:

Using version ^2.4.0 for Google cloud storageUpdate dependencies
Dependencies are resolved... (9.7s)
File Write LockBundle Operations: 18 installs, 0 upgrades, 0 removals• Installation of pyasn1 (0.4.8)
• Installation von Caching-Tools (5.2.0)
• Installation of certificates (2022.6.15)
• Installation des Character Set Normalizer (2.1.0)
• idna-Installation (3.3)
• Install protobuf (4.21.2)
• Installation of pyasn1 modules (0.2.8)
• Installation of rsa (4.8)
• Installation of six (1.16.0)
• Installation of urllib3 (1.26.10)
• Installation von Google-Auth (2.9.0)
• Installation von googleapis-common-protos (1.56.3)
• Installation Prompts (2.28.1)
• Installation von google-api-core (2.8.2)
• Installation von google-crc32c (1.3.0)
• Installation von google-cloud-core (2.3.1)
• Installation of resumable media from Google (2.3.3)
• Google Cloud Storage-Installation (2.4.0)

Next I wanted to add thegoogle-cloud-bigqueryPack for Poetry, so I ran PoetryaddDomain:

add poetry google-cloud-bigquery

But this time I got an error that demonstrates the power of poetry:

Using version ^3.2.0 for google-cloud-bigqueryUpdate dependencies
Dependencies are resolved... (0.2s)
SolverProblemErrorThe current project's Python requirement (>=3.7,<4.0) is incompatible with some of the Python requirements of the required packages:
- google-cloud-bigquery requires Python >=3.6, <3.11, so you won't be satisfied with Python >=3.11,<4.0

Since for google-cloud-bigquery (3.2.0) Python >=3.6, <3.11
and no version of google-cloud-bigquery matches >3.2.0,<4.0.0, google-cloud-bigquery is forbidden.
As revisions depend on google-cloud-bigquery (^3.2.0), version resolution therefore failed.

de ~/.poetry/lib/poetry/puzzle/solver.py:241 de _solve
237│packets = result.packets
238│ except OverrideNeeded as e:
239│ devuelve self.solve_in_compatibility_mode(z.B. overrides, use_latest=use_latest)
240│ except SolveFailure as and:
→ Raise 241│ SolverProblemError(s).
242│
243│ results = dict(
244│ deep_first_search(
245│ PackageNode(self._package, pacotes), Added_package_nodes
• Check the Python requirement of your dependencies: The Python requirement can be specified using the `python` or `markers` properties

For google-cloud-bigquery, a possible solution would be to set the 'python' property to '>=3.7,<3.11'.

https://python-poetry.org/docs/dependency-specification/#python-restricted-dependencies,
https://python-poetry.org/docs/dependency-specification/#using-environment-markers

It turns out that there is a compatibility issue between the latest version ofgoogle-nube-bigquery==3.2.0and the possible Python versions listed inpyproject.tomloffice hours.

(Video) Configuring a local Python interpreter in PyCharm | Getting started

Luckily for us, Poetry also offers us a possible solution:

For google-cloud-bigquery, a possible solution would be to set the 'python' property to '>=3.7,<3.11'.

As we can see above, Poetry asks us to be more strict with the Python version and limit ourselves to>=3,7, <3,11instead of the current setting^3.7, so I changed the python version topyproject.tomlfile as suggested.

[tool.poetry]
name="comments"
version="0.1.0"
Description = ""
autores = ["hai.rozencwajg <EMAIL>"]

[tool.poetry.dependencies]
python = ">=3.7,<3.11"
Google cloud storage = "^2.4.0"

[tool.poetry.dev-dependencias]
Pytest = "^5.2"

[Bausystem]
needed="["core-poetry>=1.0.0"]
build-backend = "poetry.core.masonry.bee"

Now, after re-executing the poetryaddcommand for thegoogle-cloud-bigqueryPackage successfully completed installation.

Using version ^3.2.0 for google-cloud-bigqueryUpdate dependencies
Dependencies are resolved... (91.8 s)
File Write LockPackage operations: 8 installs, 1 update, 0 removes• Protobuf-Update (4.21.2 -> 3.20.1)
• Install grpcio (1.47.0)
• Install grpcio-status (1.47.0)
• Install numpy (1.21.6)
• Installation of proto-plus (1.20.6)
• Installation von google-cloud-bigquery-storage (2.14.0)
• Installation of Pyarrow (8.0.0)
• Installation of python-dateutil (2.8.2)
• Installation von google-cloud-bigquery (3.2.0)

We can also see that theProtobufThe version has been downgraded (automatically) to support all our version compatibility limitations.

The newpyproject.tomlFile now looks like this:

[tool.poetry]
name="comments"
version="0.1.0"
Description = ""
autores = ["hai.rozencwajg <EMAIL>"]

[tool.poetry.dependencies]
python = ">=3.7,<3.11"
Google cloud storage = "^2.4.0"
google-nube-bigquery = "^3.2.0"

[tool.poetry.dev-dependencias]
Pytest = "^5.2"

[Bausystem]
needed="["core-poetry>=1.0.0"]
build-backend = "poetry.core.masonry.bee"

Install a specific package version

similar tolump, Poetry supports installing certain package versions:

Poetry add pandas ~= 1.3

Installing packages for development environments only

It's fun to say goodbye to so manyRequirements.txtPoetry files support management of production and development environments in a single file. install downPanda-ProfileOnly use in the development environment for data exploration.

poetry add pandas profiles --dev

ÖPanda-ProfileThe package is listed below[tool.poetry.dev-dependencias]section instead of below[tool.poetry.dependencies].

uninstall packages

Uninstalling is as easy as adding packages:

(Video) Awesome PyCharm Setup - Full Guide

delete poetry google-cloud-bigquery

Open a new shell and start it with a Poetry environment

poetry shell

Poetry is a great, feature-packed tool that allows you, as a developer, to manage your dependencies in a clean, easy, and transparent way. Using Poetry makes it easier to configure your environment and helps resolve compatibility issues.

I would definitely continue to use poetry for my future projects and I hope you feel the same way.

Hello Rozencwajis a leading data scientist inHorizon OFF, oneJLLcompany and part ofJLL Technologies. We use broad and diverse information along with cutting-edge technology to provide real estate professionals with insights that create unique business value.

Videos

1. Configure a Python interpreter | PyCharm - JetBrains
(TechWebDocs)
2. PyCharm Virtual Environments (venv) Explained! | 10 Min. | Updated 2023
(Max Teaches Tech)
3. Pycharm How To Create Virtual Environment For A Project
(Automation Step by Step)
4. How to Set Environment Variables in PyCharm - 3 Different Ways!
(Jarad Python)
5. Setup VS Code for Python with Pyenv and Poetry
(QA at the Point | Carlos Kidman)
6. PyBites Python Poetry Training
(PyBites)
Top Articles
Latest Posts
Article information

Author: Stevie Stamm

Last Updated: 05/01/2023

Views: 6676

Rating: 5 / 5 (60 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Stevie Stamm

Birthday: 1996-06-22

Address: Apt. 419 4200 Sipes Estate, East Delmerview, WY 05617

Phone: +342332224300

Job: Future Advertising Analyst

Hobby: Leather crafting, Puzzles, Leather crafting, scrapbook, Urban exploration, Cabaret, Skateboarding

Introduction: My name is Stevie Stamm, I am a colorful, sparkling, splendid, vast, open, hilarious, tender person who loves writing and wants to share my knowledge and understanding with you.