简体   繁体   中英

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. 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 interpreter executes code line by line.It is completing the file and coming out of it.

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.

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 . Probably with

$ sublime hello_world.py

At the top of your .py file

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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