为什么 Fabric 看不到我的 .bash_profile?
在 Fabric 中,当我尝试使用 .bash_profile
文件中的任何别名或函数时,它们无法被识别。例如,我的 .bash_profile
包含 alias c='workon django-canada'
,因此当我在 iTerm 或终端中输入 c
时,workon django-canada
被执行。
我的 fabfile.py 包含
def test():
local('c')
但是当我尝试 fab test 时它向我抛出这个: [localhost] local: c
/bin/sh: c: command not found
Fatal error: local() encountered an error (return code 127) while executing 'c'
Aborting.
其他 Fabric 功能工作正常。我是否必须在 Fabric 中的某个位置指定我的 bash 配置文件?
In Fabric, when I try to use any alias' or functions from my .bash_profile
file, they are not recognized. For instance my .bash_profile
contains alias c='workon django-canada'
, so when I type c
in iTerm or Terminal, workon django-canada
is executed.
My fabfile.py
contains
def test():
local('c')
But when I try fab test
it throws this at me:
[localhost] local: c
/bin/sh: c: command not found
Fatal error: local() encountered an error (return code 127) while executing 'c'
Aborting.
Other Fabric functions work fine. Do I have to specify my bash profile somewhere in fabric?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑 - 事实证明,这个问题已在 Fabric 1.4.4 中修复。从变更日志:
因此,原来的问题将这样解决:
我在下面留下了原来的答案,该答案仅与 Fabric 版本 < 相关。 1.4.4.
因为本地不使用bash。您可以在输出中清楚地看到它,
看到了吗?它使用
/bin/sh
而不是/bin/bash
。这是因为 Fabric 的local
命令的内部行为与run
略有不同。local
命令本质上是subprocess.Popen
python 类的包装器。http://docs.python.org/library/subprocess.html#popen-constuctor
并且这是你的问题。 Popen 默认为
/bin/sh
。如果您自己调用 Popen 构造函数,但您是通过 Fabric 使用它,则可以指定不同的 shell。不幸的是,Fabric 无法为您提供传递 shell 的方法,例如/bin/bash
。抱歉,这不能为您提供解决方案,但它应该可以回答您的问题。
编辑
这里是有问题的代码,直接从
operations.py
文件中定义的fabric的local
函数中提取:如您所见,它不会为可执行文件传递任何内容关键词。这会导致它使用默认值,即 /bin/sh。如果它使用 bash,它会看起来像这样:
但事实并非如此。这就是为什么他们在本地文档中说了以下内容:
EDIT - As it turns out, this was fixed in Fabric 1.4.4. From the changelog:
So the original question would be fixed like this:
I've left my original answer below, which only relates to Fabric version < 1.4.4.
Because local doesn't use bash. You can see it clearly in your output
See? It's using
/bin/sh
instead of/bin/bash
. This is because Fabric'slocal
command behaves a little differently internally thanrun
. Thelocal
command is essentially a wrapper around thesubprocess.Popen
python class.http://docs.python.org/library/subprocess.html#popen-constuctor
And here's your problem. Popen defaults to
/bin/sh
. It's possible to specify a different shell if you are calling the Popen constructor yourself, but you're using it through Fabric. And unfortunately for you, Fabric gives you no means to pass in a shell, like/bin/bash
.Sorry that doesn't offer you a solution, but it should answer your question.
EDIT
Here is the code in question, pulled directly from fabric's
local
function defined in theoperations.py
file:As you can see, it does NOT pass in anything for the executable keyword. This causes it to use the default, which is /bin/sh. If it used bash, it'd look like this:
But it doesn't. Which is why they say the following in the documentation for local:
一种解决方法就是将您拥有的任何命令包装在 bash 命令周围:
如果您需要执行大量这些操作,请考虑创建一个 自定义上下文经理。
One workaround is simply to wrap whatever command you have around a bash command:
If you need to do a lot of these, consider creating a custom context manager.
看起来您正在尝试在本地使用 virtualenvwrapper 。您需要使本地命令字符串如下所示:
这是您的一个真正的示例它在上下文管理器中为您执行此操作< /a>.
It looks like you're trying to use virtualenvwrapper locally. You'll need to make your local command string look like this:
Here's an example by yours truly that does that for you in a context manager.