Home » Python » How to run a python script from IDLE interactive shell?

How to run a python script from IDLE interactive shell?

Posted by: admin November 30, 2017 Leave a comment

Questions:

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

The following throws an error:

>>> python helloworld.py
SyntaxError: invalid syntax
Answers:

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

Questions:
Answers:

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

Questions:
Answers:

You can use this in python3:

exec(open(filename).read())

Questions:
Answers:

Try this

import os
import subprocess

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

subprocess.call(['python', DIR])

Questions:
Answers:

execFile('helloworld.py') does the job for me. A thing to note is to enter the complete directory name of the .py file if it isnt in the Python folder itself (atleast this is the case on Windows)

For example, execFile('C:/helloworld.py')

Questions:
Answers:

For example:

import subprocess

subprocess.call("C:\helloworld.py")

subprocess.call(["python", "-h"])

Questions:
Answers:

In Python 3, there is no execFile. One can use exec built-in function, for instance:

import helloworld
exec('helloworld')

Questions:
Answers:

In IDLE, the following works :-

import helloworld

I don’t know much about why it works, but it does..

Questions:
Answers:

To run a python script in a python shell such as Idle or in a Django shell you can do the following using the exec() function. Exec() executes a code object argument. A code object in Python is simply compiled Python code. So you must first compile your script file and then execute it using exec(). From your shell:

>>>file_to_compile = open('/path/to/your/file.py').read()
>>>code_object = compile(file_to_compile, '<string>', 'exec')
>>>exec(code_object)

I’m using Python 3.4. See the compile and exec docs for detailed info.

Questions:
Answers:

I tested this and it kinda works out :

exec(open('filename').read())  # Don't forget to put the filename between ' '

Leave a Reply

Your email address will not be published. Required fields are marked *