简体   繁体   English

Python:在Finder / Windows资源管理器中打开模块源代码

[英]Python: Open a modules source code in Finder/Windows explorer

Let's say I'm in the Python interactive shell, and I want to see the source code of a module I'm using. 假设我在Python交互式外壳中,并且想要查看正在使用的模块的源代码。 Say, the requests module. 说, requests模块。 I know you can use inspect.getsource() to view the source code inside the command prompt. 我知道您可以使用inspect.getsource()在命令提示符下查看源代码。 But is there a python call that would open up that module's source directory in Finder or Windows Explorer? 但是是否有python调用可以在Finder或Windows资源管理器中打开该模块的源目录?

Or is there a way to have inspect.getsource() open up the source code in an out-of-terminal editor? 还是有一种方法可以使inspect.getsource()在终端外编辑器中打开源代码?

For your first question of 'But is there a python call that would open up that module's source directory in Finder or Windows Explorer?': you can use a subprocess to open your desired folder: 对于您的第一个问题“但是在Finder或Windows资源管理器中是否有一个python调用会打开该模块的源目录?”:您可以使用子过程打开所需的文件夹:

import subprocess
mydir="c:\\"
subprocess.Popen('explorer '+mydir)

To open the file in desired editor: If you know the path to your desired editor or if the desired editor is already in the windows path you can do something like: 要在所需的编辑器中打开文件,请执行以下操作:如果您知道所需编辑器的路径,或者所需的编辑器已经在Windows路径中,则可以执行以下操作:

import subprocess
myfile="c:\\"
myEditor="gvim" #assuming gvim is in the path. If not use the full path
subprocess.Popen(myEditor + ' ' +mydir)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM