LoginSignup
43
32

More than 3 years have passed since last update.

OpenCVをVisual Studio Codeで使う時にcv2モジュールのインテリセンスが表示されない問題

Last updated at Posted at 2019-06-01

概要

VScodeでcv2をimportするとcv2モジュール関連のインテリセンスが効かないという問題が発生していた。
結論としてpylintの引数に--extension-pkg-whitelist=cv2を指定することで解決した

動作環境

OS :Ubuntu16.04
python :3.5.2
opencv :4.1.0

問題

opencvをpipでインストールし、VScodeでいざ書こうとすると次のようなエラーが表示される。
Module 'cv2' has no なんとか member.
pipでopencvのモジュールをインストールしなおしても解決されなかった。
作ったpythonファイルをターミナルから実行すると問題なく動き、インテリセンスだけが表示されない問題が発生していた。

原因

この問題はpylint引数のextension-pkg-whitelistにcv2モジュールを指定していないことが原因だった。

対策

pylintがそもそも入っていない場合はpipでインストールする。

username@PC:~$ pip install pylint

pylintの設定ファイルを出力しておく

username@PC:~$ pylint --generate-rcfile > pylintrc

次にVScodeを開いて[ファイル]→[基本設定]→[設定]を開く
設定タブが開くので、設定の検索でPython.linting.pylintArgsを探す。
args.jpg
settings.jsonを開き、次のように記載する。

settings.json
    {
    "python.linting.pylintArgs": ["--extension-pkg-whitelist=cv2"],
    }

このように設定し、VScodeを再起動するとインテリセンスが効くようになる。
vscode.jpg

43
32
3

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
43
32