Finding the source code for a Python module
I’m using PyCharm as my editor and seemingly it doesn’t behave well with certain sub-modules namely numpy.random.normal . Not to be disheartened I tracked down where numpy.random lives to /usr/lib/python2.7/dist-packages/numpy/random . I can’t see any instance of normal. There’s the definition for it in __init__.py but no actual code for me to copy into a new class for my project. Am I looking in the wrong place for the code?
Why do you want to see the source? What is the problem you are having? These functions are implemented in C, see e.g. github.com/numpy/numpy/blob/master/numpy/random/mtrand/…
Whenever I try to import it into PyCharm using noise = numpy.random.normal(0, power_noise, len(self.data)) it tells me it cannot find the reference. If I open a terminal, however, I can use the normal function as I desire.
This looks like a PyCharm bug (indeed, their bugtracker lists several bugs like this). It’s a commercial product, so take your problem to the software’s developers.
1 Answer 1
You can find out, where a package is located by doing so:
import numpy.random print numpy.random.__file__
In your case, it seems that the main parts of the module are implemented in C. You can see in the directory, that there is a file «mtrand.so» located in it. This is a shared object that was created from C sources, which are typically not delivered with the runtime package. The Python system can load such shared objects at runtime, when you import the module/package.
Finding the source code for built-in Python functions?
Is there a way to see how built in functions work in python? I don’t mean just how to use them, but also how were they built, what is the code behind sorted or enumerate etc.
8 Answers 8
Since Python is open source you can read the source code.
To find out what file a particular module or function is implemented in you can usually print the __file__ attribute. Alternatively, you may use the inspect module, see the section Retrieving Source Code in the documentation of inspect .
For built-in classes and methods this is not so straightforward since inspect.getfile and inspect.getsource will return a type error stating that the object is built-in. However, many of the built-in types can be found in the Objects sub-directory of the Python source trunk. For example, see here for the implementation of the enumerate class or here for the implementation of the list type.
@Quetzalcoatl the source code for sorted() is in /Python/bltinmodule.c although it just calls list.sort() so the real source is in /Objects/listobject.c
As a note to self, and for the future googlers: the open() functions is defined in Modules/_io/_iomodule.c in Python 3 (and not among the other builtins).
I tried input.__file__ but it says ‘builtin_function_or_method’ object has no attribute ‘_ _ file _ _’. :-\
Here is a cookbook answer to supplement @Chris’ answer, CPython has moved to GitHub and the Mercurial repository will no longer be updated:
- Install Git if necessary.
- git clone https://github.com/python/cpython.git
- Code will checkout to a subdirectory called cpython -> cd cpython
- Let’s say we are looking for the definition of print() .
- egrep —color=always -R ‘print’ | less -R
- Aha! See Python/bltinmodule.c -> builtin_print()
bltinmodule . Arrrrrrrrrgh. Why did they have to spell it so badly? I tried a quick filesystem search for builtin and came up with nothing!
I had to dig a little to find the source of the following Built-in Functions as the search would yield thousands of results. (Good luck searching for any of those to find where it’s source is)
Anyway, all those functions are defined in bltinmodule.c Functions start with builtin_
A list is an object/type, not a builtin function. You can find the implementation details for that in listobject.c github.com/python/cpython/tree/master/Objects
Looking for the implementation of builtin pow in bltinmodule.c, I only find an unhelpful static PyObject * builtin_pow_impl(PyObject *module, PyObject *base, PyObject *exp, PyObject *mod) < return PyNumber_Power(base, exp, mod); >. Is there an easy way to find where the actual algorithm implementation is hidden?
The iPython shell makes this easy: function? will give you the documentation. function?? shows also the code. BUT this only works for pure python functions.
Then you can always download the source code for the (c)Python.
If you’re interested in pythonic implementations of core functionality have a look at PyPy source.
PyPy uses RPython for most built-in stuff, which can be nearly as low-level as C to almost as high-level as Python. Usually it’s in between. In either case it’s statically typed, so it isn’t really Python.
- You can check usage about snippet using help()
- you can check hidden code for those modules using inspect
use inpsect module to explore code you want. NOTE: you can able to explore code only for modules (aka) packages you have imported
>>> import randint >>> from inspect import getsource >>> getsource(randint) # here i am going to explore code for package called `randint`
you can simply use help() command to get help about builtin functions as well its code.
for eg: if you want to see the code for str() , simply type — help(str)
>>> help(str) Help on class str in module __builtin__: class str(basestring) | str(object='') -> string | | Return a nice string representation of the object. | If the argument is a string, the return value is the same object. | | Method resolution order: | str | basestring | object | | Methods defined here: | | __add__(. ) | x.__add__(y) x+y | | __contains__(. ) | x.__contains__(y) y in x | | __eq__(. ) | x.__eq__(y) x==y | | __format__(. ) | S.__format__(format_spec) -> string | | Return a formatted version of S as described by format_spec. | | __ge__(. ) | x.__ge__(y) x>=y | | __getattribute__(. ) -- More --
Where can I get the source code of module __builtin__ in python?
Go to the folder you installed the Python libraries in, check under the version\Python folder — you should find the bltinmodule.c file.
Something along the lines of C:\python\2.X\Python
root@xenos ~> locate bltinmodule.c /usr/src/debug/Python-2.5.1/Python/bltinmodule.c /usr/src/debug/Python-2.6.4/Python/bltinmodule.c
I am on Ubuntu , not windows. Under my /usr/lib/python2.6 I don’t see any such file: find . -name ‘*.c’
I did this search with no results: sudo find / -name ‘bltinmodule.c’. Anyone else has an answer for Ubuntu ?
The easiest place to find the CPython source code is the web view of the Mercurial repository: http://hg.python.org/cpython/file/2.6/Python/bltinmodule.c
(Updated link to refer to the copy in Mercurial, although 2.6 security releases happened from the SVN repo)
For an Ubuntu-specific solution (though this should work on Debian, Mint, and other related distros), download the source and you’ll find bltinmodule.c there.
On my Ubuntu system, it is located under my python source directory, i.e., . /python2.6-2.6.6/Python/bltinmodule.c
apt-get source xxx will download the source for the specified package into a sub-directory of your current directory.
apt-get source python2.6 view ./python2.6-2.6.6/Python/bltinmodule.c
If you don’t want to use find , rebuild your locate database with sudo updatedb and then you can do a locate bltintmodule.c to find your file.
How can I find the location of the source code of a built-in Python method? [duplicate]
There are many built-in Python methods that I would like to study the source code of to understand. How can I find their location on my computer? Is there some simple command that I could run either in a Python script or in my terminal on my Linux that I could use to locate a built-in method’s source file?
2 Answers 2
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:
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 .
My example is the built-in string method isspace(). That does not require the importing of any additional core module. Where can I find the source for that?
The built-ins and other low-level functions are implemented in C for the obvious reasons of performance, so they are only available in bit compiled form. However, you can still see the source code for these functions by visiting the python source repo. This other answer is for Reference.
Particularly, in /Objects/stringobject.c , here, you can find the function, string_isspace() — the one you are looking for.
To get the file location of Python from the terminal:
But you can see the source code of a function simply by appending it with ?? (note that some functions are C compiled and not written in Python).
# Example 1: Built in compiled function. >>> open?? Docstring: open(name[, mode[, buffering]]) -> file object Open a file using the file() type, returns a file object. This is the preferred way to open a file. See file.__doc__ for further information. Type: builtin_function_or_method # Example 2: Pandas function written in Python. import pandas as pd >>> pd.DataFrame?? Init signature: pd.DataFrame(self, data=None, index=None, columns=None, dtype=None, copy=False) Source: class DataFrame(NDFrame): """ Two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Arithmetic operations align on both row and column labels. Can be thought of as a dict-like container for Series objects. The primary pandas data structure Parameters ---------- data : numpy ndarray (structured or homogeneous), dict, or DataFrame Dict can contain Series, arrays, constants, or list-like objects index : Index or array-like Index to use for resulting frame. Will default to np.arange(n) if no indexing information part of input data and no index provided columns : Index or array-like Column labels to use for resulting frame. Will default to np.arange(n) if no column labels are provided dtype : dtype, default None Data type to force, otherwise infer copy : boolean, default False Copy data from inputs. Only affects DataFrame / 2d ndarray input Examples -------- >>> d = >>> df = DataFrame(data=d, index=index) >>> df2 = DataFrame(np.random.randn(10, 5)) >>> df3 = DataFrame(np.random.randn(10, 5), . columns=['a', 'b', 'c', 'd', 'e']) See also -------- DataFrame.from_records : constructor from tuples, also record arrays DataFrame.from_dict : from dicts of Series, arrays, or dicts DataFrame.from_items : from sequence of (key, value) pairs pandas.read_csv, pandas.read_table, pandas.read_clipboard """ @property def _constructor(self): return DataFrame _constructor_sliced = Series @property def _constructor_expanddim(self): from pandas.core.panel import Panel return Panel .