gpt4 book ai didi

python - 为什么 venv 在 rsync 后中断?

转载 作者:行者123 更新时间:2023-11-28 16:56:56 27 4
gpt4 key购买 nike

我正在尝试为我的 Python 脚本和应用程序构建 CI/CD 过程。我能够在测试容器中构建我的 venv,但是当我将它 rsync 到目标服务器时,Python 版本似乎中断了。这就是我正在尝试的:

- cp -a ./. $APP_DIR
- cd $APP_DIR
- python3 -m venv venv
- source venv/bin/activate
- pip3 install -r requirements.txt
...
- rsync...

所有涉及的环境都运行 Python 3.6.8

当我在目标服务器上激活 venv 并运行 which python3 时,我得到了不正确的 /usr/bin/python3

为什么?为什么 venv 在通过 rsync 部署到服务器时会中断?

我是 Python 开发和虚拟环境过程的新手。 venv 是否应该只在它们需要运行的服务器(或容器)上创建?有时我的目标服务器上没有安装 python3-venv。是否可以使用代码部署 venv 并使用它来运行我的脚本?

最佳答案

当通过venv创建环境时,它将环境路径的绝对路径存储到bin/activate中。此外,在指向现有 python 安装的新环境中创建了一些符号链接(symbolic link)。

因此,环境仅在主机上有效路径venv被执行。文档中也有说明(部分省略):

Running this command creates the target directory [...] and places a pyvenv.cfg file in it with a home key pointing to the Python installation from which the command was run. It also creates a bin [...] subdirectory containing a copy/symlink of the Python binary/binaries (as appropriate for the platform or arguments used at environment creation time).

您可以通过以下命令轻松检查此事实:

mkdir /tmp/example_dir_for_stackoverflow
cd /tmp/example_dir_for_stackoverflow
python3 -m venv venv
grep stackoverflow venv/bin/activate

它会输出:

VIRTUAL_ENV="/tmp/example_dir_for_stackoverflow/venv"

如果你将这个环境同步到另一个系统到不同的路径和/或不同的 python 安装,bin/activate 中的设置不匹配并且它不起作用。

关于python - 为什么 venv 在 rsync 后中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57483910/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com