python3.4和3.5的区别_Python导入阴影在3.4.6和3.5.2之间不同

When a module named spam is imported, the interpreter first searches

for a built-in module with that name. If not found, it then searches

for a file named spam.py in a list of directories given by the

variable sys.path.

(强调我的)

time不是Python 3.4中的内置模块模块,但在Python 3.5中有所改变:

me@temp:~$ python3.4 -c 'import sys; print("time" in sys.builtin_module_names)'

False

me@temp:~$ python3.5 -c 'import sys; print("time" in sys.builtin_module_names)'

True您可以看到引入更改here的补丁(与issue 5309相关)。考虑到更改日志mentions the issue 5309,但没有说什么重新。在time模块中,可以肯定地说这个改变是一个副作用,是CPython的一个实现细节。

由于time不是CPython 3.4中的内置模块,并且sys.path中的第一个目录是当前脚本目录,from time import time会尝试从您的time.py文件中导入time属性,但是会失败并抛出ImportError。

在CPython 3.5中,time是一个内置模块。根据上面的引用,运行from time import time成功导入内置模块,而无需在sys.path上搜索模块。

如果您从标准库中隐藏非内置模块(例如inspect),则两个CPython版本都会引发相同的错误:

me@temp:~$ cat inspect.py

from inspect import signature

me@temp:~$ python3.4 -c 'import inspect'

Traceback (most recent call last):

File "", line 1, in

File "/home/me/inspect.py", line 1, in

from inspect import signature

ImportError: cannot import name 'signature'

me@temp:~$ python3.5 -c 'import inspect'

Traceback (most recent call last):

File "", line 1, in

File "/home/me/inspect.py", line 1, in

from inspect import signature

ImportError: cannot import name 'signature'

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值