1

Every time I run a Python script, the cmd window appears and then immediately closes. I can't even get the basic "hello python" to run.

I'm on a Windows seven machine and Python is installed to:

C:\Users\Chaos\AppData\Local\Programs\Python\Python35-32\python.exe

The test script is

C:\Users\Chaos\AppData\Local\Programs\Python\Python35-32\python.exe
print ("Hello, Python!")
raw_input()

Anyone have a clue whats happening here?

5
  • 1
    What are the contents of the script you're trying to execute? Commented Aug 14, 2016 at 16:30
  • 1
    Are you trying to run the file from your desktop? You should run your Python files by entering python yourfile.py in cmd. Commented Aug 14, 2016 at 16:31
  • 2
    Are you sure the hello python does not run? Maybe it runs, and the window closes as it exits? Check this: stackoverflow.com/questions/1000900/… Commented Aug 14, 2016 at 16:36
  • 1
    Try using input() instead of raw_input(). The latter is for python2 and won't work with python3 Commented Aug 15, 2016 at 18:24
  • As @ursan points out, your window would stay open if you used the correct input function. Commented Aug 16, 2016 at 18:05

4 Answers 4

2

First you should validate that Python is installed. You said it was installed here for your machine: C:\Users\Chaos\AppData\Local\Programs\Python\Python35-32\python.exe

You should double check that Python is in your system environment variables. This varies by version of windows but you essentially want to:

  1. go to control panel
  2. go to system
  3. go to advanced system settings
  4. click on environment variables
  5. Click on new or add and then add the path to the folder containing the python executable, so add this: C:\Users\Chaos\AppData\Local\Programs\Python\Python35-32\

Now launch cmd.exe and type in 'python' without the quotes and you should see the REPL launch. If the REPL doesn't launch you know you have an installation issue or didn't properly setup the environment variable. If the REPL does launch then you should be able to launch your python script from the command line:

python -m yourHelloWorld.py

Please note that you need to prove an absolute path to your file if it's not in the current working directory that you're trying to launch from, or you need to navigate to that directory.

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

Comments

1

There are two problems with your code itself. I am not so sure about naming the file and adding python to PATH and that sort of stuff but I can see 2 problems in the code.

The first problem is the space between the print and where the brackets start. This is what it should look like:

print("Hello world!")

The second problem is using raw_input() instead of input() this is what that should look like:

input()

So your overall code should be this:

print("Hello world!")
input()

Comments

0

try write this comment on first line in your python file: # -*- coding: utf-8 -*-

sorry for my bad English.

Comments

0

This should work:

py script.py

Or even

script.py

When the window just closes like that, the script likely crashed.

If it doesn't, you might actually have an empty/stale copy of script.py in the current folder, try making sure that the script filename you're specifying is indeed the one you want to run.

To debug this try providing a full path to py

py "C:\Users\me\Desktop\Script.py"

Comments

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.