2

So I've been finding a few answers but none of them do the trick. I'd like to open a file (script.py) in IDLE using a python script (openscript.py). It seems os.system or subprocess.Popen should do the trick but I can't seem to find the correct link to open in IDLE.

import os
def openFile():
    filename = "script.py"
    os.system("C:\\Program Files\\Python38\\Lib\\idlelib\\idle.pyw " + filename)

openFile()
3
  • What's the issue ? Does Idle open at all or is just the 'script' not shown in Idle ? Commented Feb 16, 2021 at 11:43
  • C:\Users\xyz\AppData\Local\Programs\Python\Python38\Lib\idlelib>idle.bat C:\Users\xyz\py\filename.py Commented Feb 16, 2021 at 11:49
  • 1
    Stylistic suggestion: consider r' ... ' so you don't have to escape all those backslashes Commented Feb 16, 2021 at 12:39

1 Answer 1

2

Since .pyw is Windows-only, you must be running on Windows. When asking a question, always give the error message, as it likely has the answer. On Win10, your code results in a Windows error box saying that there is no app associated with that file and a suggestion to install one if needed. Replacing os.system with subprocess.run gives the error OSError: [WinError 193] %1 is not a valid Win32 application. So replace idle.pyw with the application that will run IDLE. The following (saved as tem3.py) works for me when run either from an IDLE editor or Command Prompt.

import subprocess as sub
def openFile():
    filename = "f:/Python/a/tem3.py"
    sub.run(r"C:\Programs\Python39\pythonw.exe -m idlelib " + filename)

openFile()
Sign up to request clarification or add additional context in comments.

3 Comments

Perfect thank you very much! -m idlelib at the end of the link to python did the trick!
Just as an addition: using subprocess.Popen makes the script you're executing continue. Using subprocess.run stops your script untill you close IDLE.
Right. Depends on what one wants. IDLE itself uses .Popen to start the subprocess that executes user code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.