我如何了解一个给定的Python模块的源文件安装在哪里?Windows上的方法和Linux上的方法不同吗?
我特别试图寻找datetime模块的源代码,但我对更一般的答案也感兴趣。
我如何了解一个给定的Python模块的源文件安装在哪里?Windows上的方法和Linux上的方法不同吗?
我特别试图寻找datetime模块的源代码,但我对更一般的答案也感兴趣。
当前回答
在像Spyder这样的IDE中,导入模块,然后单独运行模块。 在这里输入图像描述
其他回答
在python解释器中,您可以导入特定的模块,然后键入help(module)。这将提供诸如名称、文件、模块文档、描述等详细信息。
Ex:
import os
help(os)
Help on module os:
NAME
os - OS routines for Mac, NT, or Posix depending on what system we're on.
FILE
/usr/lib/python2.6/os.py
MODULE DOCS
http://docs.python.org/library/os
DESCRIPTION
This exports:
- all functions from posix, nt, os2, or ce, e.g. unlink, stat, etc.
- os.path is one of the modules posixpath, or ntpath
- os.name is 'posix', 'nt', 'os2', 'ce' or 'riscos'
等人
如上所述 在python中只使用help(模块) 即
进口分数
帮助(分数)
如果你的模块,在例子中的分数,被安装,然后它会告诉你的位置和有关它的信息,如果它没有安装,它说模块不可用
如果它不可用,默认情况下python不会附带,在这种情况下,你可以检查你在哪里找到它的下载信息
我意识到这个答案晚了4年,但现有的答案在误导人们。
正确的方法是永远不要__file__,或者尝试遍历sys. file__。路径和搜索自己,等等(除非你需要向后兼容超过2.1)。
它是inspect模块——特别是getfile或getsourcefile。
除非你想学习和实现CPython 2的规则(这些规则有文档,但很痛苦)。3.x)用于将.pyc映射到.py文件;处理.zip档案、eggs和模块包;尝试不同的方法得到。so/的路径。不支持__file__的Pyd文件;弄清楚Jython/IronPython/PyPy做什么;等。在这种情况下,放手去做吧。
Meanwhile, every Python version's source from 2.0+ is available online at http://hg.python.org/cpython/file/X.Y/ (e.g., 2.7 or 3.3). So, once you discover that inspect.getfile(datetime) is a .so or .pyd file like /usr/local/lib/python2.7/lib-dynload/datetime.so, you can look it up inside the Modules directory. Strictly speaking, there's no way to be sure of which file defines which module, but nearly all of them are either foo.c or foomodule.c, so it shouldn't be hard to guess that datetimemodule.c is what you want.
在Ubuntu 12.04上,例如python2的numpy包,可以在以下位置找到:
/usr/lib/python2.7/dist-packages/numpy
当然,这不是一般的答案
对于那些更喜欢GUI解决方案的人:如果你正在使用GUI,如Spyder (Anaconda安装的一部分),你可以右键单击模块名称(如“import csv”中的“csv”),并选择“go to definition”-这将打开文件,但在顶部你可以看到确切的文件位置(“C:....csv.py”)