AndreaNobili
AndreaNobili

Reputation: 42957

How to execute a Python program from the Python shell?

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

Upvotes: 0

Views: 430

Answers (4)

Liaozi
Liaozi

Reputation: 1

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')

Upvotes: 0

Paul Draper
Paul Draper

Reputation: 83245

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.

Upvotes: 5

CML
CML

Reputation: 353

from within the Python IDLE shell:

File -> Open... -> Select your Python program

When your program has openend select Run -> Run Module or press F5

Upvotes: 0

Artur
Artur

Reputation: 7257

  • 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

Upvotes: 3

Related Questions