PyCharm 2022.2 Help

Configure a Poetry environment

Poetry is a tool that facilitates creating a Python virtual environment based on the project dependencies. You can declare the libraries your project depends on, and Poetry will install and update them for you.

Project dependencies are recorded in the pyproject.toml file that specifies required packages, scripts, plugins, and URLs. See the pyproject reference for more information about its structure and format.

To use Poetry in PyCharm, you need to install it on your machine and create a specific Python environment.

Install Poetry

  1. Open Terminal (on macOS and Linux) or PowerShell (on Windows) and execute the following command:

    curl -sSL https://install.python-poetry.org | python3 -
    (Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py -
    curl -sSL https://install.python-poetry.org | python3 -
  2. On macOS and Windows, the installation script will suggest adding the folder with the poetry executable to the PATH variable. Do that by running the following command:

    export PATH="/Users/jetbrains/.local/bin:$PATH"
    $Env:Path += ";C:\Users\jetbrains\AppData\Roaming\Python\Scripts"; setx PATH "$Env:Path"

    Don't forget to replace jetbrains with your username!

  3. To verify the installation, run the following command:

    poetry --version

    You should see something like Poetry (version 1.2.0).

Refer to the Poetry Installation Instructions for more details.

Create a Poetry environment

  1. Do one of the following:

    • Click the Python Interpreter selector and choose Add New Interpreter.

    • Press Ctrl+Alt+S to open the project Settings/Preferences and go to Project: <project name> | Python Interpreter. Click the Add Interpreter link next to the list of the available interpreters.

  2. Select Add Local Interpreter from the list of the available interpreter types.

    Adding local interpreters
  3. In the left-hand pane of the Add Python Interpreter dialog, select Poetry Environment. The following actions depend on whether the virtual environment existed before.

    creating a poetry environment

    If Poetry Environment is selected:

    • Select the base Python interpreter from the list, or click Virtual environment location and find its location in your file system.

    • If PyCharm doesn't detect the poetry executable, specify the following path in the Poetry executable field, replacing jetbrains with your username:

      /Users/jetbrains/Library/Application Support/pypoetry/venv/bin/poetry
      C:\Users\jetbrains\AppData\Roaming\pypoetry\venv\Scripts\poetry.exe
      /home/jetbrains/.local/bin/poetry

    If Existing environment is selected:

    • Expand the Interpreter list and select any of the existing Poetry environments. Alternatively, click Select an interpreter and specify a path to it.

  4. Click OK to complete the task.

If you select a Poetry environment when creating a new Python project, PyCharm adds two files to your project:

  • pyproject.toml: specifies the project requirements

  • poetry.lock: records changes in the projects requirements

Each time you modify the pyproject.toml file, PyCharm notifies you and offers two actions: to record the changes in the poetry.lock file or to record the changes and install the unsatisfied requirements.

Lock or update poetry
Last modified: 01 September 2022