运行后出现`pip: error: No command by the name pip install -r requirements.txt`

2 投票
1 回答
1530 浏览
提问于 2025-04-17 07:37

我正在尝试创建一个叫做 bootstrap.py 的脚本,这个脚本会创建一个虚拟环境,并从 requirements.txt 文件中安装所需的包。我的项目其他贡献者应该能够从 GitHub 上下载这个项目,然后运行 python bootstrap.py,接着运行 source env/bin/activate,这样就能顺利安装我的应用程序。下面是我写的脚本,我是参考这个页面的: http://pypi.python.org/pypi/virtualenv

import virtualenv, textwrap
output = virtualenv.create_bootstrap_script(textwrap.dedent("""
def after_install(options, home_dir):
    if sys.platform == 'win32':
        bin = 'Scripts'
    else:
        bin = 'bin'

    subprocess.call([join(home_dir,bin,'pip'),'install -r requirements.txt'])

"""))
print output

下面是我用来创建 bootstrap 并运行它的命令:

python create_bootstrap.py > bootstrap.py
python bootstrap.py env

下面是输出结果:

New python executable in env/bin/python
Installing setuptools............done.
Installing pip...............done.
Usage: pip COMMAND [OPTIONS]

pip: error: No command by the name pip install -r requirements.txt
  (maybe you meant "pip install install -r requirements.txt")

requirements.txt 文件的内容如下:

sqlalchemy==0.7

如果有其他的建议或者对我做错的地方有什么提示,那就太好了。非常感谢!

1 个回答

3

subprocess.call([join(home_dir,bin,'pip'),'install -r requirements.txt'])

'install -r requirements.txt' 被当作一个包含空格的单一参数来处理,所以子进程模块把它理解为调用 pip 'install -r requirements.txt'

你可以通过单独指定每个参数来解决这个问题:

subprocess.call([join(home_dir,bin,'pip'), 'install', '-r', 'requirements.txt'])

撰写回答