Applescript 使用了错误的 python 版本,如何更改?
我最近在 OS X 上安装了 python 2.6.5 而不是 2.6.1。一切正常,除了当我尝试通过 Applescript 编辑器执行 python 脚本时。如果我执行以下行,我会得到版本 2.6.1:
Applescript:
do shell script "python -c \"from sys import version; print version\""
但是如果我在 OS X 终端中执行类似的行,我会得到版本 2.6.5:
OS X 终端:
python -c "from sys import version; print version"
可能出了什么问题?这可以很容易地解决吗?
I recently installed python 2.6.5 over 2.6.1 on OS X. Everything works fine except when I try executing python scripts via the Applescript editor. If I execute the following line I get version 2.6.1:
Applescript:
do shell script "python -c \"from sys import version; print version\""
But if I execute the similar line in the OS X terminal i get version 2.6.5:
OS X terminal:
python -c "from sys import version; print version"
What could be wrong? Can this somehow be easily fixed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不建议覆盖系统安装的
python
(或任何 UNIX 命令),因为某些系统应用程序可能依赖于安装的特定版本的python
的怪癖。如今,在完全不同的路径中安装另一个不同的
python
(或perl
或ruby
)副本已成为一种习惯。然后,在 bash 的情况下,在
.profile
等中适当设置路径,以便在命令行中选择您安装的python
。但请注意,GUI 应用程序从系统继承环境变量,并且不会读取
.profile
或其他内容。有一种方法可以更改 GUI 应用程序看到的PATH
环境变量(请参阅 此处),但我不推荐它。您在哪里安装了
python
2.6.5?说它在然后在你的AppleScript代码中你可以说
I won't recommend overwriting system-installed
python
(or whatever UNIX commands,) because some of the system app might depend on the quirk of a particular version ofpython
installed.It's quite customary these days to install another distinct copy of
python
(orperl
orruby
) in an entirely different path.Then you set up the path appropriately in
.profile
etc. in the case of bash so that in the command linepython
you installed will be chosen.But note that the GUI app inherits the environment variables from the system and don't read
.profile
or whatever. There's a way to changePATH
environment variable seen by the GUI apps (see here), but I don't recommend it.Where did you install your
python
2.6.5? Say it's inThen in your AppleScript code you can just say
Python 2.6.5 安装程序会修改您的
~/.bash_profile
文件,将 Python 2.6.5 的bin
目录放在$PATH
的开头,在/usr/bin
之前,这是操作系统默认的 Python 解释器所在的位置。当您通过终端登录时,会自动读取该配置文件,但使用do shell script
时则不会。您要么需要指定所需的python
可执行文件的完整路径(可能是最好的选择),或者您可以让脚本预先读取 bash 配置文件(尽管请注意do shell script
使用sh
,而不是bash
):The Python 2.6.5 installer modifies your
~/.bash_profile
file to put Python 2.6.5'sbin
directory at the start of your$PATH
, ahead of/usr/bin
which is where the OS's default Python interpreter lies. That profile is automatically read when you log in via Terminal, but not when usingdo shell script
. You either need to specify the full path to thepython
executable you want (probably the best option), or you could have your script read the bash profile beforehand (though be aware thatdo shell script
usessh
, notbash
):Mac 默认安装将 python 放置在 /usr/bin 中,python 安装程序将其放置在带有符号链接的框架中 /usr/local/bin 中。只需删除 /usr/bin 中的旧版本并创建新的符号链接
The mac default install places python in /usr/bin, the python installer places it in frameworks with sym links in /usr/local/bin. Just remove the old version in /usr/bin and create new sym links