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

问题描述 投票:0回答:1

我最近迁移到

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

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

[...]
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

链接到完整的脚本源

一旦删除源目录。 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

最后的想法

我忍不住认为我在这里误用了

poetry
。因此,我们将非常感谢任何帮助。

提前致谢

问候

python virtualenv python-poetry
1个回答
9
投票

我参加聚会迟到了,但我想建议一种方法来实现这一目标。 虽然

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 --no-deps -r requirements.txt

# then you install your own project
pip install .

好了,您需要的所有内容都将独立包含在

venv
文件夹中。

© www.soinside.com 2019 - 2024. All rights reserved.