Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python deps not found on path after installing with Poetry (and Poetry installed with pipx) #5927

Closed
2 tasks done
ajhynes7 opened this issue Jun 28, 2022 · 8 comments
Closed
2 tasks done
Labels
kind/bug Something isn't working as expected

Comments

@ajhynes7
Copy link

  • I am on the latest Poetry version.
  • I have searched the issues of this repo and believe that this is not a duplicate.
  • OS version and name: Docker Python image
  • Poetry version: 1.1.13

Issue

I'm trying to install Python dependencies with Poetry, after installing Poetry with pipx. The problem is that the Python dependencies installed with Poetry aren't being found on the path.

Here's my demo directory structure:

$ tree
.
├── Dockerfile
├── foo.py
├── poetry.lock
├── poetry.toml
└── pyproject.toml

I'm installing flake8 as a dev dependency.

# pyproject.toml

[tool.poetry]
name = "demo"
version = "0.1.0"
description = ""
authors = []

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

[tool.poetry.dev-dependencies]
flake8 = "^4.0.1"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

I've disabled Poetry's virtualenv creation.

# poetry.toml

[virtualenvs]
create = false

Here's a Dockerfile to provide a minimal working example:

# Dockerfile

FROM python:3.8-slim-buster

RUN pip install pipx && \
    pipx install poetry

# Put Poetry on the path.
ENV PATH=/root/.local/bin:$PATH

WORKDIR /app

COPY pyproject.toml poetry.toml poetry.lock ./
RUN poetry install --no-root

COPY . .

ENTRYPOINT []

The flake8 command isn't found in the Docker container.

❯ docker build -t demo .

❯ docker run demo flake8 --version
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "flake8": executable file not found in $PATH: unknown.
ERRO[0000] error waiting for container: context canceled

But Poetry shows that it knows about flake8.

❯ docker run demo poetry show flake8
Skipping virtualenv creation, as specified in config file.
name         : flake8
version      : 4.0.1
description  : the modular source code checker: pep8 pyflakes and co

dependencies
 - mccabe >=0.6.0,<0.7.0
 - pycodestyle >=2.8.0,<2.9.0
 - pyflakes >=2.4.0,<2.5.0

Running flake8 with poetry run works.

❯ docker run demo poetry run flake8 --version
Skipping virtualenv creation, as specified in config file.
4.0.1 (mccabe: 0.6.1, pycodestyle: 2.8.0, pyflakes: 2.4.0) CPython 3.8.13 on
Linux

But I thought that poetry run wasn't required if you disabled Poetry's virtualenv feature.

@ajhynes7 ajhynes7 added kind/bug Something isn't working as expected status/triage This issue needs to be triaged labels Jun 28, 2022
@dimbleby
Copy link
Contributor

I've disabled Poetry's virtualenv creation.

... and haven't created and activated a virtual environment yourself? Where are you expecting your flake8 to show up?

I think that in 1.1.13 it will appear in poetry's own virtual environment which makes no sense at all. In later 1.2.0 betas poetry tries to add it to the system python which, per #5928, probably won't work.

If you don't want poetry to manage your virtual environment you almost certainly nevertheless do want a virtual environment, you are just taking responsibility for doing this yourself.

@ajhynes7
Copy link
Author

... and haven't created and activated a virtual environment yourself? Where are you expecting your flake8 to show up?

In the default Python environment, so you can see flake8 from running pip show flake8.

If you don't want poetry to manage your virtual environment you almost certainly nevertheless do want a virtual environment, you are just taking responsibility for doing this yourself.

Usually yes, but this is in a Docker container so I don't think I need a virtual environment.

@dimbleby
Copy link
Contributor

As I say, more recent poetry versions will put this into the system python, so if that's what you want then you're good. Just install poetry==1.2.0b2.

@dimbleby
Copy link
Contributor

Actually if you're installing via pipx that's probably not true - because pipx puts a new python interpreter into the virtual environment so poetry will still find that and still install flake8 into poetry venv.

But if you install as RUN curl -sSL https://install.python-poetry.org | POETRY_VERSION="1.2.0b2" python - then what I said should be true.

@ajhynes7
Copy link
Author

It works when Poetry is installed with the installation script (without needing a virtual environment or Poetry 1.2.0b2).

FROM python:3.8-slim-buster

RUN apt-get update && \
    apt-get install -y curl

RUN curl -sSL https://install.python-poetry.org | python -

# Put Poetry on the path.
ENV PATH=/root/.local/bin:$PATH

WORKDIR /app

COPY pyproject.toml poetry.toml poetry.lock ./

RUN poetry install --no-root

COPY . .

ENTRYPOINT []

So the problem is specifically when Poetry is installed with pipx.

pipx puts a new python interpreter into the virtual environment

Do you know if there's a way to disable that? I expected Poetry to behave the same way when installed with the script or pipx.

@dimbleby
Copy link
Contributor

No way so far as I know - poetry looks for the python that is executing it and works from there.

To my mind it's inadvisable not to be explicit about your virtual environment, whether that be by creating one manually or by having poetry do it for you. You can always tweak your path afterwards so that executables in that environment are available to all.

@ajhynes7
Copy link
Author

Ok, I found a solution that uses pipx by activating a virtual environment in Docker.

FROM python:3.8.13-slim-buster 

RUN pip install pipx && \
    pipx install poetry

# Put Poetry on the path.
ENV PATH=/root/.local/bin:$PATH

WORKDIR /app

COPY pyproject.toml poetry.toml poetry.lock ./

ENV VIRTUAL_ENV=/opt/env
RUN python -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

RUN poetry install --no-root

COPY . .

ENTRYPOINT []

@mkniewallner mkniewallner removed the status/triage This issue needs to be triaged label Jun 29, 2022
Copy link

github-actions bot commented Mar 1, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/bug Something isn't working as expected
Projects
None yet
Development

No branches or pull requests

3 participants