現象
Visual Studio Code から pytest を使用したところ、 site-packages のモジュール読み込み時に importError が発生した。
$ pytest test/url/test_list.py
======================================================================================= test session starts ========================================================================================
platform darwin -- Python 3.10.6, pytest-7.2.1, pluggy-1.0.0
rootdir: /Users/centipede/works/sample
collected 0 items / 1 error
============================================================================================== ERRORS ==============================================================================================
______________________________________________________________________________ ERROR collecting test/url/test_list.py ______________________________________________________________________________
ImportError while importing test module '/Users/centipede/works/sample/test/url/test_list.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../.pyenv/versions/3.10.6/lib/python3.10/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
test/url/test_list.py:3: in <module>
import requests
E ModuleNotFoundError: No module named 'requests'
===================================================================================== short test summary info ======================================================================================
ERROR test/url/test_list.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
========================================================================================= 1 error in 0.04s =========================================================================================
エラーメッセージには、pyenv でインストールしている 3.10.6 の pytest が実行されている。しかし、VS Code の Python: Select Interpreter
で 3.10.3 を選択していた。
環境
複数の異なるバージョンをもつ python (3.10.3 と 3.10.6) を pyenv でインストールしていた。バージョンは 3.10.3 を選択。
$ pyenv versions
system
* 3.10.3 (set by /Users/centipede/.pyenv/version)
3.10.6
回避策
利用したいバージョンの python 以外をアンインストールしたら、 importError は発生しなくなった。
今回は Python 3.10.3 と 3.10.6 が pyenv でインストールされていた。そして、バージョン 3.10.6 をアンインストールし、pyenv 環境に 3.10.3 を残した。
$ pyenv uninstall 3.10.6
pyenv: remove /Users/centipede/.pyenv/versions/3.10.6? [y|N] y
pyenv: 3.10.6 uninstalled
$ pyenv versions
system
* 3.10.3 (set by /Users/centipede/.pyenv/version)
$ pytest test/url/test_list.py
======================================================================================= test session starts ========================================================================================
platform darwin -- Python 3.10.3, pytest-7.1.2, pluggy-1.0.0
rootdir: /Users/centipede/works/sample
collected 2 items
test/url/test_list.py .. [100%]
======================================================================================== 2 passed in 1.25s =========================================================================================
推測される原因
VS Code の設定で、使用する Python (今回は 3.10.3 ) を選択しても、VS Code 内で pytest を実行するときは、デフォルトの (推薦される) バージョンの Python (3.10.6) で pytest が実行されている可能性がある。