简体   繁体   English

如何在 Python 中使用 IDLE 打开当前正在运行的脚本的 .py 文件?

[英]How can I open the currently running script's .py file using IDLE in Python?

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.在用 Python 和 Tkinter 编写的 GUI 应用程序中,我想添加一个菜单命令,它将在 IDLE 中打开 *.py 文件的源代码。 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.我尝试使用 os.system 从os.system的 Scripts 文件夹中打开 IDLE,但这取决于平台。 I couldn't find a way to get the Scripts folder of Python to make it independent.我找不到方法来获取 Python 的 Scripts 文件夹以使其独立。 How can I achieve this?我怎样才能做到这一点?

To open a file in an IDLE editor, the command line is要在 IDLE 编辑器中打开文件,命令行是

<python> -m idlelib <path>

where <python> is the full path to a python executable or a name that resolves to such.其中<python>是 python 可执行文件的完整路径或解析为此类的名称。 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 .如果您想使用运行 python 应用程序的相同 python 打开 IDLE,这可能是您想要的,请使用与平台无关的sys.executable The path to the source code for the running app is __file__ .正在运行的应用程序的源代码路径是__file__ This is one of the semi-hidden global variables when python runs a file.这是 python 运行文件时的半隐藏全局变量之一。 Or give the path to any other file.或提供任何其他文件的路径。

Whether you use os.system or subprocess is a different issue.无论您使用subprocess还是os.system是一个不同的问题。 The latter is more flexible.后者更灵活。 subprocess.run replaces os.system . subprocess.run替换os.system subprocess.Popen should not block. subprocess.Popen不应阻塞。 IDLE uses the latter to run user code. IDLE 使用后者来运行用户代码。

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.但不知道这种方式会不会产生一些问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 Python idle 中打开 .py 文件 - Can't open .py file in Python idle IDLE无法打开.py文件,并暗示“文件的编码对于Python 3.x无效”。 - IDLE can not open the .py file and it hints that “The file's encoding is invalid for Python 3.x.” 运行python脚本的NSTask无法打开.py文件 - NSTask running a python script can't open .py file 如何获取当前运行的 python 文件所在的驱动器? - How can I get the drive that the currently running python file is in? 如何在python中找到当前正在执行的脚本文件和路径? - How can I find the currently executing script file and path in python? 如何可靠地打开与当前正在运行的脚本位于同一目录中的文件 - How to reliably open a file in the same directory as the currently running script 通过python脚本在IDLE中打开文件 - Open file in IDLE through python script python解释器是否将当前正在运行的脚本的py或pyc文件保存在一个临时文件夹中? - Does python interpreter keep py or pyc file of currently running script in a temporary folder? 我如何使用脚本在脚本文件或views.py中运行python函数来不加载页面 - How Can I run a python function in script file or in the views.py using javascript for not loading the page Python:如何让脚本在运行时写入文件,然后保持打开状态以在按键上进行写入? - Python: How can I have a script write to a file when it's run then stay open to write on a keypress?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM