简体   繁体   中英

How to use python poetry to install package to a virtualenv in a standalone fashion?

I've recently migrated to poetry for my dependencies management so pardon if my question is out of the scope of poetry here.

Final goal

My final goal is to create a RPM package that contains a virtualenv with my software installed along with all its dependencies . This RPM would then provide my software in isolation with the system where it is installed.

Reproduce the problem

I'm facing a problem while using poetry install in my virtualenv . As soon as the source directory of my software is deleted, my CLI refuses to work any longer.

Reproduce

I've created a simple repository to reproduce the problem: https://github.com/riton/python-poetry-venv

Here are the that I'm using with poetry :

#!/bin/bash -ex

VENV_DIR="/venv"
SRC_DIR="/src"
ALT_SRC_DIR="/src2"
USER_CACHE_DIR="~/.cache"

# Copy directory (cause we're mounting it read-only in the container)
# and we want to remove the source directory later on
cp -r $SRC_DIR $ALT_SRC_DIR

# We'll remove this directory to test if the soft is still working
# without the source dir
cd $ALT_SRC_DIR

[...]

python3.8 -m venv "$VENV_DIR"

source $VENV_DIR/bin/activate

[...]

poetry install --no-dev -v

[...]

# Our software will be called without an activated virtualenv
# so 'deactivate' the current one
deactivate

cd /

echo "Try after install"

# Start the "CLI" after installation
$VENV_DIR/bin/python-poetry-venv

echo "Removing source directory and trying again"
rm -rf $ALT_SRC_DIR

$VENV_DIR/bin/python-poetry-venv

echo "Removing user cache dir and trying again"
rm -rf $USER_CACHE_DIR

$VENV_DIR/bin/python-poetry-venv

The script above fails with the following error:

[...]
Try after install
+ /venv/bin/python-poetry-venv
THIS IS THE MAIN
+ echo 'Removing source directory and trying again'
Removing source directory and trying again
+ rm -rf /src2
+ /venv/bin/python-poetry-venv
Traceback (most recent call last):
  File "/venv/bin/python-poetry-venv", line 2, in <module>
    from python_poetry_venv.cli import main
ModuleNotFoundError: No module named 'python_poetry_venv'
make: *** [Makefile:2: test-with-poetry-install] Error 1

link to the full script source

As soon as the source directory is removed. The CLI refuses to work any longer.

Trying with pip install

I've tried to replace the poetry install with something like poetry build && pip install dist/*.whl ( link to this script version )

With the version using pip install of the .whl file, I'm successfully creating a standalone deployment of my application. This is suitable to RPM packaging and could be deployed anywhere.

Software versions

+ python3.8 -V        
Python 3.8.13
          
+ poetry --version   
Poetry version 1.1.13

Final thoughts

I can't help but think that I'm misusing poetry here. So any help will be very much appreciated.

Thanks in advance

Regards

I'm late to the party, but I want to suggest a way to accomplish this. While poetry is amazing at managing your project's main and dev dependencies and locking their versions, I wouldn't rely on it while deploying on your situation. Here's a way to solve it:

# export your dependencies in the requirements.txt format using poetry
poetry export --without-hashes -f requirements.txt -o requirements.txt

# create your venv like you did on your example (you may want to upgrade pip/wheel/setuptools first)
python3 -m venv venv && . venv/bin/activate

# then install the dependencies
pip install --no-cache-dir -r requirements.txt

# then you install your own project
pip install .

There you have it, everything you need will be self-contained in the venv folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM