【问题标题】:How to build properly an alias?如何正确构建别名?
【发布时间】:2013-10-28 23:30:49
【问题描述】:

我在.bash_profile 中添加了一个新别名python3.3,以便轻松启动python3.3 版本的pyzo

我可以在终端中毫无问题地使用此别名,但是当我使用 subprocess.check_call(args = ["python3.3", onePyFile]) 之类的名称时,出现以下错误。

Traceback (most recent call last):
  ...
  File "/Library/Frameworks/pyzo2013b/lib/python3.3/subprocess.py", line 540, in check_call
    retcode = call(*popenargs, **kwargs)
  File "/Library/Frameworks/pyzo2013b/lib/python3.3/subprocess.py", line 521, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/Library/Frameworks/pyzo2013b/lib/python3.3/subprocess.py", line 818, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/pyzo2013b/lib/python3.3/subprocess.py", line 1416, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'python3.3'

我猜我的别名不是到处都能看到的。那么我该如何解决我的问题呢?建立自己的别名的好方法是什么?

如果我尝试subprocess.check_call(args = ["python3.3", onePyFile], shell = True),则会出现以下错误。

onePyFile.py: python3.3: command not found
Traceback (most recent call last):
  File "mainFile.py", line 72, in <module>
    shell = True
  File "/Library/Frameworks/pyzo2013b/lib/python3.3/subprocess.py", line 545, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['python3.3', 'onePyFile.py']' returned non-zero exit status 127

如果我只使用subprocess.check_call(args = ["python3.3", onePyFile]),其中onePyFile 的第一行是#! /usr/bin/env python3.3,则会出现以下错误。

env: python3.3: No such file or directory
Traceback (most recent call last):
  ...

我认为我的问题更多是关于符号链接而不是 Python 调用。但我不知道出了什么问题。事实上,这是我第一次使用别名创建个人符号链接。

【问题讨论】:

  • 如果您是这样想的,请摆脱符号链接并使用 python 3.3 的完整路径。您可能还需要 onePyFile 的完整路径。
  • 我知道我可以做到,但我也想知道如何解决别名问题。

标签: python unix subprocess


【解决方案1】:

试试subprocess.check_call(args = ["python3.3", onePyFile] , shell=True, env={'ENV':path_of_bash_profile})

如果 shell 为 True,指定的命令将通过 shell 执行。如果您将 Python 主要用于它在大多数系统 shell 上提供的增强控制流,并且仍然希望方便地访问其他 shell 功能,例如 shell 管道、文件名通配符、环境变量扩展和 ~ 到用户家的扩展,这将很有用目录。但是,请注意 Python 本身提供了许多类似 shell 的功能的实现(特别是 glob、fnmatch、os.walk()、os.path.expandvars()、os.path.expanduser() 和 shutil)。

【讨论】:

  • 我试过了,但还是有一个错误。我将把它放在我的问题中。
  • @projetmbc 尝试 subprocess.check_call(args = ["python3.3", onePyFile] , shell=True,env={'ENV':absolute_path_of_bash_profile})
  • 这不起作用。真是一团糟……我快淹死了……;-)
  • 在我更新的问题的末尾给出了错误。
【解决方案2】:

这是因为默认情况下 subprocess 不会加载 shell(请参阅 the docs),因此它不会获取 .bash_profile 中的内容。 使用这个:

subprocess.check_call(args = ["python3.3", onePyFile], shell=True)

编辑:似乎 glasslion 比我快!

编辑2:我做了更多的挖掘,发现了一些奇怪的东西。因为 shell=True 似乎没有按预期工作,所以我采用了更直接的方法,直接调用 bash。

a.py:

from subprocess import check_call
check_call(['bash', '-c', '. ~/.bash_profile && ABC a bc'])

a.sh(你的 python3 可执行文件是什么):

echo $*

我第一次尝试在.bash_profile 中使用alias ABC='~/Documents/a.sh'

$ python ~/Documents/a.py
bash: ABC: command not found
Traceback (most recent call last):
  File "Documents/a.py", line 6, in <module>
    check_call(['bash', '-c', '. ' + expanduser('~/.bash_profile') + ' && ABC a bc'])
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 511, in check_call
subprocess.CalledProcessError: Command '['bash', '-c', '. ~/.bash_profile && ABC a bc']' returned non-zero exit status 127

然后我从别名切换到函数:ABC() { ~/Documents/a.sh $*; } 它奏效了!

$ python ~/Documents/a.py
a bc

底线是我让它工作了,但我不知道为什么! shell 不可靠,所以最好是跳过 shell。

我们可以巧妙地使用 shebang 原则(正如 Fred Mitchell 建议的那样):

from subprocess import check_call
check_call(['/usr/bin/env', 'python3', onePyFile])

如果 Python 3 安装正确,这将起作用,独立于它的路径(我想你想要实现的)。

【讨论】:

  • 使用一个函数来管理你的“部署”非常方便。它避免了是否使用您的 shell 配置文件以及您可能想要哪个 shell 的问题。有时您可能会发现使用完整路径很有用,有时使用相对路径。如果一切都是相对于“基本路径”的,那么很容易修改,或者“基本路径”可以在运行时传入。
【解决方案3】:

为什么不在 onePyFile 的第一行给 python3.3 加上一个 shebang 并省略显式运行 python?

【讨论】:

  • 另一种方法是直接调用/usr/bin/env python3.3,它会跳过shebang并且不需要修改任何东西!
  • 这可以完成这项工作,但如果我使用其他别名,我将不得不解决这个问题。
  • 没什么,这就是问题所在。我的别名只“存在”在终端中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-31
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
  • 2020-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多