当前目录如果有同名的系统模块,那么当前目录的模块会被import,系统模块会被忽略,如:
1 ghostwu@ghostwu:~/python/module$ ls
2 import_test.py string.py
3 ghostwu@ghostwu:~/python/module$ cat string.py
4 #!/usr/bin/python
5 #coding:utf-8
6
7 def add( a, b ):
8 return a + b
9 ghostwu@ghostwu:~/python/module$ cat import_test.py
10 #!/usr/bin/python
11 #coding:utf-8
12 import string
13 str = 'ghostwu'
14 print string.capitalize( str )
15
16 ghostwu@ghostwu:~/python/module$ python import_test.py
17 Traceback (most recent call last):
18 File "import_test.py", line 8, in
19 print string.capitalize( str )
20 AttributeError: 'module' object has no attribute 'capitalize'
21 ghostwu@ghostwu:~/python/module$ ls -a
22 . .. import_test.py string.py string.pyc
在当前目录下,定义了一个同名的string模块( 指的是与系统的string模块同名 ),由于执行的时候,当前目录的模块被import了,所以识别不了系统string模块的方法capttalize.
只要删除目录下的string.py string.pyc,就能正常import系统的模块
1 ghostwu@ghostwu:~/python/module$ ls -a
2 . .. import_test.py string.py string.pyc
3 ghostwu@ghostwu:~/python/module$ rm string.py string.pyc
4 ghostwu@ghostwu:~/python/module$ ls -a
5 . .. import_test.py
6 ghostwu@ghostwu:~/python/module$ python import_test.py
7 Ghostwu