How do I find the location of Python module sources?

The Question :

506 people think this question is useful

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.

The Question Comments :

The Answer 1

394 people think this answer is useful

For a pure python module you can find the source by looking at themodule.__file__. The datetime module, however, is written in C, and therefore datetime.__file__ points to a .so file (there is no datetime.__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

The Answer 2

227 people think this answer is useful

Running python -v from the command line should tell you what is being imported and from where. This works for me on Windows and Mac OS X.

C:\>python -v
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# C:\Python24\lib\site.pyc has bad mtime
import site # from C:\Python24\lib\
# wrote C:\Python24\lib\site.pyc
# C:\Python24\lib\os.pyc has bad mtime
import os # from C:\Python24\lib\
# wrote C:\Python24\lib\os.pyc
import nt # builtin
# C:\Python24\lib\ntpath.pyc has bad mtime

I’m not sure what those bad mtime’s are on my install!

The Answer 3

124 people think this answer is useful

I realize this answer is 4 years late, but the existing answers are misleading people.

The right way to do this is never __file__, or trying to walk through sys.path and search for yourself, etc. (unless you need to be backward compatible beyond 2.1).

It’s the inspect module—in particular, getfile or getsourcefile.

Unless you want to learn and implement the rules (which are documented, but painful, for CPython 2.x, and not documented at all for other implementations, or 3.x) for mapping .pyc to .py files; dealing with .zip archives, eggs, and module packages; trying different ways to get the path to .so/.pyd files that don’t support __file__; figuring out what Jython/IronPython/PyPy do; etc. In which case, go for it.

Meanwhile, every Python version’s source from 2.0+ is available online at (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/, 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.

The Answer 4

80 people think this answer is useful

If you’re using pip to install your modules, just pip show $module the location is returned.

The Answer 5

68 people think this answer is useful

The sys.path list contains the list of directories which will be searched for modules at runtime:

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

The Answer 6

38 people think this answer is useful

from the standard library try imp.find_module

>>> import imp
>>> imp.find_module('fontTools')
(None, 'C:\\Python27\\lib\\site-packages\\FontTools\\fontTools', ('', '', 5))
>>> imp.find_module('datetime')
(None, 'datetime', ('', '', 6))

The Answer 7

35 people think this answer is useful

datetime is a builtin module, so there is no (Python) source file.

For modules coming from .py (or .pyc) files, you can use mymodule.__file__, e.g.

> import random
> random.__file__

The Answer 8

13 people think this answer is useful

Here’s a one-liner to get the filename for a module, suitable for shell aliasing:

echo 'import sys; t=__import__(sys.argv[1],fromlist=[\".\"]); print(t.__file__)'  | python - 

Set up as an alias:

alias getpmpath="echo 'import sys; t=__import__(sys.argv[1],fromlist=[\".\"]); print(t.__file__)'  | python - "

To use:

$ getpmpath twisted
$ getpmpath twisted.web

The Answer 9

12 people think this answer is useful

In the python interpreter you could import the particular module and then type help(module). This gives details such as Name, File, Module Docs, Description et al.


import os


Help on module os:


os - OS routines for Mac, NT, or Posix depending on what system we're on.





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

- is 'posix', 'nt', 'os2', 'ce' or 'riscos'

et al

The Answer 10

12 people think this answer is useful

On windows you can find the location of the python module as shown below:i.e find rest_framework module enter image description here

The Answer 11

10 people think this answer is useful

New in Python 3.2, you can now use e.g. code_info() from the dis module:

The Answer 12

9 people think this answer is useful

Check out this nifty “cdp” command to cd to the directory containing the source for the indicated Python module:

cdp () {
  cd "$(python -c "import os.path as _, ${1}; \
    print _.dirname(_.realpath(${1}.__file__[:-1]))"

The Answer 13

4 people think this answer is useful

On Ubuntu 12.04, for example numpy package for python2, can be found at:


Of course, this is not generic answer

The Answer 14

1 people think this answer is useful

Another way to check if you have multiple python versions installed, from the terminal.

-MBP:~python3 -m pip show pyperclip

Location: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-

MBP:~ python -m pip show pyperclip

Location: /Users/umeshvuyyuru/Library/Python/2.7/lib/python/site-packages

The Answer 15

0 people think this answer is useful

Not all python modules are written in python. Datetime happens to be one of them that is not, and (on linux) is

You would have to download the source code to the python standard library to get at it.

The Answer 16

0 people think this answer is useful

For those who prefer a GUI solution: if you’re using a gui such as Spyder (part of the Anaconda installation) you can just right-click the module name (such as “csv” in “import csv”) and select “go to definition” – this will open the file, but also on the top you can see the exact file location (“C:…”)

The Answer 17

0 people think this answer is useful

If you are not using interpreter then you can run the code below:

import site
print (site.getsitepackages())


['C:\\Users\\<your username>\\AppData\\Local\\Programs\\Python\\Python37', 'C:\\Users\\<your username>\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages']

The second element in Array will be your package location. In this case:

C:\Users\<your username>\AppData\Local\Programs\Python\Python37\lib\site-packages

The Answer 18

0 people think this answer is useful

In an IDE like Spyder, import the module and then run the module individually. enter image description here

Add a Comment