且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-09-30 12:16:16

此错误:

python:无法打开文件'test.py':[Errno 2]没有这样的文件或目录

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

表示文件"test.py"不存在. (或者,但是,但是不在当前工作目录中.)

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

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

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

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

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.

您的终端(实际上是终端中的外壳)的概念是当前工作目录",这是它当前在其中的目录(文件夹).

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

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

Thus, if you type something like:

python test.py

test.py必须位于当前工作目录中.在Linux中,您可以使用cd更改当前的工作目录.如果您是新手,则可能需要教程. (请注意,该搜索对我的第一匹配是此***视频.作者视频中使用的是Mac,但是Mac和Linux都使用bash作为外壳,因此应适用于您.)

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