I am very very new in Python and I have a doubt.
If I write a program in a text editor (such as Nodepad++), then can I execute it from the Python shell (the one that begin with>>)? What command have I to launch to execute my Python program?
Tnx
Andrea
-
but the command "python program.py" in the DOS shell or in the Python shell?AndreaNobili– AndreaNobili2013年11月03日 10:26:08 +00:00Commented Nov 3, 2013 at 10:26
-
I would strongly suggest using an IDE - there are a lot of free ones out there - or at least IDLE which comes as a part of your python installation.Steve Barnes– Steve Barnes2013年11月03日 10:29:22 +00:00Commented Nov 3, 2013 at 10:29
-
@AndreaNobili 'python program.py' is to be run in the DOS shell.Antimony– Antimony2013年11月03日 11:30:29 +00:00Commented Nov 3, 2013 at 11:30
4 Answers 4
From the Python console, you can run
execfile('program.py')
where program.py is the path to your file.
EDIT:
In Python 3, you'd have to define execfile yourself before you could use it. Copy and paste the following.
def execfile(path, globals=None, locals=None):
with open(path, "r") as file:
exec(file.read(), globals, locals)
You specifically asked for running it from the Python prompt, but if possible, consider running it from the normal command prompt (DOS, bash, etc.) It's a little easier, and more normal.
Comments
- Exit python interpreter/console.
- Edit your program in notepad++ creating first_program.py in the same directory where your python.exe is
- start cmd.exe from within exactly the same directory
- type python first_program.py*
you are done
4 Comments
from within the Python IDLE shell:
File -> Open... -> Select your Python program
When your program has openend select Run -> Run Module or press F5
Comments
In the view of mine: you wrote a program: test.py
print 'test file'
and you turn to the windows cmd: you excuted python,and you got this
>
then you can just simply:
os.system('python test.py')