"Learn Python with me" python sailing

Source: Internet
Author: User
Tags gtk

Say that the high-rise building on the ground, will not go before you learn to crawl. Especially for me, such as the side of the side of the class, no matter how to first code, wait until I wait for the side of the wings of the hard to test first.

1, how do I run python?

First to python.org download the Python installation file to say, brother you slow is org not COM careful amount, of course there are Chinese download links ...! Then you can next,next,finish, and when let you know it's the Windows platform (and of course ActivePython is also excellent). Under Linux may have been ready for you, I use the Ubuntu10.04.3 is 2.6 temporary enough. If you feel you want to be up-to-date then download the source code, CMMS (./configuration, make, do install). If all goes well, then you can open Python's idle under My indows, the Linux comparison is simple and straightforward on the command just fine, Python. (if not, then struggle to get to the path.)

2. With the environment, start code.

(1) in the command-line environment of interpretation, play it.

(2) Of course 1, can not meet all our needs one but want to write some decent program or save the file. After you've written it, save it, filename.py it, and then, on the command line, CD to your Python source file, Python filename.py, and the cute Python begins to work.

(3) The way to script is to bind to our environment.

Specifies the location of Python in the python environment variable, such as Ubunut, Python=>/usr/bin/python, Windows below: c:\python2.7\ Python.exe, edit the python source file at the beginning of the file Binding Python command.

At the beginning of the file #! /usr/bin/python, or #!. C:\python2.7\python.exe, and then save the source file (you can use the. PY end!) ),

If you have permission to modify the source file in Linux, sudo chmod 755 filename and then./filename can work!

3. Python module

When we or others have completed a set of functions, we can share each other. How to call someone else's module? For example, hello_world.py (saved in/home

/tom/python/hello_world.py)

Python code
    1. title = "I love Python"

If we think about using the title variable:

Open the Python interpreter:

Python code
  1. >>> Import sys
  2. >>> Sys.path
  3. [‘‘,'/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg ','/usr/local/lib/python2.6/dist-packages/virtualenv-1.7-py2.6.egg ','/usr/local/lib/python2.6/dist-packages/django-1.3.1-py2.6.egg ','/usr/local/lib/python2.6/dist-packages/tornado-2.1.1-py2.6.egg ','/usr/local/lib/python2.6/dist-packages/pastescript-1.7.5-py2.6.egg ','/usr/local/lib/python2.6/dist-packages/pastedeploy-1.5.0-py2.6.egg ','/usr/local/lib/python2.6/dist-packages/paste-1.7.5.1-py2.6.egg ','/usr/local/lib/python2.6/dist-packages/cherrypy-3.2.2-py2.6.egg ', '/usr/lib/python2.6 ',   '/usr/lib/python2.6/plat-linux2 ',  < Span class= "string" > '/USR/LIB/PYTHON2.6/LIB-TK ',   '/usr/lib/python2.6/lib-old ',  < Span class= "string" > '/usr/lib/python2.6/lib-dynload ',  /usr/lib/python2.6/ Dist-packages ',   '/usr/lib/python2.6/dist-packages/pil ',   '/usr/lib/pymodules/python2.6 ',  < Span class= "string" > '/usr/lib/python2.6/dist-packages/gtk-2.0 ',  /usr/lib/pymodules/ Python2.6/gtk-2.0 ',   '/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode ',  < Span class= "string" > '/usr/local/lib/python2.6/dist-packages ']  

We looked at the path of the Python system load module, but obviously there is no hello_world.py file path, so we will not find hello_world.py, so we will add the Hello_world path, as long as:

Python code
    1. Sys.path.append ('/home/tom/python ')

Then we can import the Python module,

Python code
    1. Import Hello_world
    2. Print Hello_world.title
    3. From Hello_world Import title
    4. Print title

Two different ways.

When the module file is first imported into the Python session, the Python interpreter executes the code in the module completely, and if you import the same module again in Python's interactive environment, Python will no longer execute, all the code that changes the module during that time, Python is still the old code in the interactive environment. If you need to load new code, you need to use reload

Such as:

Python code
    1. Reload (Hello_world)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.