How do I find the location of Python module sources?
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
Python-2.6/Modules/datetimemodule.c
You can also find the latest version of this file on github on the web at
https://github.com/python/cpython/blob/main/Modules/_datetimemodule.c
How to retrieve a module's path?
import a_module
print(a_module.__file__)
Will actually give you the path to the .pyc file that was loaded, at least on Mac OS X. So I guess you can do:
import os
path = os.path.abspath(a_module.__file__)
You can also try:
path = os.path.dirname(a_module.__file__)
To get the module's directory.
Where are the python modules stored?
- Is there a way to obtain a list of
Python modules available (i.e.
installed) on a machine?
This works for me:
help('modules')
- Where is the module code actually
stored on my machine?
Usually in /lib/site-packages
in your Python folder. (At least, on Windows.)
You can use sys.path
to find out what directories are searched for modules.
How do I find the location of my Python site-packages directory?
There are two types of site-packages directories, global and per user.
Global site-packages ("dist-packages") directories are listed in
sys.path
when you run:python -m site
For a more concise list run
getsitepackages
from the site module in Python code:python -c 'import site; print(site.getsitepackages())'
Caution: In virtual environments getsitepackages is not available with older versions of
virtualenv
,sys.path
from above will list the virtualenv's site-packages directory correctly, though. In Python 3, you may use the sysconfig module instead:python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])'
The per user site-packages directory (PEP 370) is where Python installs your local packages:
python -m site --user-site
If this points to a non-existing directory check the exit status of Python and see
python -m site --help
for explanations.Hint: Running
pip list --user
orpip freeze --user
gives you a list of all installed per user site-packages.
Practical Tips
<package>.__path__
lets you identify the location(s) of a specific package: (details)$ python -c "import setuptools as _; print(_.__path__)"
['/usr/lib/python2.7/dist-packages/setuptools']<module>.__file__
lets you identify the location of a specific module: (difference)$ python3 -c "import os as _; print(_.__file__)"
/usr/lib/python3.6/os.pyRun
pip show <package>
to show Debian-style package information:$ pip show pytest
Name: pytest
Version: 3.8.2
Summary: pytest: simple powerful testing with Python
Home-page: https://docs.pytest.org/en/latest/
Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others
Author-email: None
License: MIT license
Location: /home/peter/.local/lib/python3.4/site-packages
Requires: more-itertools, atomicwrites, setuptools, attrs, pathlib2, six, py, pluggy
How do I find the location of Python module sources while I can not import it?
Try:
import imp
imp.find_module('cv2')
Where are python modules stored?
Suppose you want to know the location of xyz
module. You do this on the interpreter
>>> import xyx
>>> xyz.__file__
You'll get the location of the pyc
file from where your module is being imported.
You can also simply do
>>> xyz
to get the module name and the location of the module.
To know about all the possible locations from where the modules are imported, use sys.path
:
>>> import sys
>>> sys.path
This will give you the list of the locations where Python searches for a module when you do import
.
Hope that helps.
is there a way to view the source of a module from within the python console?
Some of the methods of inspect module are well-suited for this purpose:
import module
import inspect
src = inspect.getsource(module)
How can I find the location of the source code of a built-in Python method?
You can usually find the source files for core python modules in the python installation folder itself. For instance, on linux
, I can find the source code for os
module which is a quite popular python module in this location:
/usr/lib/python2.7/os.py
If you are on windows
, this is generally C:\python27\lib
, but you can verify it for yourself by running which python
in case of linux and where python
in case of windows
.
how to get module location
It is worth mentioning that packages have __file__
attribute which points to __init__.py
, they also have __path__
which points to the package directory. So you can use hasattr(module_name, '__path__') and module_name.__path__[0] or module_name.__file__
.
Example (in REPL):
import socket, SOAPpy # SOAPpy is a package
socket.__file__
# .../python2.5/socket.pyc
socket.__path__
# AttributeError: 'module' object has no attribute '__path__'
SOAPpy.__file__
# .../2.5/site-packages/SOAPpy/__init__.pyc
SOAPpy.__path__
# ['.../2.5/site-packages/SOAPpy']
Related Topics
Tensorflow Only Running on 1/32 of the Training Data Provided
How to Make an Exe File from a Python Program
Remove Punctuation from Unicode Formatted Strings
How to Insert a Column at a Specific Column Index in Pandas
Intersection of Two Graphs in Python, Find the X Value
Regex Matching 5-Digit Substrings Not Enclosed with Digits
Pretty Printing a Pandas Dataframe
Calling Filter Returns <Filter Object at ... >
Is Distributing Python Source Code in Docker Secure
How to Transform an Xml File Using Xslt in Python
How to Scroll the Background Surface in Pygame
Numpy "Where" with Multiple Conditions
How to Remove Nan Value While Combining Two Column in Panda Data Frame
How to Remove Nan Values from a Numpy Array
Matplotlib: Save Plot to Numpy Array
Execute Multiple Commands in Paramiko So That Commands Are Affected by Their Predecessors
JSONify a SQLalchemy Result Set in Flask
Weird Try-Except-Else-Finally Behavior with Return Statements