2

So I'm an extreme beginner to programming, just starting the Python class on Coursera. Using Python 2.7.10

Anyway, I made a simple print statement script in Notepad++

print "Hello World"

and saved as a python file on my desktop

newprog.py

However when I try to run it a cmd window appears and disappears and I'm not quite sure whats wrong.

The other question that this was linked as a duplicate to is about accessing python through the command prompt, which I don't have a problem with. From answers given it is now apparent to me that my dilemma was due to an erroneous belief that the interpreter would remain open after running whatever script I wrote.

David Ngo
  • 23
  • 1
  • 1
  • 4

5 Answers5

2

Sounds like your program simply opened, ran and exited. So nothing was wrong, it just all happened a bit quick for you to see it.

You should run it from a command prompt or get an IDE like Pycharm, which will allow you to both write and run your script in one program.

To run from command prompt, use either Windows Key + R and type 'cmd' or click start and type 'cmd' into search box. Then you can drag your script to the command prompt window and press Enter to run it.

If you wanted to run it by double click, you'd need something to stop it from finishing until you'd read the message. To achieve this you can use the raw_input function, which waits for user input.

So your script would then look like

print "Hello World"
raw_input("Press Enter to exit")

Then you could double click and press enter when you are ready to exit.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
  • Oh I see, so by default python closes after running your script? – David Ngo Jun 09 '15 at 02:01
  • Yes. Once your script ends the python interpreter ends too. It's what happens with with most programming languages. – Paul Rooney Jun 09 '15 at 02:03
  • Oh I see, I didn't know that! So would you recommend that as a beginner I do everything in the interactive shell then? – David Ngo Jun 09 '15 at 02:06
  • not only python, any simple command line program you make with any language will likely do the same. i.e: a .bat file with: echo "Hello world!" will yield the same results – Dleep Jun 09 '15 at 02:08
  • The interactive shell is good for small things, but you don't save whatever it is you've typed in, so you lose it when you quit. If you're doing anything more than a couple of lines do it in a file. – Paul Rooney Jun 09 '15 at 02:12
  • Err..I'm sorry but could you clarify what you mean by do it in a file? Like Notepad++ that I used to write this? I didn't address this part initially but could you explain what Pycharm is exactly, if it differs a fair bit from Notepad++? – David Ngo Jun 09 '15 at 02:18
  • Sorry I meant save it to a file and run it from the command line or from an [IDE](http://en.wikipedia.org/wiki/Integrated_development_environment). I've never used PyCharm myself but it seems popular. Its an IDE, so it has the text editor and launcher for the program in one place, along with other stuff that might be useful later like auto complete and a debugger. You can download it and try it. – Paul Rooney Jun 09 '15 at 02:26
1

Go to the command prompt window

python

then type in

execfile('path to newfile.py here')

Your file will now be executed

Nitin Kashyap
  • 184
  • 1
  • 1
  • 13
0

I'm running python 3.4.3. But it should be the same, I hope.

Go go "..\PythonXX\Lib\idlelib" and look for idle.pyw NOT idle.py and using the you're able to execute simple one line commands like the one you have up there.

From that you can also create a new file and do more complicated stuff.

If you create a shortcut to your desktop, you'll be able to access it easier.

Let me know if it helps, or at least correct path.

Leb
  • 15,483
  • 10
  • 56
  • 75
0

Your script is probably working and then finishing, the result is shown but not for long. I recommend opening the console and running your script from there, or you could use a simple batch file to run python scripts and then wait for a key press.

To open the console you can use the Windows key along the R key, Win-R (to run a new process) and write cmd, or you look for cmd in your Window's start menu.

With the console opened, you must locate the path where your script is, you can use the cd (Change Directory) to get there, for example:

cd C:\Users\your_name\Desktop

and then write:

python newprog.py

to run your script.

Another option is to use this simple batch file (save it as python34.bat or similar, but the extension must be .bat, put it wherever you like):

@ECHO OFF
C:\Python34\python.exe %*
pause
@ECHO ON

And then use that to run your scripts by right clicking a python script file, open with (run with) and use this batch script as default (if you want). Also, if you have another version of Python, or is installed elsewhere, you must change the "C:\Python34\" part.

sempiedram
  • 137
  • 12
0

This is a computer we're talking about here. It might take you triple the time it takes a computer to multiple two numbers for example. With this notion in mind, the computer quickly prints then exits.

raw_input() # at the end of script wait for user to supply input, delaying script exit
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
  • Thank you for taking the time to answer, the other answer was a tad bit more detailed so I chose to accept that, but I appreciate that you took the time anyway – David Ngo Jun 09 '15 at 02:13