How to view the source code of a python program

Release: 2019-07-08 09:13:12
Original
7170 people have browsed it

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'
>>>
Copy after login

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())
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template