How to run a python script from IDLE interactive shell?

PythonShellCommand LinePython Idle

Python Problem Overview


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

The following throws an error:

>>> python helloworld.py
SyntaxError: invalid syntax

Python Solutions


Solution 1 - Python

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 - Python

You can use this in python3:

exec(open(filename).read())

Solution 3 - Python

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 - Python

EASIEST WAY

python -i helloworld.py  #Python 2

python3 -i helloworld.py #Python 3

Solution 5 - Python

Try this

import os
import subprocess

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

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

Solution 6 - Python

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

Solution 7 - Python

In a python console, one can try the following 2 ways.

under the same work directory,

>> import helloworld

# if you have a variable x, you can print it in the IDLE.

>> helloworld.x

# if you have a function func, you can also call it like this.

>> helloworld.func()

>> runfile("./helloworld.py")

Solution 8 - Python

For example:

import subprocess

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

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

Solution 9 - Python

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

import helloworld
exec('helloworld')

Solution 10 - Python

In IDLE, the following works :-

import helloworld

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

Solution 11 - Python

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, '', 'exec') > >>>exec(code_object)

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

Solution 12 - Python

I tested this and it kinda works out :

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

Solution 13 - Python

you can do it by two ways

  • import file_name

  • exec(open('file_name').read())

but make sure that file should be stored where your program is running

Solution 14 - Python

On Windows environment, you can execute py file on Python3 shell command line with the following syntax:

> exec(open('absolute path to file_name').read())

Below explains how to execute a simple helloworld.py file from python shell command line

> File Location: C:/Users/testuser/testfolder/helloworld.py > > File Content: print("hello world")

We can execute this file on Python3.7 Shell as below:

>>> import os
>>> abs_path = 'C://Users/testuser/testfolder'
>>> os.chdir(abs_path)
>>> os.getcwd()
'C:\\Users\\testuser\\testfolder'

>>> exec(open("helloworld.py").read())
hello world

>>> exec(open("C:\\Users\\testuser\\testfolder\\helloworld.py").read())
hello world

>>> os.path.abspath("helloworld.py")
'C:\\Users\\testuser\\testfolder\\helloworld.py'
>>> import helloworld
hello world

Solution 15 - Python

There is one more alternative (for windows) -

    import os
    os.system('py "<path of program with extension>"')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1703914View Question on Stackoverflow
Solution 1 - PythonHugues FontenelleView Answer on Stackoverflow
Solution 2 - PythonFacePalmView Answer on Stackoverflow
Solution 3 - PythonNed DeilyView Answer on Stackoverflow
Solution 4 - PythonLeonardView Answer on Stackoverflow
Solution 5 - PythonSergey NosovView Answer on Stackoverflow
Solution 6 - Pythonoptimistic_kidView Answer on Stackoverflow
Solution 7 - PythonLynnSunshineView Answer on Stackoverflow
Solution 8 - PythonSergey NosovView Answer on Stackoverflow
Solution 9 - PythonpiogorView Answer on Stackoverflow
Solution 10 - PythonAditya DixitView Answer on Stackoverflow
Solution 11 - Pythony2knoproblemView Answer on Stackoverflow
Solution 12 - PythonRemache AmineView Answer on Stackoverflow
Solution 13 - Pythonujjal dasView Answer on Stackoverflow
Solution 14 - PythonvinsinrawView Answer on Stackoverflow
Solution 15 - PythonGrasshopperView Answer on Stackoverflow