How to run a python script from IDLE interactive shell?

364,510

Solution 1

Python3:

exec(open('helloworld.py').read())

If your file not in the same dir:

exec(open('./app/filename.py').read())

See https://stackoverflow.com/a/437857/739577 for passing global/local variables.


In deprecated Python versions

Python2 Built-in function: execfile

execfile('helloworld.py')

It normally cannot be called with arguments. But here's a workaround:

import sys
sys.argv = ['helloworld.py', 'arg']  # argv[0] should still be the script name
execfile('helloworld.py')

Deprecated since 2.6: popen

import os
os.popen('python helloworld.py') # Just run the program
os.popen('python helloworld.py').read() # Also gets you the stdout

With arguments:

os.popen('python helloworld.py arg').read()

Advance usage: subprocess

import subprocess
subprocess.call(['python', 'helloworld.py']) # Just run the program
subprocess.check_output(['python', 'helloworld.py']) # Also gets you the stdout

With arguments:

subprocess.call(['python', 'helloworld.py', 'arg'])

Read the docs for details :-)


Tested with this basic helloworld.py:

import sys
if len(sys.argv) > 1:
    print(sys.argv[1])

Solution 2

You can use this in python3:

exec(open(filename).read())

Solution 3

The IDLE shell window is not the same as a terminal shell (e.g. running sh or bash). Rather, it is just like being in the Python interactive interpreter (python -i). The easiest way to run a script in IDLE is to use the Open command from the File menu (this may vary a bit depending on which platform you are running) to load your script file into an IDLE editor window and then use the Run -> Run Module command (shortcut F5).

Solution 4

EASIEST WAY

python -i helloworld.py  #Python 2

python3 -i helloworld.py #Python 3

Solution 5

Try this

import os
import subprocess

DIR = os.path.join('C:\\', 'Users', 'Sergey', 'Desktop', 'helloword.py')

subprocess.call(['python', DIR])
Share:
364,510

Related videos on Youtube

tshepang
Author by

tshepang

I do software development for a living and as a hobby. My favorite language is Rust, and I've used Python much in the past. My OS of choice is Debian.

Updated on July 04, 2020

Comments

  • tshepang
    tshepang almost 4 years

    How do I run a python script from within the IDLE interactive shell?

    The following throws an error:

    >>> python helloworld.py
    SyntaxError: invalid syntax
    
    • TerryA
      TerryA almost 11 years
      What does helloworld.py look like?
    • Serial
      Serial almost 11 years
      yeah it means something is wrong with youre code post youre code!
    • Ned Deily
      Ned Deily almost 11 years
      No, not necessarily. Chances are the OP is typing python helloworld.py in an IDLE shell window and that doesn't work.
    • Terry Jan Reedy
      Terry Jan Reedy about 9 years
      Nor would it work in the standard interpreter. This issue has come up before where people mistakenly think that the interpreter prompt is a command-line prompt.
    • Krishna Oza
      Krishna Oza about 9 years
      You should accept the answer from Ned Deily if that answer your question correctly. This will also help fellow developers to quickly spot the correct answer.
    • Leonard
      Leonard almost 6 years
      EASIEST WAY: python -i helloworld.py also works for python3
  • Erica Kane
    Erica Kane about 9 years
    But you can't pass in arguments. :(
  • Ned Deily
    Ned Deily about 9 years
    Unfortunately, no, it's not easy to run a Python file in IDLE while passing in command line arguments. There is a long-standing open issue for IDLE to do so (bugs.python.org/issue5680). One workaround for testing is to manually initialize sys.argv at the very beginning of the program, for example, under the usual if __name__ == "__main__" boilerplate.
  • Terry Jan Reedy
    Terry Jan Reedy about 9 years
    subprocess.call(r'c:\path\to\something.py') does not work for me. OSError: [WinError 193] %1 is not a valid Win32 application
  • Sergey Nosov
    Sergey Nosov about 9 years
    Try this import os import subprocess DIR = os.path.join('C:\\', 'Users', 'Sergey', 'Desktop', 'a.py') subprocess.call(['python', DIR])
  • arsho
    arsho about 8 years
    This answer clarifies why we can not run python script using IDLE shell. Thanks @NedDeily
  • ViFI
    ViFI almost 8 years
    Can you please add an example with command line arguments as well.
  • tanweer alam
    tanweer alam over 7 years
    what you are doing is loading a module not running from shell. one difference between two is: 1) Loading module your module name name__= name of file 2) run from shell module name _name__="_main"
  • Forever Learner
    Forever Learner over 5 years
    thanks for the quotation tip, this worked for me. Upvoted.
  • Suncatcher
    Suncatcher about 5 years
    Doesn't work with Python3 and asker didn't specify Python version explicitly
  • Hugues Fontenelle
    Hugues Fontenelle about 5 years
    Added exec for Python3
  • Dan Nolan
    Dan Nolan over 4 years
    As of IDLE 3.7.4, you can now run a module with arguments. Use the new Run -> Run with Customized... command (shortcut Shift+F5) and a popup will open where you can supply your arguments. Unfortunately it doesn't remember them currently so you'll be pasting them with every run.
  • Grasshopper
    Grasshopper almost 4 years
    importing a program to run it is not a good option. Even I used it in beginning but now I know about potential problems like it takes away the effeciency of your program because you are importing it (though negligible).
  • Grasshopper
    Grasshopper almost 4 years
    It is not a good way to run programs because you are actually importing and not running it. Its contents will be imported and it will be meaningless if you don't want to utilise them.