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

发布于 2025-01-19 08:29:04 字数 2839 浏览 1 评论 0原文

我最近迁移到诗歌我的依赖关系管理,因此如果我的问题不在诗歌的范围之内,请原谅。

最终目标

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

在我的诗歌> virtualenv中使用诗歌安装时,重现

我面临的问题。一旦删除了我的软件的源目录,我的 cli 就拒绝工作。

重现

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

这是我使用诗歌

#!/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

链接到完整脚本源

source Directory 被删除。 CLI拒绝工作。

尝试使用pip安装

我尝试用诗歌构建&amp;&amp;&amp;&amp; pip install dist/*。whl链接到此脚本版本),

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

软件版本的

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

最终想法

我不禁认为我在这里滥用诗歌。因此,任何帮助将不胜感激。

提前

感谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饭团 2025-01-26 08:29:04

我参加聚会迟到了,但我想提出一种实现这一目标的方法。
诗歌在管理项目的主要和开发依赖项并锁定其版本方面令人惊讶,但在部署您的情况时,我不会依靠它。这是一种解决它的方法:

# 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文件夹中独立。

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 --no-deps -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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文