How to run python script from IDLE interactive shell?

How to run python script from IDLE interactive shell?

The following error message is displayed:

>>> python helloworld.py SyntaxError: invalid syntax 
+72
python command-line shell python-idle
Jun 22 '13 at 5:03
source share
12 answers

Built-in Function: execfile

 execfile('helloworld.py') 

Usually it cannot be called with arguments. But here is 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() 



Using Advance: 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 more in the documents :-)




Tested with this base helloworld.py :

 import sys if len(sys.argv) > 1: print(sys.argv[1]) 
+99
Feb 08 '14 at 19:21
source share

You can use this in python3:

 exec(open(filename).read()) 
+26
Dec 01 '16 at 9:30
source share

The IDLE shell window does not match the terminal shell (for example, sh or bash works). Rather, it seems to work in an interactive Python interpreter ( python -i ). The easiest way to run a script in IDLE is to use the Open command on the File menu (this may vary slightly depending on which platform you are using) to load your script file into the IDLE editor and then use the Run β†’ Run Module command (shortcut F5) .

+20
Jun 22 '13 at 5:18
source share

try it

 import os import subprocess DIR = os.path.join('C:\\', 'Users', 'Sergey', 'Desktop', 'helloword.py') subprocess.call(['python', DIR]) 
+5
Mar 14 '15 at 8:08
source share

execFile('helloworld.py') does the job for me. Note the full directory name of the .py file if it is not in the Python folder itself (at least this applies to Windows)

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

+4
Oct 10 '14 at 9:56
source share

For example:

 import subprocess subprocess.call("C:\helloworld.py") subprocess.call(["python", "-h"]) 
+2
Mar 13 '15 at 20:24
source share

THE EASIEST WAY

 python -i helloworld.py #Python 2 python3 -i helloworld.py #Python 3 
+2
Aug 02 '18 at 5:08
source share

The following works in IDLE: -

 import helloworld 

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

+1
Jul 08 '16 at 15:12
source share

To run a python script in a python shell such as Idle or in a Django shell, you can do the following with the exec () function. Exec () executes the code object argument. The code object in Python is just compiled Python code. Therefore, 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 am using Python 3.4. See compile and exec info for more details.

+1
Sep 12 '16 at 19:27
source share

There is no execFile in Python 3. You can use the exec built-in function, for example:

 import helloworld exec('helloworld') 
0
Mar 08 '16 at 12:25
source share

I tested this and it kind of works:

 exec(open('filename').read()) # Don't forget to put the filename between ' ' 
0
Aug 13 '17 at 13:39 on
source share

You can do this in two ways.

  • import file_name

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

but make sure the file should be stored where your program works

0
Dec 08 '18 at 14:34
source share



All Articles