Skip to content

Instantly share code, notes, and snippets.

@jmsalcido
Last active April 17, 2024 16:51
Show Gist options
  • Save jmsalcido/d89f4fa5059867816000220eebe0d95c to your computer and use it in GitHub Desktop.
Save jmsalcido/d89f4fa5059867816000220eebe0d95c to your computer and use it in GitHub Desktop.
Load correctly poetry environment from docker compose into pycharm available environments

PyCharm and Poetry with Docker (Compose)

I use PyCharm for my python development and normally I use a virtual env but now I found poetry and it manages the dependencies in a better way than just having a pip requirements file.

I had issues loading correctly a poetry environment from docker compose into pycharm available environments.

This sometimes works, sometimes I just reload the entire environment.

Step 1:

Run your docker image of course.

Step 2:

Go to the Python Project Settings at: Project Interpreter

image

Step 3:

Click over Python Interpreter and select Show All...

image

Step 4:

Click over the + button and add a new docker compose item.

image

Step 5:

Continue through the wizard and select your Docker service correctly.

image

Step 6:

The wizard will try to run docker? I dont know why it fails over my pycharm or what but it does not seem to matter.

image

Step 7:

Shell into your docker container and execute: poetry env info -p and copy that path and include /bin/python

Step 8:

On the wizard click over the 3 dots: ...

image

Step 9:

Add the path from the poetry environment but remember to add /bin/python at the end.

image

And hopefully... you will have your environment correctly.

Happy coding.


There is an actual YouTrack issue to follow if you are interesting in something from jetbrains: https://youtrack.jetbrains.com/issue/PY-42909/Support-poetry-in-docker-compose-python-interpreter

@lnlrbr
Copy link

lnlrbr commented Sep 18, 2023

Hi,
You can directly set it using your Dockerfile like so:

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    POETRY_VERSION=1.6.0 \
    POETRY_VIRTUALENVS_IN_PROJECT=true \
    POETRY_HOME="/opt/poetry"

ENV PATH="$POETRY_HOME/bin:/src/.venv/bin:$PATH"

RUN apk --no-cache add curl

# Install poetry - respects $POETRY_VERSION & $POETRY_HOME
RUN curl -sSL https://install.python-poetry.org | python3 -

WORKDIR /src

Pycharm will find the right Python bin in the venv

@Makaveli-x
Copy link

@lnlrbr thanks,its works for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment