简体   繁体   English

从终端问题运行python程序

[英]Running a python program from a terminal issue

Basically this is something that I had an issue with running C programme and I'm trying to run a python program from a terminal but the problem arises again. 基本上,这是我在运行C程序时遇到的问题,我正在尝试从终端运行python程序,但问题再次出现。 after tying an exact file in a exact directory the terminal doesn't seem to run the program and just skip it. 在一个确切的目录中绑定一个确切的文件后,终端似乎并没有运行该程序,只是跳过了它。

Is there something that I'm doing wrong to run and display on a terminal command? 我在运行并在终端命令上显示时出错了吗?

$ ls
hello_world.py
$ python hello_world.py
$

This below 'script' is how I run the code with sublime text. 下面的“脚本”是我如何使用崇高的文本运行代码。 Basically from my point of view there is no problem in script itself.. 从我的角度来看,基本上脚本本身没有问题。

print('Hello Python world!')

If your python is properly installed then there is no problem with the terminal. 如果您的python安装正确,则终端没有问题。 Python interpreter executes code line by line.It is completing the file and coming out of it. Python解释器逐行执行代码,它将完成文件并从文件中提取出来。

Whenever I have this kind of problem it is because the file I'm trying to run is not the same I am changing. 每当遇到此类问题时,都是因为我要运行的文件与我要更改的文件不同。 I recommend using something (like cat ) to check the content of the file before actually running it. 我建议在实际运行文件之前使用某种东西(例如cat )检查文件的内容。

So, in your case, pop up your terminal and run: 因此,在您的情况下,弹出终端并运行:

$ cat hello_world.py

The output should be 输出应为

$ cat hello_world.py
print("Hello Python World!")

If the output is blank, there is your problem. 如果输出为空白,则说明您有问题。 Try to open that exact file with sublime . 尝试使用sublime打开该确切文件。 Probably with 大概与

$ sublime hello_world.py

At the top of your .py file .py文件顶部

Add

#!/usr/bin/python3 

Then when you need to give your file permissions 然后,当您需要授予文件权限时

chmod +x hello_world.py

Then to run 然后运行

./hello_world.py

This will run it in python3 if you want python2 just change that in the usr/bin 如果您要python2只是在usr / bin中更改它,它将在python3中运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM