macos 终端中的 Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3158291/
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-10-21 07:02:20  来源:igfitidea点击:

Python in terminal

pythonmacosterminal

提问by Paul Patterson

This question concerns running python files in terminal that are not stored in the home directory. I think I have solved the first bit of this puzzle by modifying my path so that it includes the directory where my python programs are stored.

这个问题涉及在终端中运行未存储在主目录中的 python 文件。我想我已经通过修改我的路径解决了这个难题的第一部分,以便它包含存储我的 python 程序的目录。

So where as initially echo $PATHwould yield the following: /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

因此,最初 echo $PATH会产生以下结果:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

it now yields: /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/Users/paulpatterson/Documents/Python

它现在产生: /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/Users/paulpatterson/Documents/Python

However despite the fact that the correct folder is now in my path, none of the python files within this folder run. For example there is a file in there called recap.py, when I open terminal and type in: python recap.pyI get:

然而,尽管正确的文件夹现在在我的路径中,但该文件夹中的任何 python 文件都没有运行。例如,那里有一个名为 recap.py 的文件,当我打开终端并输入: python recap.py我得到:

python: can't open file 'recap.py': [Errno 2] No such file or directory

If I simply type in recap.py(i.e. omitting the 'python' bit), I get: -bash: /Users/paulpatterson/Documents/Python/recap.py: Permission denied

如果我只是输入recap.py(即省略“python”位),我会得到: -bash: /Users/paulpatterson/Documents/Python/recap.py: Permission denied

Can anyone enlighten me? Ideally I want to set it up so as soon as terminal opens all I need to do is type the file name and not even type python.

任何人都可以启发我吗?理想情况下,我想设置它,以便一旦终端打开,我需要做的就是输入文件名,甚至不用输入 python。

I've spent hours trying to sort this out, any help is appreciated.

我花了几个小时试图解决这个问题,任何帮助表示赞赏。

Paul.

保罗。

回答by dreeves

Including the directory where a command lives in your $PATH means you can run commands in that directory from anywhere. But in your first example, you are running the command "python" with recap.py as an argument. So your shell does not search your $PATH to find where recap.py lives. To make recap.py runnable as a command by itself, see this:

在 $PATH 中包含命令所在的目录意味着您可以从任何地方运行该目录中的命令。但是在您的第一个示例中,您使用 recap.py 作为参数运行命令“python”。所以你的 shell 不会搜索你的 $PATH 来找到 recap.py 所在的位置。要使 recap.py 本身可以作为命令运行,请参阅:

http://en.wikipedia.org/wiki/Shebang_(Unix)

http://en.wikipedia.org/wiki/Shebang_(Unix)

In short, you need to include #!/usr/bin/env pythonas the first line, and chmod the file to be executable (chmod u+x recap.py).

简而言之,您需要将包含#!/usr/bin/env python作为第一行,并将文件 chmod 为可执行文件 ( chmod u+x recap.py)。

回答by Wilduck

You may have tried this before, but I ran into similar problems at one point and this is the process that works for me. In the directory where recap.py is stored:

您之前可能已经尝试过这个,但我曾经遇到过类似的问题,这是对我有用的过程。在存储 recap.py 的目录中:

chmod +x recap.py
./recap.py

The ./ being the key part, as it works as the full path to the directory. This also assumes that you have

./ 是关键部分,因为它用作目录的完整路径。这也假设你有

#!/usr/bin/env python

or something similar as the first line of your program. The first two characters first line is called a shebangand indicates that the file is a script to be executed by the interpreter specified by the path following it. So, as others have suggested, your python interpreter may be located somewhere other than /usr/bin (possibly in /bin), so you'll need to find this, and include this line at the top of any python script you wish to execute from the terminal.

或类似于程序第一行的内容。第一行的前两个字符称为shebang,表示该文件是由其后的路径指定的解释器执行的脚本。因此,正如其他人所建议的,您的 Python 解释器可能位于 /usr/bin 以外的其他位置(可能在 /bin 中),因此您需要找到它,并在您希望添加的任何 Python 脚本的顶部包含这一行从终端执行。

If you don't know where python is located, you can use:

如果你不知道 python 所在的位置,你可以使用:

which python

at the terminal, which should print the path to your python install. You can then use that path after your shebang.

在终端,它应该打印你的 python 安装路径。然后,您可以在 shebang 之后使用该路径。

回答by mikerobi

Make sure the first line in every script is "#!/usr/bin/env python" (no qoutes). Do "chmod +x file.py" for every file. You should then be able to run each script as file.py.

确保每个脚本的第一行是“#!/usr/bin/env python”(没有 qoutes)。对每个文件执行“chmod +x file.py”。然后,您应该能够将每个脚本作为 file.py 运行。

回答by Daenyth

The other responders have done a good job with your initial question, but I think you would be very well served to actually learn how to use the command line, as your replies have shown that you're not familiar with it yet. I always recommend this. It's a fantastic guide that will get you comfortable using it.

其他回复者在您最初的问题上做得很好,但我认为您实际上学习如何使用命令行会非常有用,因为您的回复表明您还不熟悉它。我总是推荐这个。这是一个很棒的指南,可以让您舒适地使用它。

回答by Paul

Type in:

输入:

which python 

into terminal and that should get you the path to python. Put that on top of your script as others have suggested:

进入终端,这应该会让你找到 python 的路径。正如其他人所建议的那样,将其放在您的脚本之上:

#! /path/to/python

Also make sure it is executable (the whole chmod stuff). You can check this by typing:

还要确保它是可执行的(整个 chmod 的东西)。您可以通过键入以下内容进行检查:

ls -l

The file should then have something like -rwx-r--r-- next to it. The x means it is executable.

该文件旁边应该有类似 -rwx-r--r-- 的内容。x 表示它是可执行的。

回答by freegnu

Try typing:

尝试输入:

which env

to find out what the path of env is on your system. Change the shebang path to match the full path of the env command found by using the which command. If that fails. It means you either don't have env installed or env is installed in a system path. Try specifying the full path to the python executable instead of using env by typing:

找出系统上 env 的路径。更改shebang 路径以匹配使用which 命令找到的env 命令的完整路径。如果那失败了。这意味着您没有安装 env 或 env 安装在系统路径中。尝试通过键入以下命令来指定 python 可执行文件的完整路径,而不是使用 env:

which python

And using the full path return in place of the /path/to/env python shebang. The first line of your recap.py should look something like this:

并使用完整路径返回代替 /path/to/env python shebang。recap.py 的第一行应该是这样的:

#!/path/to/python

回答by Mary Martinez

I don't know how useful this is to you now seeing as it is a few years later, but I've been struggling with a very similar issue for a good hour now. I am using the Python IDLE to save my code as a .py file. When I was trying to run it in terminal I kept getting a syntax error in the first line. My first line of code was

我不知道这对你现在有多大用处,因为它是几年后的事,但我已经为一个非常相似的问题苦苦挣扎了一个小时。我正在使用 Python IDLE 将我的代码保存为 .py 文件。当我尝试在终端中运行它时,我在第一行中不断收到语法错误。我的第一行代码是

#!/usr/bin/env python

but the interpreter was reading my first line as the IDLE's shell description "(Python w.7.5 (v2.75:ab05e7dd2788... etc, etc.)". What I had to do was delete all of these lines, including the first >>>in order for terminal to open my file. So I deleted all the crap the IDLE places as its first lines and voilà! It worked. Here's how I got it to work once in the terminal:

但是解释器正在阅读我的第一行作为 IDLE 的 shell 描述“(Python w.7.5 (v2.75:ab05e7dd2788 ... etc, etc.)”。我必须做的是删除所有这些行,包括第一行>>>为了让终端打开我的文件。所以我删除了 IDLE 位置的所有废话作为第一行,瞧!它起作用了。这是我在终端中让它工作一次的方法:

cd /directory/where/you/have/your/file
chmod +x filename.py
python filename.py

That's it!

而已!