2008年6月26日 星期四

module

a python file is a module

use import to import module:
ex:
import  test
this will import test.py

a module is imported only once per process by default. Further imports reuse loaded modules in memory
use sys.modules.keys() to find loaded module

when module is imported, the module is executed

module search path:
import modules in other directories:
set PHTYONPATH

import and from:
1. only use import:
ex:
import test
test.run()

2. use import and from
ex:
from test import run
run()
now we can reference run() without test
ex:
from test import *
now we can reference any attribute of module test

show module's attribute
ex:
test.__dict__.keys()

reload:
ex:
import test
reload(test)

module packages:
import  dir1.dir2.test
dir1 and dir2 are directories,  the file imported is test.py
dir1 and dir2 must both contain  __init__.py
dir1 must under the directory of python search path

沒有留言: