群里讨论之后,我这边尝试的时候发现了一个特征
在 Linux 环境的 Python 3,只 import urllib
,然后第一次访问 urllib.request 提示找不到,第二次访问的时候是正常的。
qing@qing-win:/mnt/c/Users/qing/Desktop$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> urllib.request
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'urllib' has no attribute 'request'
>>> urllib.request
<module 'urllib.request' from '/usr/lib/python3.6/urllib/request.py'>
>>> urllib.request
<module 'urllib.request' from '/usr/lib/python3.6/urllib/request.py'>
Windows 环境,两次访问这个属性都提示找不到:
PS C:\Users\qing\Desktop> python3
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> urllib.request
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'urllib' has no attribute 'request'
>>> urllib.request
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'urllib' has no attribute 'request'
而直接 import urllib.request
然后是可以的
PS C:\Users\qing\Desktop> python3
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib.request
>>> urllib.request
<module 'urllib.request' from 'C:\\Python36\\lib\\urllib\\request.py'>
Python用的不熟悉,猜测可能是一些模块加载的机制的问题?抛砖引玉 ?
(import requests
的路过。。。