[英]How do I run poetry install in a subprocess without reference to the current virtualenv?
我正在用我们拥有的命令行工具编写一些自动设置代码。 此命令行工具随 poetry 一起安装,因此当有人调用命令行工具时,它来自 poetry 设置的虚拟环境。
我试图调用的命令是poetry install
for another directory 。 我希望此命令为其他目录创建一个新的 virtualenv。
我目前正在做的是
import subprocess
other_directory = "/some/path"
subprocess.run([f"cd {other_directory} && poetry install && poetry env info"], cwd=other_directory, shell=True, check=True)
但这总是将其他存储库中的依赖项安装到我用来运行此脚本的 virtualenv 中。 我也尝试过使用系统诗歌的路径而不是 virtualenv 中的路径,但这也不起作用。 poetry env info
命令总是输出当前 virtualenv 的信息:
Virtualenv
Python: 3.10.9
Implementation: CPython
Path: /home/veith/.cache/pypoetry/virtualenvs/my-cli-tool-Qe2jDmlM-py3.10
Executable: /home/veith/.cache/pypoetry/virtualenvs/my-cli-tool-Qe2jDmlM-py3.10/bin/python
Valid: True
如果我手动cd
到另一个目录并运行poetry env info
,它总是显示没有设置 virtualenv:
Virtualenv
Python: 3.10.9
Implementation: CPython
Path: NA
Executable: NA
我在这里寻找的 output 是:
Virtualenv
Python: 3.10.9
Implementation: CPython
Path: /home/veith/.cache/pypoetry/virtualenvs/my-other-directory-HASH-py3.10
Executable: /home/veith/.cache/pypoetry/virtualenvs/my-other-directory-HASH-py3.10/bin/python
Valid: True
有什么办法可以做到这一点? 如果我在子进程调用中设置cwd
关键字,我什至不明白诗歌从哪里知道当前的 virtualenv。
如果 Poetry 检测到当前环境是一个 venv,则它不会创建一个新的 venv。 这是通过查找VIRTUAL_ENV
环境变量来完成的。
如果您取消设置子流程的变量,poetry 会很乐意为您创建一个新的 venv。
例如:
import os
import subprocess
other_directory = "/some/path"
current_env = os.environ.copy()
if "VIRTUAL_ENV" in current_env:
del current_env["VIRTUAL_ENV"]
subprocess.run([f"cd {other_directory} && poetry install && poetry env info"], cwd=other_directory, shell=True, check=True, env=current_env)
问题未解决?试试以下方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.