gpt4 book ai didi

python - 刚开始编写 Fabric 文件来安装包

转载 作者:行者123 更新时间:2023-12-01 05:59:01 25 4
gpt4 key购买 nike

from fabric.api import sudo, put, run

def install():
run('source /home/user/.virtualenvs/demo/bin/activate')
run('pip install requests')

if __name__ == '__main__':
install()
# to run this, do fab fabfile

Q1。我想在那个环境中安装 pip 。但脚本运行后,requests 并未安装在 virtualenv 中。为什么?

第二季度。要求我提供主机信息(以下也是Q1的日志)。如果我的目标是在本地运行,是否有更好的方法来处理这个问题?

No hosts found. Please specify (single) host string for connection: localhost
[localhost] run: source /home/user/.virtualenvs/demo/bin/activate
[localhost] Passphrase for private key:
[localhost] Login password:
[localhost] run: pip install requests
[localhost] out: Requirement already satisfied (use --upgrade to upgrade): requests in /usr/local/lib/python2.7/dist-packages
[localhost] out: Requirement already satisfied (use --upgrade to upgrade): certifi>=0.0.7 in /usr/local/lib/python2.7/dist-packages (from requests)
[localhost] out: Requirement already satisfied (use --upgrade to upgrade): oauthlib>=0.1.0,<0.2.0 in /usr/local/lib/python2.7/dist-packages (from requests)
[localhost] out: Requirement already satisfied (use --upgrade to upgrade): chardet>=1.0.0 in /usr/lib/python2.7/dist-packages (from requests)
[localhost] out: Requirement already satisfied (use --upgrade to upgrade): rsa in /usr/local/lib/python2.7/dist-packages (from oauthlib>=0.1.0,<0.2.0->requests)
[localhost] out: Requirement already satisfied (use --upgrade to upgrade): pyasn1>=0.0.13 in /usr/local/lib/python2.7/dist-packages (from rsa->oauthlib>=0.1.0,<0.2.0->requests)
[localhost] out: Cleaning up...
[localhost] out:
<小时/>

更新

我可以通过一个 run 命令安装这些软件包。有更好的方法吗?

谢谢。

<小时/>

这是更新代码:

from fabric.api import sudo, put, run
from fabric.context_managers import prefix

def install():
with prefix('source /home/user/.virtualenvs/demo/bin/activate'):
run('pip install requests')

if __name__ == '__main__':
install()

http://docs.fabfile.org/en/1.2.0/api/core/context_managers.html?highlight=prefix#fabric.context_managers.prefix

最佳答案

每个fabric.run命令都会生成一个单独的子shell,在其中执行该命令。

如果您在其中一个运行命令中激活 virtualenv,则该环境不可用于单独调用运行命令中的后续命令执行。

请参阅 stackoverflow 上的以下讨论以获取正确的解决方案:

检查 contextmanager 的用法,以在激活 virtualenv 的情况下运行命令。

关于python - 刚开始编写 Fabric 文件来安装包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11351734/

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