包含数据成员和方法的类
一组相关但彼此独立的操作函数
全局变量
路径搜索:指查找某个文件的操作,是动词
搜索路径:需要查找的一组目录,是名词
1
2
3
4
|
>>>
import
mymodules
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
ImportError: No module named mymodules
|
设置环境变量PYTHONPATH
在sys.path中添加搜索路径
1
2
3
|
>>>
import
sys
>>> sys.path
['
', '
/
usr
/
local
/
lib
/
python2.
7
/
dist
-
packages
/
pip
-
8.0
.
2
-
py2.
7.egg
', '
/
usr
/
local
/
lib
/
python2.
7
/
dist
-
packages
/
setuptools
-
3.3
-
py2.
7.egg
', '
/
usr
/
lib
/
python2.
7
', '
/
usr
/
lib
/
python2.
7
/
plat
-
x86_64
-
linux
-
gnu
', '
/
usr
/
lib
/
python2.
7
/
lib
-
tk
', '
/
usr
/
lib
/
python2.
7
/
lib
-
old
', '
/
usr
/
lib
/
python2.
7
/
lib
-
dynload
', '
/
usr
/
local
/
lib
/
python2.
7
/
dist
-
packages
', '
/
usr
/
lib
/
python2.
7
/
dist
-
packages
', '
/
usr
/
lib
/
python2.
7
/
dist
-
packages
/
PILcompat
', '
/
usr
/
lib
/
python2.
7
/
dist
-
packages
/
gtk
-
2.0
', '
/
usr
/
lib
/
python2.
7
/
dist
-
packages
/
ubuntu
-
sso
-
client']
|
1
2
3
4
5
6
|
>>>
import
my
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
ImportError: No module named my
>>> sys.path.append(
'/home/xpleaf/test'
)
>>>
import
my
|
1
2
|
>>> sys.modules
{
'copy_reg'
: <module
'copy_reg'
from
'/usr/lib/python2.7/copy_reg.pyc'
>,
'sre_compile'
: <module
'sre_compile'
from
'/usr/lib/python2.7/sre_compile.pyc'
>,...}
|
内建名称空间:由__builtins__模块中的名字构成,Python解释器启动时自动加载
全局名称空间:加载完内建名称空间后加载
局部名称空间:执行一个函数时会产生该名称空间,函数执行结束后就会释放
1
2
3
4
5
6
7
8
9
10
|
>>> foo
=
test()
>>> foo.name
=
'xpleaf'
>>> foo.loving
=
'cl'
>>>
>>> foo.name
'xpleaf'
>>> foo.loving
'cl'
>>> foo.__dict__
{
'name'
:
'xpleaf'
,
'loving'
:
'cl'
}
|
Python标准库模块
Python第三方模块
应用程序自定义模块
如果在一个模块的顶层导入,它的作用域是全局的
如果在函数中导入一个模块,它的作用域是局部的
如果模块是被第一次导入,它将加载并执行
1
2
3
|
>>>
import
test
Hello
>>>
import
test
|
1
|
from
module
import
name1[, name2[, ...nameN]]
|
1
2
|
from
flask
import
render_template, redirect, flash, \
url_for, request, current_app, jsonify
|
1
|
from
datetime
import
datetime as showtime
|
载入时执行模块
导入(import)和加载(load)
导入到当前名称空间
关于__future__
1
|
from
__future__
import
new_feature
|
警告框架
从ZIP文件中导入模块
“新的”导入钩子
__import__()
1
|
__import__
(name,
globals
=
{},
locals
=
{}, fromlist
=
[], level
=
-
1
)
|
1
|
sys
=
__import__
(
'sys'
)
|
globals()和locals()
1
2
3
4
5
6
7
|
>>>
def
sayHi(name):
...
print
'Hello, '
, name
...
>>>
globals
()[
'sayHi'
]
<function sayHi at
0x7f56dc7bf140
>
>>>
globals
()[
'sayHi'
](
'xpleaf'
)
Hello, xpleaf
|
reload()
1
2
3
4
5
6
|
>>>
import
test
Hello
>>>
import
test
>>>
reload
(test)
Hello
<module
'test'
from
'test.pyc'
>
|
模块必须是全部导入,而不能是使用from-import
reload()的参数是模块名字本身,而不是其字符串,即是reload(test)而不是reload('test')
1
2
3
4
5
6
7
8
9
10
11
|
import
Phone.Mobile.Analog
Phone.Mobile.Analog.dial()
from
Phone
import
Mobile
Mobile.Analog.dial(
'3245648'
)
from
Phone.Mobile
import
Analog
Analog.dial()
from
Phone.Mobile.Analog
import
dial
dial(
'3245648'
)
|
导入一个包,实际上是导入这个包的__init__.py文件(模块)
1
2
|
packageName
=
'Phone'
print
'Package <Phone>'
|
1
2
|
>>>
import
Phone
Package <Phone>
|
1
2
|
>>>
dir
()
[
'Phone'
,
'__builtins__'
,
'__doc__'
,
'__name__'
,
'__package__'
]
|
1
2
3
4
5
6
7
8
|
>>> Phone.Mobile
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
AttributeError:
'module'
object
has no attribute
'Mobile'
>>> Phone.Pager
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
AttributeError:
'module'
object
has no attribute
'Pager'
|
1
2
|
>>> Phone.packageName
'Phone'
|
在__init__.py中可以先导入一些模块或子包,使得可以通过上层包访问子包
1
2
|
import
Mobile
import
Pager
|
1
2
3
4
5
|
>>>
import
Phone
>>> Phone.Mobile
<module
'Phone.Mobile'
from
'Phone/Mobile/__init__.pyc'
>
>>> Phone.Pager
<module
'Phone.Pager'
from
'Phone/Pager/__init__.pyc'
>
|
1
2
3
4
|
>>> Phone.Mobile.Digital
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
AttributeError:
'module'
object
has no attribute
'Digital'
|
使用__all__变量来导入所有子包
1
2
3
|
import
Mobile
__all__
=
[
'Fax'
,
'Pager'
,
'Voicedta'
]
|
1
2
3
|
>>>
from
Phone
import
*
>>>
dir
()
[
'Fax'
,
'Pager'
,
'Voicedta'
,
'__builtins__'
,
'__doc__'
,
'__name__'
,
'__package__'
]
|
1
2
|
from
Package
import
Module
import
Package
|
1
|
from
.[Package
/
Module]
import
Modulce
/
Variable
|
必须是使用from import语句
from 关键字后面一定有句点标识.
只适用在包中
1
2
|
name
=
'xpleaf'
_loving
=
'cl'
|
1
2
3
4
5
6
7
8
9
10
11
|
>>>
from
manage
import
*
>>> name
'xpleaf'
>>> _loving
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
NameError: name
'_loving'
is
not
defined
>>>
>>>
import
manage
>>> manage._loving
'cl'
|
1
2
|
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
|
1
|
# coding: utf-8
|