Python Learning Notes (v)-modules

Source: Internet
Author: User

I. Using modules

Introducing the SYS module, using the Import

#!/usr/bin/env python#-*-coding:utf-8-*-' a test module ' __author__ = ' Michael Liao ' import sysdef Test ():    args = s YS.ARGV    If Len (args) ==1:        print ' Hello, world! '    Elif len (args) ==2:        print ' Hello,%s! '% args[1]    else:        print ' Too many arguments! ' If __name__== ' __main__ ':    Test ()
Running Python hello.py Michael gets the sys.argv is [' hello.py ', ' Michael ']

Variables that use underscores _ as prefixes in Python are private variables and can only be used inside the module.

Variables such as __xx__ are special variables, and our own variables do not typically use this variable name.


Two. Installing a third-party module

When we try to load a module, the Python parser searches the current directory, all installed built-in modules, and third-party modules, and the search path is stored in the PATH variable of the SYS module.

>>> Import sys>>> Sys.path

If we want to add our own search directory, there are two ways:

1. Directly modify the Sys.path, add the directory to be searched Sys.path.append ('/users/imchael/my_py_scripts ')

This method is modified at run time and expires after the run is finished

2. Set the environment variable Pythonpath


Three. Using __future__

Since subsequent versions of Python are often incompatible with the previous version, in order to smooth the transition to a new version, Python provides a __future__ module that lets you experiment with some features of the new version in the old version:

If you want the ring to use python3.x's division in the python2.7 code, you can use the Division implementation of the __FUTURE__ module:

From __future__ import divisionprint ' 10/3 = ', 10/3  # 10/3 = 3.33333333333print ' 10.0/3 = ', 10.0/3  # 10. 0/3 = 3.33333333333print '//3 = ', +//3  # 10//3 = 3

Python Learning Notes (v)-modules

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.