Linux 如何在终端(ubuntu)上运行 python 脚本?

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

How to run python script on terminal (ubuntu)?

pythonlinuxubuntu

提问by wombatp

I'm new with python, I've been learning for a few weeks. However now I've just changed my OS and I'm now using ubuntu and I can't run any script on my terminal.

我是 python 新手,我已经学习了几个星期。但是现在我刚刚改变了我的操作系统,我现在使用的是 ubuntu,我无法在我的终端上运行任何脚本。

I made sure to have the #!/usr/bin/env pythonbut when I go to the terminal and type, for example python test.pythe terminal shows an error message like this

我确保有#!/usr/bin/env python但是当我去终端并输入时,例如python test.py终端显示这样的错误消息

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

python:无法打开文件“test.py”:[Errno 2] 没有那个文件或目录

what do I do?
I must save the file in any specific folder to make it run on terminal?

我该怎么办?
我必须将文件保存在任何特定文件夹中才能使其在终端上运行吗?

采纳答案by Thanatos

This error:

这个错误:

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

python:无法打开文件“test.py”:[Errno 2] 没有那个文件或目录

Means that the file "test.py" doesn't exist. (Or, it does, but it isn't in the current working directory.)

意味着文件“test.py”不存在。(或者,它确实存在,但它不在当前工作目录中。)

I must save the file in any specific folder to make it run on terminal?

我必须将文件保存在任何特定文件夹中才能使其在终端上运行吗?

No, it can be where ever you want. However, if you just say, "test.py", you'll need to be in the directory containing test.py.

不,它可以在您想要的任何地方。但是,如果您只是说“test.py”,则您需要位于包含 test.py 的目录中。

Your terminal (actually, the shell in the terminal) has a concept of "Current working directory", which is what directory (folder) it is currently "in".

您的终端(实际上是终端中的 shell)有一个“当前工作目录”的概念,即它当前“在”的目录(文件夹)。

Thus, if you type something like:

因此,如果您键入以下内容:

python test.py

test.pyneeds to be in the current working directory. In Linux, you can change the current working directory with cd. You might want a tutorialif you're new. (Note that the first hit on that search for me is this YouTube video. The author in the video is using a Mac, but both Mac and Linux use bashfor a shell, so it should apply to you.)

test.py需要在当前工作目录中。在 Linux 中,您可以使用cd. 如果你是新手,你可能需要一个教程。(请注意,搜索我的第一个点击是这个 YouTube 视频视频中的作者使用的是 Mac,但 Mac 和 Linux 都bash用于外壳,因此它应该适用于您。)

回答by Shilpa

First create the file you want, with any editor like vi r gedit. And save with. Py extension.In that the first line should be

首先创建您想要的文件,使用类似 vi r gedit 的任何编辑器。并保存。Py 扩展名。第一行应该是

!/usr/bin/env python

!/usr/bin/env python

回答by Tovin

Save your python file in a spot where you will be able to find it again. Then navigate to that spot using the command line (cd /home/[profile]/spot/you/saved/file) or go to that location with the file browser. If you use the latter, right click and select "Open In Terminal." When the terminal opens, type "sudo chmod +x Yourfilename." After entering your password, type "python ./Yourfilename" which will open your python file in the command line. Hope this helps!

将您的 python 文件保存在您可以再次找到它的地方。然后使用命令行导航到该位置 (cd /home/[profile]/spot/you/saved/file) 或使用文件浏览器转到该位置。如果您使用后者,请右键单击并选择“在终端中打开”。当终端打开时,输入“sudo chmod +x Yourfilename”。输入密码后,键入“python ./Yourfilename”,这将在命令行中打开您的 python 文件。希望这可以帮助!

Running Linux Mint

运行 Linux Mint

回答by HakunaMatata

Set the PATH as below:

设置PATH如下:



In the csh shell ? type setenv PATH "$PATH:/usr/local/bin/python"and press Enter.

在 csh 外壳中?键入setenv PATH "$PATH:/usr/local/bin/python"并按 Enter。

In the bash shell (Linux) ? type export PATH="$PATH:/usr/local/bin/python"and press Enter.

在 bash shell (Linux) 中?键入export PATH="$PATH:/usr/local/bin/python"并按 Enter。

In the sh or ksh shell ? type PATH="$PATH:/usr/local/bin/python"and press Enter.

在 sh 或 ksh 外壳中?键入PATH="$PATH:/usr/local/bin/python"并按 Enter。

Note ? /usr/local/bin/pythonis the path of the Python directory

笔记 ?/usr/local/bin/python是Python目录的路径



now run as below:

现在运行如下:

-bash-4.2$ python test.py

Hello, Python!

回答by Youil Aushana

Sorry, Im a newbie myself and I had this issue:

对不起,我自己是新手,我遇到了这个问题:

./hello.py: line 1: syntax error near unexpected token "Hello World"' ./hello.py: line 1:print("Hello World")'

./hello.py:第 1 行:意外标记附近的语法错误"Hello World"' ./hello.py: line 1:print("Hello World")'

I added the file header for the python 'deal' as #!/usr/bin/python

我将 python 'deal' 的文件头添加为 #!/usr/bin/python

Then simple executed the program with './hello.py'

然后用'./hello.py'简单地执行程序