Home >Backend Development >Python Tutorial >How to view the source code of a python program

How to view the source code of a python program

尚
Original
2019-06-28 16:06:307194browse

How to view the source code of a python program

If you know the source code location, you can use the following code to view the source code:

>>> import string
>>> string.__file__
'/usr/lib/python2.7/string.pyc'
>>>

The string.py in the corresponding directory is the source code of the package, but some libraries are written in C Yes, an error will be prompted. For such a library, you need to download the python source code and look directly at the c source file.

If you don’t know the source code location, you can use the following code:

import module_name
with open(str(module_name.__file__),"r") as f:
print (f.read())

Then paste the code into the IDE.

For more Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of How to view the source code of a python program. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn