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