如何在 Mac OS X 中从终端运行我的 python 脚本而不必输入完整路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4718071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:10:30  来源:igfitidea点击:

How can I run my python script from the terminal in Mac OS X without having to type the full path?

pythonmacospathterminal

提问by tobuslieven

I'm on Mac OS 10.6 Snow Leopard and I'm trying to add a directory to my PATH variable so I can run a tiny script I wrote by just typing: python alarm.py at the terminal prompt.

我在 Mac OS 10.6 Snow Leopard 上,我正在尝试向我的 PATH 变量添加一个目录,这样我就可以运行我编写的一个小脚本,只需在终端提示符下键入:python alarm.py。

I put the path in my .profile file and it seems to show up when I echo $PATH, but python still can't find script the that I've put in that directory.

我把路径放在我的 .profile 文件中,当我回显 $PATH 时它似乎出现了,但是 python 仍然找不到我放在那个目录中的脚本。

Here's the contents of my .profile file in my home directory:

这是我的主目录中的 .profile 文件的内容:

~ toby$ vim .profile 
export PATH=/Users/tobylieven/Documents/my_scripts:$PATH

Here's the output of echo $PATH, where all seems well:

这是 echo $PATH 的输出,看起来一切正常:

~ toby$ echo $PATH
/Users/tobylieven/Documents/my_scripts:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

Here's the script I'm trying to run:

这是我试图运行的脚本:

~ toby$ ls /Users/tobylieven/Documents/my_scripts 
-rwxrwxrwx@ 1 tobylieven  staff  276 17 Jan 21:17 alarm.py

Here's the command I'm trying to use to run the script and the fail message I'm getting instead:

这是我试图用来运行脚本的命令和我收到的失败消息:

~ toby$ python alarm.py 
python: can't open file 'alarm.py': [Errno 2] No such file or directory

If anyone has an idea what I might be doing wrong, that'd be great. Thanks a lot.

如果有人知道我可能做错了什么,那就太好了。非常感谢。

采纳答案by phihag

PATH is only for executables, not for python scripts. Add the following to the beginning of your Python script:

PATH 仅适用于可执行文件,不适用于 Python 脚本。将以下内容添加到 Python 脚本的开头:

#!/usr/bin/env python

and run

并运行

sudo chmod a+x /Users/tobylieven/Documents/my_scripts/alarm.py

Then, you can type just alarm.pyto execute your program.

然后,您可以键入只是alarm.py为了执行您的程序。

回答by David

You need to modify the Python specific path variable: PYTHONPATH.

您需要修改 Python 特定的路径变量:PYTHONPATH。

So:

所以:

export PYTHONPATH=/Users/tobylieven/Documents/my_scripts

should get you working.

应该让你工作。

See: Python Module Search path

请参阅:Python 模块搜索路径

回答by Matt Crinklaw-Vogt

change alarm.py to include:

更改 alarm.py 以包括:

#!/bin/python

as the very first line in the file.

作为文件中的第一行。

(or /usr/bin/python, depending on where you python interpreter is located. You can figure this out by typing: which pythonin the terminal.)

(或 /usr/bin/python,取决于你的 python 解释器所在的位置。你可以通过which python在终端中输入:来解决这个问题。)

You can then just run alarm.pyinstead of python alarm.py.

然后你可以直接运行alarm.py而不是python alarm.py.

e.g.:

例如:

~ toby$ alarm.py  

And phihag who beat me by a few seconds is right, you need to add execute permissions (via chmod) to alarm.py.

比我领先几秒的phihag是对的,你需要给alarm.py添加执行权限(通过chmod)。

回答by John Ballinger

Something of interest that I really struggled with on OS X coming from Window, is that you its very hard to get the directory of your current script.

我在来自 Window 的 OS X 上真正挣扎的有趣之处在于,您很难获得当前脚本的目录。

I found this.

我找到了这个。

#! /bin/zsh 
cd "${0:h}"  

Now you could execute a python file relative to the executed script instead of having to know the exact path where your python file is. This might or might not help but I use this a lot to make my scripts and .command files work better.

现在您可以相对于执行的脚本执行一个 python 文件,而不必知道您的 python 文件所在的确切路径。这可能有帮助,也可能无济于事,但我经常使用它来使我的脚本和 .command 文件更好地工作。

回答by jasonleonhard

Which python are you targeting?

你的目标是哪条蟒蛇?

Did you install it with brew? It uses a different path.

你是用brew安装的吗?它使用不同的路径。

which python3or which python

which python3或者 which python

Choose the one you want

选择你想要的

Copy that output

复制那个输出

Paste it at the top of your python file

将它粘贴到你的 python 文件的顶部

add a #!in front of that path so it looks something like

#!在该路径前面添加一个,使其看起来像

#!/usr/local/bin/python3

#!/usr/local/bin/python3

Make sure to change the file permissions

确保更改文件权限

chmod +x filename

chmod +x filename

Put that file in a folder that is in your path

将该文件放在您路径中的文件夹中

Not sure if your folder is in your path?

不确定您的文件夹是否在您的路径中?

echo $path

echo $path

How to add that folder to your path?

如何将该文件夹添加到您的路径?

Find your path first

先找到你的路

echo $HOME

echo $HOME

If you are using bash or zsh you might have something like this

如果您使用的是 bash 或 zsh,您可能会遇到这样的情况

In ~/.bash_profileor ~/.bashrcor ~/.zshrcat the bottom of your file

~/.bash_profile~/.bashrc~/.zshrc在你的文件底部

export PYTHON_UTILS="$HOME/code/python/utils"

export PYTHON_UTILS="$HOME/code/python/utils"

export PATH="$PYTHON_UTILS:$PATH"

export PATH="$PYTHON_UTILS:$PATH"

Consider removing the .pyfrom your file bc it is not needed in this case

考虑.py从您的文件中删除bc 在这种情况下不需要

Close and open your terminal, which is sourcing your file by its path

关闭并打开您的终端,该终端通过其路径获取您的文件

And now you should be able to treat your python file similar to a bash command

现在你应该能够像对待 bash 命令一样对待你的 python 文件

You don't need to use python3 filename.pyto run the file, you can just use filename

你不需要使用python3 filename.py来运行文件,你可以使用filename

From anywhere on your filesystem!

从文件系统上的任何地方!