Python 子进程未导航 venv 文件夹



我正在尝试自动化我在Python中设置venv环境时经常经历的一些步骤。我能够到达创建文件夹的位置,并在其中包含我想要的任何模块的requirements.txt文件夹。但似乎会话不记得最后一个命令,阻止我激活venv并在之后安装 requirements.txt 文件。

# Navigate to new venv directory
subprocess.run("cd " + destination, shell=True)
# Activate venv environment.
subprocess.run("Scripts\activate.bat", shell=True)
# Install requirements with pip.
subprocess.run("pip install -r requirements.txt", shell=True)

输出:

The system cannot find the path specified.

Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'

您应该

导航到有要求的文件夹.txt激活 env 后,此文件肯定不在环境文件夹中。

我没有导航到该文件夹并尝试使用 pip 进行安装,而是在 venv Scripts 文件夹中调用了pip并安装了在写入 requirements.txt 时已经附加到列表中的软件包。

# Install requirements to venv.
subprocess.run([os.path.join(destination, 'Scripts', 'pip.exe')] + 'install {}'.format(' '.join(requirements)).split())

最新更新