1

In a GUI app written in Python and Tkinter, I want to add a menu command which will open the source code of the *.py file in IDLE. I also want it to be platform independent.

I've tried using os.system to open IDLE from Scripts folder of Python, but that would be platform dependent. I couldn't find a way to get the Scripts folder of Python to make it independent. How can I achieve this?

2 Answers 2

2

To open a file in an IDLE editor, the command line is

<python> -m idlelib <path>

where <python> is the full path to a python executable or a name that resolves to such. If you want to open IDLE with the same python that is running you python app, which is likely what you want, use the platform-independent sys.executable. The path to the source code for the running app is __file__. This is one of the semi-hidden global variables when python runs a file. Or give the path to any other file.

Whether you use os.system or subprocess is a different issue. The latter is more flexible. subprocess.run replaces os.system. subprocess.Popen should not block. IDLE uses the latter to run user code.

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

2 Comments

I don't know much about executables in Linux, OSX or MacOS; will subprocess.Popen(f"{sys.executable} -m idlelib {os.path.realpath(__file__)}") work in all operating systems?
I believe so, or the list form Popen([sys.executable, '-m', 'idlelib', __file__]) should.
0

I tried some special way:

    import idlelib.pyshell
    import sys
    sys.argv.clear()
    sys.argv.append("")
    sys.argv.append(fName)
    idlelib.pyshell.main()

It works! But I don't know whether this way will generate some problems.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.