1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pythonのプログラム名をhttp.pyにしてはいけない

Posted at
http.py
import urllib.request
req = urllib.request.Request("https://hoge")

このプログラムを実行するとエラーになります。

$ python3 http.py
Traceback (most recent call last):
  File "/home/norioy01/http.py", line 1, in <module>
    import urllib.request
  File "/usr/lib/python3.9/urllib/request.py", line 88, in <module>
    import http.client
  File "/home/norioy01/http.py", line 3, in <module>
    req = urllib.request.Request("https://hoge")
AttributeError: module 'urllib' has no attribute 'request'
$ 

エラーの原因はimportがループする為です。

  1. http.pyのimport urllib.requestでurllib/request.pyが実行。
  2. urllib/request.pyのimport http.clientでhttp.pyが実行。
urllib/request.py
#・・・省略・・・
import http.client
#・・・省略・・・

プログラム名を変更すれば正常に動きます。

$ mv http.py hoge.py
$ 
$ python3 hoge.py
$ 
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?