how to view source code of programs (python)
Python newbie here....I was interested in writing a basic touch typing tutor program in python. I was wondering what is the best way to download source code of applications that people have written that are of a similar nature (to use as a guide to learn from). I went to http://pypi.python.org/ and downloaded some packages but I'm not quite sure how t开发者_如何转开发o view the actual python code. Which file are you supposed to open. I found a run.py file in one of them and it doesn't seem to even work when I try to run it. I'm sure I'm missing something here.
Thanks!
You can also use inspect
to view the code from the interpreter. So say your file is named "test.py" and it's on your PYTHONPATH
(if you don't know what that is, then make sure you are in the same directory as "test.py").
>>> import inspect
>>> import test # this imports test.py
>>> print inspect.getsource(test)
def hello(name):
print("Hello %s" % name)
If you were using IPython
, then you could also do:
In [1]: import test
In [2]: test??
You can't edit the code like this... but you can view it.
There is usually a README explaining things.
If not, the src/ directory is where you should start looking and open some files with a text editor like Notepad++.
Double-clicking a .py script will execute the script (and may not send any useful results to the screen). To view the source you can open a .py file with IDLE (which comes with Python) or even Notepad although a more advanced text editor or IDE is recommended. See Is there a good, free Python IDE for Windows for IDE recommendations by the stackoverflow community. Good luck, and welcome to the Python community!
精彩评论