如何使用 python诗以独立的方式将包安装到 virtualenv ?

use*_*077 4 python virtualenv python-poetry

我最近迁移到poetry我的依赖项管理,所以如果我的问题超出了poetry这里的范围,请原谅。

最终目标

我的最终目标是创建一个RPM 包,其中包含一个virtualenv,其中安装了我的软件及其所有依赖项。然后,该 RPM 将提供与安装该软件的系统隔离的软件。

重现问题

我在使用poetry install我的virtualenv. 一旦我的软件的源目录被删除,我的CLI就无法再工作。

复制

我创建了一个简单的存储库来重现问题:https ://github.com/riton/python-poetry-venv

这是我正在使用的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
Run Code Online (Sandbox Code Playgroud)

上面的脚本失败并出现以下错误:

[...]
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
Run Code Online (Sandbox Code Playgroud)

链接到完整的脚本源

一旦源目录被删除。CLI 拒绝再工作。

尝试与pip install

我尝试poetry install用类似的东西替换poetry build && pip install dist/*.whl链接到此脚本版本

pip install通过使用该文件的版本.whl,我成功创建了应用程序的独立部署。这适用于 RPM 打包,并且可以部署在任何地方。

软件版本

+ python3.8 -V        
Python 3.8.13
          
+ poetry --version   
Poetry version 1.1.13
Run Code Online (Sandbox Code Playgroud)

最后的想法

我忍不住认为我poetry在这里滥用了。因此,我们将非常感谢任何帮助。

提前致谢

问候

luk*_*ris 8

我参加聚会迟到了,但我想建议一种方法来实现这一目标。虽然poetry在管理项目的主要和开发依赖项并锁定其版本方面非常出色,但在根据您的情况进行部署时我不会依赖它。这是解决它的方法:

# 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 .
Run Code Online (Sandbox Code Playgroud)

好了,您需要的所有内容都将独立包含在该venv文件夹中