How do I learn where the source file for a given Python module is installed? Is the method different on Windows than on Linux?
I'm trying to look for the source of the datetime
module in particular, but I'm interested in a more general answer as well.
If you're using pip to install your modules, just
pip show $module
the location is returned.from the standard library try imp.find_module
Not all python modules are written in python. Datetime happens to be one of them that is not, and (on linux) is datetime.so.
You would have to download the source code to the python standard library to get at it.
You can see source right from the git repository. For example here is the datetime module and as a bonus, the subprocess module.
If you do more digging around you can find the traditional implementation of the standard Python interpreter, which is written in ‘C’ and also called ‘CPython’
On windows you can find the location of the python module as shown below:i.e find rest_framework module
For a pure python module you can find the source by looking at
themodule.__file__
. The datetime module, however, is written in C, and thereforedatetime.__file__
points to a .so file (there is nodatetime.__file__
on Windows), and therefore, you can't see the source.If you download a python source tarball and extract it, the modules' code can be found in the Modules subdirectory.
For example, if you want to find the datetime code for python 2.6, you can look at
You can also find the latest Mercurial version on the web at https://hg.python.org/cpython/file/tip/Modules/_datetimemodule.c