Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read Python source code directly from IDE

Tags:

python

pycharm

I'm currently learning Python and I want to gain a deeper understanding of how python works by reading its source code.

I could manually go to the directory where Python is installed and check out the source code

enter image description here

I was wondering, is it possible to read Python source code directly from IDE such as PyCharm?

I tried to control click on a method name, even though it did bring me to the method definition page, it did not contain any implementation code

enter image description here

Edit 1

I understand a large of python (cpython to be exact) is implemented in c. Is there anyway to read the c code in IDE such as PyCharm?

like image 219
Thor Avatar asked Dec 20 '17 07:12

Thor


People also ask

How do you view source code in Python?

These are mostly compiled sources which means only the bytecode is used by the Interpreter. Not all the functions are written in C, most of it is actually written in pure python. One way to view source is through terminal by using ipython . Alternatively you can find what you want, by using inspect.

What is the source code in Python?

Source code is the list of human-readable instructions that a programmer writes—often in a word processing program—when he is developing a program. The source code is run through a compiler to turn it into machine code, also called object code, that a computer can understand and execute.

How do you write source code in Python?

By default, TextEdit stores files in Rich Text Format. That means you can do things like bold or italics, or even underline. But this is not suitable for writing source code. To change it, we're going to go over to TextEdit, choose Preferences, and then we're going to choose plain text under the Format section.


1 Answers

Normally PyCharm does what you want, unless the Ctrl+Click leads you to a C function. In this case you will get only minimal information on the function signature and return value, etc., just like you show in your post. built-ins are mainly written in C for an efficiency reason, so if you want to see the C code you'll have to download CPython source code and dive into it. Another alternative would be to download the PyPy source code and take a look at it. Still, you can use PyCharm to read code from modules written in Python in the standard library and that would be helpful in your learning process. Believe me, you'll find good quality code there.

like image 155
lpozo Avatar answered Oct 21 '22 11:10

lpozo