我如何了解一个给定的Python模块的源文件安装在哪里?Windows上的方法和Linux上的方法不同吗?

我特别试图寻找datetime模块的源代码,但我对更一般的答案也感兴趣。


当前回答

在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(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模块,你可以通过查看module.__file__找到源代码。 然而,datetime模块是用C语言编写的,因此datetime模块是用C语言编写的。__file__指向一个.so文件(没有datetime. so文件)。__file__在Windows上),因此,你不能看到源代码。

如果你下载一个python源tarball并提取它,模块的代码可以在modules子目录中找到。

例如,如果你想找到python 2.6的datetime代码,你可以查看

Python-2.6/Modules/datetimemodule.c

你也可以在github上找到这个文件的最新版本 https://github.com/python/cpython/blob/main/Modules/_datetimemodule.c

在windows上,你可以找到python模块的位置,如下所示:E找到rest_framework模块

sys。Path list包含在运行时搜索模块的目录列表:

python -v
>>> import sys
>>> sys.path
['', '/usr/local/lib/python25.zip', '/usr/local/lib/python2.5', ... ]

如上所述 在python中只使用help(模块) 即

进口分数

帮助(分数)

如果你的模块,在例子中的分数,被安装,然后它会告诉你的位置和有关它的信息,如果它没有安装,它说模块不可用

如果它不可用,默认情况下python不会附带,在这种情况下,你可以检查你在哪里找到它的下载信息