【问题标题】:Multi commands with python使用python的多命令
【发布时间】:2020-04-01 02:54:14
【问题描述】:

我想用 python 激活虚拟环境,然后运行一些命令,例如 (pip install requirements.txt)。我想用 python 运行以下命令。我怎样才能做到这一点。谢谢...

C:\directory\myfolder\venv\Scripts\activate
(venv) C:\directory\myfolder> pip install requirements.txt

例如,我尝试了以下代码;

import subprocess
commands = ['C:\directory\myfolder\venv\Scripts\activate', 'pip install requirements.txt']
procs = [subprocess.Popen(i, shell=True) for i in commands]
for p in procs:
    p.wait()

输出:错误:无法打开需求文件:[Errno 2] 没有这样的文件或目录:'requirements.txt'

【问题讨论】:

  • 在你的shell例子中,工作目录是C:\directory\myfolder;当您运行 Python 脚本时,情况似乎并非如此。
  • 还有一个问题是你在一个 shell 中执行,而不是采购,activate 在你执行后立即退出。

标签: python bash shell subprocess virtualenv


【解决方案1】:

这对我有用:

$ pip install -r requirements.txt --no-index `--find-links` file:///tmp/packages

--no-index - 忽略包索引(只查看 --find-links URL)。

-f, --find-links <URL> - 如果是 html 文件的 URL 或路径,则解析指向档案的链接。如果本地路径或 file:// URL 是目录,则在目录列表中查找存档。

或者试试这个

首先,创建一个虚拟环境

在 python 3.6 中

virtualenv --python=/usr/bin/python3.6 <path/to/new/virtualenv/>

在 python 2.7 中

virtualenv --python=/usr/bin/python2.7 <path/to/new/virtualenv/>

然后安装 requirements.txt 文件中所有可用的包。

pip install -r <path/to/the/> requirement.txt

【讨论】:

    猜你喜欢
    • 2012-12-25
    • 2013-11-11
    • 1970-01-01
    • 2018-12-09
    • 2015-06-17
    • 1970-01-01
    • 2011-07-24
    • 2022-01-10
    • 1970-01-01
    相关资源
    最近更新 更多