Ivar Eriksson
Ivar Eriksson

Reputation: 973

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!

Upvotes: 4

Views: 871

Answers (1)

Avantol13
Avantol13

Reputation: 1059

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.

Upvotes: 2

Related Questions