Textmate 执行python脚本的错误:env: python3: No such file or directory

在 macos 中使用 textmate 执行脚本如下:

#!/usr/bin/env python3
# -*- coding:utf-8 -*-


print("hello, world!!!")

报出错误:env: python3: No such file or directory

改为 env python 就可以顺利执行。

在 TextMate 的 Preferences -> variables 查看环境变量 PATH 的值为:

$PATH:/opt/local/bin:/usr/texbin:/usr/local/bin

在 MacOS 中,python3 的路径为 /usr/local/bin/python3,python 的路径为 /usr/bin/python

系统中的 PATH 环境变量为:

$ echo $PATH
/Users/guli/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

是没错的,包含了 python3 和 python 的路径,那为什么在 textmate 中只能调用 python 而不能调用 python3 呢?

然后搜索到一篇 textmate 的博客:大意是 textmate 没有继承 MacOS 的环境变量设置,因为它没有解析 shell 的启动脚本。所以如果执行的命令在默认 PATH 路径之外的话,需要在 textmate 的设置中添加命令的路径。

好了,那奇怪的点在于,textmate 路径也是对的。为啥还是不行呢。

试了试,有两种方法是可以的:

1,在 python 脚本改写第一行,直接写上python3的路径:

#!/usr/local/bin/python3
# -*- coding:utf-8 -*-

2,在 textmate 的 PATH 设置中,添加python3的命令路径:

$PATH:/opt/local/bin:/usr/texbin:/usr/local/bin:/usr/local/bin/python3

好了,最奇怪的事来了,我后来把这两种改动都还原了,但是也可以执行成功了。

但暂时就这样先,不再研究下去了,先用着,已经花了1个小时折腾这个事情了。

下面是参考的那篇textmate博客文档:

Defining a $PATH

http://blog.macromates.com/2014/defining-a-path/

Upcoming changes to OS X have caused us to make some changes in TextMate that have been causing some users to see a env: ruby18: No such file or directory error when running bundle items. This is caused by improper setup of the PATH variable, but lets go over what this variable does and how to correct the issue.

OS X comes with many command line tools that TextMate takes advantage of such as ruby, python, git, etc. To find these programs there is the environment variable PATH which is a list of paths that is used to find the required tools.

Note that while you may already have set the PATH variable for your shell (e.g. bash) it is not inherited in TextMate, as TextMate does not parse your shell setup scripts. For commands that require tools outside the default path list, like the LaTeX typeset command, we normally provide a list of common locations that TextMate should also search, so you normally should not need to change the PATH in TextMate.

To add to the PATH variable inside TextMate open Preferences and go to the Variables section and add a new item with the name of PATH. It is important that the value set augment the existing value instead of replacing it. To do this you should prefix the value with $PATH:. Example values for common uses:

MacPorts: $PATH:/opt/local/bin

Homebrew: $PATH:/usr/local/bin

This post relates to TextMate 2.0 only.

    原文作者:C86guli
    原文地址: https://www.jianshu.com/p/332ef255c827
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞