Help us understand the problem. What is going on with this article?

Pythonでtest.pyを作るな!

More than 5 years have passed since last update.

「え? なんで?」と思われた方もいらっしゃるかもしれませんが、Pythonの標準ライブラリには、testというパッケージがあるので、test.pyやtest.pyがimportするモジュールが、このパッケージを利用するとエラーが発生します。test.pyという名前のスクリプトは極力作らない方がいいです。

実際に試したのが下記のログです。

$ echo from test import support >test.py
$ python test.py
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from test import support
  File "/tmp/tmp.TWTFmPjXqY/test.py", line 1, in <module>
    from test import support
ImportError: cannot import name support
$ mv test.py test_.py
$ python test_.py
$ 

test.pyでfrom test import supportを実行するとImportErrorが発生しますが、test_.pyにリネームしてから実行すればエラーにならないことが分かります。

以上、前に引っかかって気をつけていたのに、うっかり忘れて再度引っかかったので自戒を込めて書いたTipsでした。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした