question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Up-to-date packages are re-installed if virtualenvs.create is false

See original GitHub issue

Re-opening #4254, which was closed wrongly, #4329 does not fix this bug.

  • I am on the latest current master (22c3aaddc20ad74d3633738093f685fd6dbb998a) Poetry version.

  • I have searched the issues of this repo and believe that this is not a duplicate.

  • If an exception occurs when executing a command, I executed it again in debug mode (-vvv option).

  • OS version and name: Ubuntu 20.04

  • Poetry version: current master (22c3aaddc20ad74d3633738093f685fd6dbb998a) (also tested with version 1.2.0a2)

  • Link of a Gist with the contents of your pyproject.toml file: https://gist.github.com/jstriebel/6e804c769e2075be390496e68474a36c

Issue

Packages that were already installed (by poetry itself) are re-installed if virtualenvs.create is false. This bug only appears on v1.2, it does not occur on the 1.1 branch or the latest v1.1 releases. I added a reproducible case via a Dockerfile in the linked gist.

When running docker build . on those files, this produces:

Step 12/14 : RUN poetry install
 ---> Running in 73dfd5dcf683
Skipping virtualenv creation, as specified in config file.
Installing dependencies from lock file

Package operations: 3 installs, 0 updates, 2 removals

  • Removing importlib-metadata (4.6.1)
  • Removing zipp (3.5.0)
  • Installing attrs (21.2.0)
  • Installing pyrsistent (0.18.0)
  • Installing jsonschema (3.2.0)
Removing intermediate container 73dfd5dcf683
 ---> 04e982ae2043
Step 13/14 : RUN echo SECOND INSTALL!!!
 ---> Running in 7ec46c74fa47
SECOND INSTALL!!!
Removing intermediate container 7ec46c74fa47
 ---> eff9efcdee5e
Step 14/14 : RUN poetry install
 ---> Running in a877dc9c4864
Skipping virtualenv creation, as specified in config file.
Installing dependencies from lock file

Package operations: 3 installs, 0 updates, 0 removals

  • Installing attrs (21.2.0)
  • Installing pyrsistent (0.18.0)
  • Installing jsonschema (3.2.0)
Removing intermediate container a877dc9c4864

The packages are not installed twice if virtualenvs.create is true (tested by commenting out the COPY poetry.toml / line).

Possibly related previous issues: #1711, #1882 (solved in previous releases, but re-introduced in v1.2)

Given that this was broken multiple times in different ways now, and was not fixed by #4329 as intended, would it be possible to add some tests for this case as well? I’d be happy to help here, but have no experience with the poetry code-base.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
abncommented, Mar 4, 2022

@jstriebel the root cause here is the sys.path value in ubuntu image’s python installation. The sequence of events that cause this issue is the following:

  1. When Poetry discovers installed packages, the sys.path output is used when the system environment is in use. This on your base image returns without /usr/lib/python3.8/site-packages (as debian systems rely on dist-packages for system site. However the local (/usr/local/lib) site is correctly present.
root@413fb0865efc:/# python3 -m site
sys.path = [
    '/',
    '/usr/lib/python38.zip',
    '/usr/lib/python3.8',
    '/usr/lib/python3.8/lib-dynload',
    '/root/.local/lib/python3.8/site-packages',
    '/usr/local/lib/python3.8/dist-packages',
    '/usr/lib/python3/dist-packages',
]
USER_BASE: '/root/.local' (exists)
USER_SITE: '/root/.local/lib/python3.8/site-packages' (exists)
ENABLE_USER_SITE: True
  1. Poetry proceeds to download the wheel and install it using pip with the option --prefix /usr.
  2. pip installs package to /usr/lib/python3.8/site-packages as per Python spec.
  3. When Poetry attempts to detect installed packages again, as the site-packages directory is not in sys.path it is detected as no installed.

You can work around this issue by setting PYTHONPATH.

ENV PYTHONPATH=/usr/lib/python3.8/site-packages

Poetry could try passing --pre /usr/local instead to pip. However, I am not certain there is a realiable way to do this across distros.

Personally, I would always recommend using a virtual environment, even in containers. A lot less volatility.

Additionally, you can verify that this scenario works without issue in another image like shown here.

podman run --rm -i --entrypoint bash python:3.10 <<EOF
set -xe
python -m pip install -q git+https://github.com/python-poetry/poetry.git@master
poetry config virtualenvs.create false
poetry new foobar
pushd foobar
poetry add pycowsay
poetry install
poetry install
EOF
+ python -m pip install git+https://github.com/python-poetry/poetry.git@master
+ poetry config virtualenvs.create false
+ poetry new foobar
Created package foobar in foobar
+ pushd foobar
/foobar /
+ poetry add pycowsay
Skipping virtualenv creation, as specified in config file.
Using version ^0.0.0.1 for pycowsay

Updating dependencies
Resolving dependencies...

Writing lock file

Package operations: 1 install, 0 updates, 0 removals

  • Installing pycowsay (0.0.0.1)
+ poetry install
Skipping virtualenv creation, as specified in config file.
Installing dependencies from lock file

No dependencies to install or update

Installing the current project: foobar (0.1.0)
+ poetry install
Skipping virtualenv creation, as specified in config file.
Installing dependencies from lock file

No dependencies to install or update

Installing the current project: foobar (0.1.0)
0reactions
jstriebelcommented, Apr 19, 2022

@abn Thanks a lot for the detailed answer, and sorry for my late reply.

I agree, that this seems to be a bug in the configuration. Should sys.prefix rather point to /usr/local, where pip-installed packages are expected under debian/ubuntu? I assume the /usr prefix should only be used for system-packages, e.g. installed via apt.

Poetry could try passing --pre /usr/local instead to pip. However, I am not certain there is a realiable way to do this across distros.

How about simply not providing --prefix to pip? I guess this might be a more stable strategy in the case of no virtualenv? This would ensure that the packages are installed “the standard way”. However, finding the correct installation path is more of a hassle then.

However the local (/usr/local/lib) site is correctly present.

Only the dist-packages directory, not for site-packages, right?

I’m very grateful that you are helping to deal with the package installation chaos! Thanks a ton for your work 🙏

The only “fix”/“workaround” I could see atm is removing the prefix. If that’s something you’d like to avoid, please feel free to close this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

pre-installed with packages on creating virtual environment ...
I am on Ubuntu 20.04 with this version of virtualenv: python3-virtualenv/focal,focal,now 20.0.17-1 all - I never had the issue while on previous ...
Read more >
venv — Creation of virtual environments — Python 3.11.1 ...
Creation of virtual environments is done by executing the command venv : ... to true if venv is run with the --system-site-packages option,...
Read more >
Configuration | Documentation | Poetry - Python dependency ...
Use currently activated Python version to create a new virtual environment. If set to false , Python version used during Poetry installation is...
Read more >
Understanding Python virtual environments using venv and ...
A Virtual environment is a light weight python installation with its own package directories and python binary (either copied or linked from ...
Read more >
Advanced Usage of Pipenv - Python Packaging Authority
For a specific package to be installed from an alternate package index, ... This will fail a build if the Pipfile.lock is out–of–date,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found