如何在终端(ubuntu)上运行python脚本? [英] How to run python script on terminal (ubuntu)?

查看:736
本文介绍了如何在终端(ubuntu)上运行python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,我已经学习了几周.但是,现在我刚刚更改了操作系统,现在正在使用ubuntu,并且无法在终端上运行任何脚本.

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.

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

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

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

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

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

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

推荐答案

此错误:

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更改当前的工作目录.如果您是新手,则可能需要教程. (请注意,该搜索对我的第一匹配是此YouTube视频.作者视频中使用的是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 YouTube 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.)

这篇关于如何在终端(ubuntu)上运行python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆