0
1

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 5 years have passed since last update.

Pythonでinstallしたモジュールが使えない、とか、importできない人向け(PYTHONPATHの話)

Last updated at Posted at 2018-10-24

モジュール入れたのに認識してくれないよぉー

そんな時は、

pythonを実行している環境で、↓を実行しましょう。
※IDEとかから起動してたらブレークしてから、↓を実行。コマンド起動してる人はpdbでブレークだ!

>>> import sys
>>> sys.path

パスの一覧が出てくると思うのですが、一覧の中に使いたいモジュールのフォルダの親フォルダが入っていますか。。?

例)使いたい hoge_module が↓に入ってる時、
C:\Users\xxxxx\AppData\Local\Programs\Python\Python36\Lib\site-packages\hoge_module
sys.pathの一覧の中に
C:\Users\xxxxx\AppData\Local\Programs\Python\Python36\Lib\site-packages
っていうのがあるかっていうこと。

ここを見てるっていうことは無いんじゃないかと思うのですが、そういう時は環境変数にPYTHONPATHを設定して、使いたいモジュールの親フォルダを設定しましょう。

windowsなら、(使いたいモジュールが、c:\hoge\hoge_module にいる場合)

set PYTHONPATH=c:\hoge

linuxなら、(使いたいモジュールが、/home/hoge/hoge_module にいる場合)

export PYTHONPATH="/home/hoge"

↑を実行した状態で、もう一度、

>>> import sys
>>> sys.path

パスの一覧の中にPYTHONPATHで設定したパスが出てくるはず。
そして使いたいモジュールが使えるようになっているはず。

まーでも、、

意図的にやってるわけじゃないのにPYTHONPATH設定しないと上手く動かない時は、自分の環境が正しい状態なのか振り返ってみた方が良いでしょう。。

参考リンク

公式のモジュール検索パスについての説明

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?