Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Python from Command Line

I wrote a very simple python program in hope that I could run it from the Windows command line. In the terminal I type python.exe hw.py and instead of running the program I get the python interpreter. Does anyone know what I'm doing wrong?

Program:

def hello():
    return "Hello World!"


if __name__ == "__main__":
    hello()

I've also tried even simpler programs such as

print("Hello world!")

and

return "Hello World!"

but nothing works. The goal here is to get output in the console. Thanks!

like image 796
Ivar Eriksson Avatar asked Sep 16 '25 09:09

Ivar Eriksson


1 Answers

To summarize my comments as an answer, to call the file from the command line you need the location to your python installation in your PATH variable in Windows. Then you can just use the following in the command line:

python path/to/file/filename.py

If you're trying to print to the console you need to use print("Hello!") and not return.

like image 153
Avantol13 Avatar answered Sep 17 '25 22:09

Avantol13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!