Finding the source code for a Python module

2019-05-28 17:17发布

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?

1条回答
别忘想泡老子
2楼-- · 2019-05-28 18:06

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.

查看更多
登录 后发表回答