user618111
user618111

Reputation: 439

How to launch a Python file on Windows 7?

this is the first time I have used Python. I downloaded the file ActivePython-2.7.1.4-win32-x86 and installed it on my computer; I'm using Win7.

So when I tried to run a python program, it appears and disappears very quickly. I don't have enough time to see anything on the screen. I just downloaded the file and double-cliked on it.

How do I launch this file? I know that it is a long file for a first Python tutorial.

Upvotes: 2

Views: 14522

Answers (5)

peter2108
peter2108

Reputation: 6090

Just a bit more on this.

You have a script myscript.py in a folder C:\myscripts. This is how to set up Windows 7 so that you can type > myscript into a CMD window and the script will run.

1) Set your PATH variable to include the Python Interpreter.

Control Panel > System and Security > System > Advanced Settings > Environment Variables. You can set either the System Variables or the User Variables. Scroll down till you find PATH, select it, click Edit.The Path appears selected in a new dialog. I always copy it into Notepad to edit it though all you need do is add ;C:\Python27 to the end of the list. Save this.

2) Set your PATH variable to include C:\myscripts

3) Set your PATHEXT variable to include ;.PY. (This is the bit that saves you from typing myscript.py)

This may now just work. Try opening a command window and typing myscript

But it may not. Windows can still mess you about. I had installed and then uninstalled a Python package and when I typed myscript Windows opened a box asking me which program to use. I browsed for C:\python27\python.exe and clicked that. Windows opened another command window ran the script and closed it before I could see what my script had done! To fix this when Windows opens its dialog select your Python and click the "Always do this" checkbox at the bottom. Then it doesn't open and close another window and things work as they should. Or they did for me.

Added: Above does not say how to pass arguments to your script. For this see answer Windows fails to pass arguments to python script

Upvotes: 0

Vamana
Vamana

Reputation: 578

Or run it from a batch file:

myprog.py
pause

Has the advantage that you can specify a different version of Python too.

Upvotes: 1

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115530

run it from a command prompt:

> python myscript.py

You can also start only the python interpreter from the command prompt (or by running python.exe) and then try some commands:

> python
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
>>> a = 2
>>> b = 7
>>> print a+b
9
>>> 

Upvotes: 2

Adrien Plisson
Adrien Plisson

Reputation: 23293

go to Start > All programs > Accessories and click on Command Prompt. then drag the python file from the explorer view into this command line and press Enter...

now you can watch the output of the script execution !

Upvotes: 3

Katriel
Katriel

Reputation: 123632

Add the line

input()

to the end of the program, with the correct indentation. The issue is that after the data is printed to the console the program finishes, so the console goes away. input tells the program to wait for input, so the console won't be closed when it finishes printing.

I hope you're not using that program to learn Python; it's pretty complicated!

Upvotes: 4

Related Questions