pytho模块的加载顺序

        当前目录如果有同名的系统模块,那么当前目录的模块会被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 <module>
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    

转载于:https://www.cnblogs.com/68xi/p/8616360.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值