About Modules

Modules Are Programs. Any Python program can be imported as a module. First you must keep in mind that modules are used to define things and reuse code.

1) the .pyc file
When you import a module, you may notice that a new file appears, like c:\python\hello.pyc. The file with the .pyc extension  is a (platform-independent) processed (“compiled”) Python file that has been translated to a format that Python can handle more efficiently.  If you import the same module later, Python will import the .pyc file rather than the .py file, unless the .py file has changed; in that case, a new .pyc file is generated. Deleting the .pyc file does no harm (as long as there is an equivalent .py file available)—a new one is created when needed.

2) Making Your Modules Available

There are two ways of doing this: put your module in the right place or tell the interpreter where to look. 

1 Put your module in the right place

>>> import sys, pprint
>>> pprint.pprint(sys.path)

2 Tell the interpreter where to look

One way of doing this is to edit sys.path:

>>> import sys
>>> sys.path.append('c:/python') #using the Windows directory
>>> sys.path.append('~/python')  #using the linux directory

but that is not a common way to do it. The standard method is to include your module directory (or directories) in the environment variable PYTHONPATH.

In UNIX and Mac OS X, you will probably set environment variables in some shell file that is executed every time you log in. If you use a shell such as bash, the file is .bashrc, found in your home directory. Add the following to that file to add the directory ~/python to your PYTHONPATH:
export PYTHONPATH=$PYTHONPATH:~/python

Note that this usage only work for bash. if you use IDE, you can use some other way to add the new python path.

3)Packages

To structure your modules, you can group them into packages. A package is basically just another type of module. The interesting thing about them is that they can contain other modules. While a module is stored in a file (with the file name extension .py), a package is a directory. To make Python treat it as a package, it must contain a file (module) named __init__.py. The contents of this file will be the contents of the package, if you import it as if it were a plain module. For example, if you had a package named constants, and the file constants/__init__.py contains the statement PI = 3.14, you would be able to do the following:

import constants
print constants.PI

To put modules inside a package, simply put the module files inside the package directory.

4) Explore Modules

1 Using dir

To find out what a module contains, you can use the dir function, which lists all the attributes of an object (and therefore all functions, classes, variables, and so on of a module)

>>> import copy
>>> dir(copy)

2 The __all__ variable 

It store the public interfaces of the module.

>>> import copy
>>> copy.__all__
['Error', 'copy', 'deepcopy']
More specifically, it tells the interpreter what it means to import all the names from this module. So if you use this:

from copy import *

you get only the three functions listed in the __all__ variable.

To import PyStringMap(not a default public interface), for example, you would need to be explicit, by either importing copy and using copy.PyStringMap, or by using from copy import PyStringMap ".If you don’t set __all__, the names exported in a starred import defaults to all global names in the module that don’t begin with an underscore.

3 Get help with help()

>>> help(copy.copy)
4 Use the documentation

>>> print range.__doc__
range([start,] stop[, step]) -> list of integers ……
5 Use the source
>>> print copy.__file__
/usr/lib/python2.7/copy.pyc #linux environment
Reading source code is, in fact, one of the best ways to learn Python—besides coding yourself.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值