4

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!

10
  • Are you typing it as python.exe hw.py? Or python.exe \n hw.py? Commented Nov 13, 2015 at 16:08
  • 1
    docs.python.org/2/faq/windows.html - for reference. Commented Nov 13, 2015 at 16:09
  • 1
    docs.python.org/2/faq/… this is what you need, just run as my_script.py Commented Nov 13, 2015 at 16:11
  • @MorganThrapp It's all on the same line no "\n" Commented Nov 13, 2015 at 16:11
  • If you have python in your PATH in windows you should just be able to type python path/to/your/file/hw.py Commented Nov 13, 2015 at 16:20

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If the system is configured correctly, just run path/to/file/filename.py. Managing PATH for multiple Python installations is a pain (except with virtual environments). With Python 3 installed, .py scripts should be associated with py.exe, which lets you use virtual Unix shebangs such as #!/usr/bin/python or #!/usr/bin/python3, or explicit Windows paths in the shebang such as #!C:\Python27\python.exe. Just set a common scripts directory in PATH.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.