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.

`poetry install --no-root` with only poetry.lock

See original GitHub issue
  • I have searched the issues of this repo and believe that this is not a duplicate.
  • I have searched the documentation and believe that my question is not covered.

Feature Request

When doing poetry install, both pyproject.toml and poetry.lock are required. This makes sense as the package itself is installed along with its dependencies.

However, this breaks caching in Docker. Let’s say I have these lines in docker:

COPY pyproject.toml poetry.lock README.md /
RUN  poetry install --no-dev

(Note how I also need the README.md as that is referenced in the pyproject.toml)

The main problem here is: On every build, I bump the version using poetry version .... This changes the pyproject.toml file, therefore breaks the docker caching mechanism. In comparison, with an additional requirements.txt, I can do something like:

COPY requirements.txt /
RUN pip install -r requirements.txt
COPY pyproject.toml poetry.lock README.md /
RUN  poetry install --no-dev

In this case, all the dependencies (which quite often do not change between builds) can be cached. This speeds up my docker build by 80-90%, but it’s obviously ugly to have to rely on requirements.txt

FYI: I have a workaround for requirements.txt, where I simply generate it from poetry.lock:

def _poetry_lock_to_requirements_txt(start_dir):
    with open(f'{start_dir}/poetry.lock') as f:
        lock = toml.load(f)

    requirements = {package['name']: package['version']
                    for package in lock['package']}

    with open(f'{start_dir}/requirements.txt', 'w') as f:
        f.writelines(f'{name}=={version}\n' for name, version in sorted(requirements.items()))

Alternatively, I could do some trickery around when to (re-)set the version, but this is getting uglier and uglier [plus I need requirements.txt anyway due to an other issue with poetry that I find really hard to pin down - more on that soon]

I would suggest adding the flag poetry install --dependencies-only which only requires poetry.lock, not pyproject.toml

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:103
  • Comments:37 (8 by maintainers)

github_iconTop GitHub Comments

54reactions
a-recknagelcommented, Aug 22, 2019

Honest question, why would you use poetry to set up a container? pip (with export from preview builds, as dmontagu pointed out) is enough. See here for a stack overflow post where someone had a different problem with using poetry in docker, where just using pip seems a good alternative.

21reactions
mcouthoncommented, Apr 4, 2020

This issue is related to #1899. I posted my current solution for caching there, and I’ll post it here too, if it might help anyone:

# Only copying these files here in order to take advantage of Docker cache. We only want the
# next stage (poetry install) to run if these files change, but not the rest of the app.
COPY pyproject.toml poetry.lock ./

# Currently poetry install is significantly slower than pip install, so we're creating a
# requirements.txt output and running pip install with it.
# Follow this issue: https://github.com/python-poetry/poetry/issues/338
# Setting --without-hashes because of this issue: https://github.com/pypa/pip/issues/4995
RUN poetry config virtualenvs.create false \
                && poetry export --without-hashes -f requirements.txt --dev \
                |  poetry run pip install -r /dev/stdin \
                && poetry debug

COPY  . ./

# Because initially we only copy the lock and pyproject file, we can only install the dependencies
# in the RUN above, as the `packages` portion of the pyproject.toml file is not
# available at this point. Now, after the whole package has been copied in, we run `poetry install`
# again to only install packages, scripts, etc. (and thus it should be very quick).
# See this issue for more context: https://github.com/python-poetry/poetry/issues/1899
RUN poetry install --no-interaction

# We're setting the entrypoint to `poetry run` because poetry installed entry points aren't
# available in the PATH by default, but it is available for `poetry run`
ENTRYPOINT ["poetry", "run"]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Basic usage | Documentation | Poetry - Python dependency ...
The current project is installed in editable mode by default. If you want to install the dependencies only, run the install command with...
Read more >
pip install your poetry.lock - bneijt.nl
The best way to install poetry-lock-package is to just add it to poetry using poetry add --dev poetry-lock-package . You could also use...
Read more >
poetry-lock-package - PyPI
If you want to be able to install the dependencies, but not the package itself, you can use the --no-root command line argument...
Read more >
How to use poetry with docker? - Stack Overflow
Install poetry with pip, configure virtualenv, install ... WORKDIR /app # copy only pyproject.toml and poetry.lock file nothing else here ...
Read more >
Managing your Python dependencies with Poetry - Devopsbay
After successful installation let's see how to use this tool. If you are just starting a new project, you can use poetry new...
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