【トラブル】Pandas の環境設定でのトラブル

■ はじめに

https://dk521123.hatenablog.com/entry/2019/10/22/014957

で、Pandas を動かそうとしたらエラーになったので
その際のトラブルシューティングを記録しておく

目次

【1】エラー「ModuleNotFoundError: No module named 'pandas'」が表示
【2】エラー「UserWarning: mkl-service package failed to import」が表示
【3】エラー「ImportError: Missing required dependencies ['numpy']」が表示

【1】エラー「ModuleNotFoundError: No module named 'pandas'」が表示

Pythonを実行したら「import pandas as pd」で
以下「エラー内容」が表示される。

エラー内容

Traceback (most recent call last):
  File "xxxx.py", line 2, in <module>
    import pandas
ModuleNotFoundError: No module named 'pandas'

試したこと

なお、以下を行ったが解消されず。
 1) pip install pandas (pip reinstall pandas後に再インストールも)
 2) pip3 install pandas (pip3 reinstall pandas後に再インストールも)
 3) conda install pandas (conda reinstall pandas後に再インストールも)

原因

pythonの実行が、仮想環境のほうに向いてたため

解決案

# Windowsの場合
1) activate [仮想環境名] で仮想環境に入る
2) conda install pandas で Pandasをインストール
3) deactivate で仮想環境に出る

【2】エラー「UserWarning: mkl-service package failed to import」が表示

エラー内容

UserWarning: mkl-service package failed to import, 
therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation
under condition when Gnu OpenMP had already been loaded
 by Python process is not assured.
Please install mkl-service package, see http://github.com/IntelPython/mkl-service
・・・略・・・
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
  * The Python version is: Python3.6 from XXXX
  * The NumPy version is: "1.XX.XX"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: DLL load failed: 指定されたモジュールが見つかりません。

試したこと

1) エラー内容で「see http://github.com/IntelPython/mkl-service」って言ってるから
 上記のURLに載っている
 『conda install -c intel mkl-service, or conda install -c conda-forge mkl-service』の
 「conda install -c intel mkl-service」を実行。
 => エラーは変わらなかったが、インストールが実行されたので
  これも必要だった気がする

2) NumPyの再インストール
 => エラーは変わらなかったが、インストールが実行されたので
  これも必要だった気がする

解決案

環境変数 Path に「C:\Users\XXXX\Anaconda3\Library\bin」を追加

参考文献
https://qiita.com/HashiY/items/71b7d205624c2b9e128e

【3】エラー「ImportError: Missing required dependencies ['numpy']」が表示

エラー内容

Traceback (most recent call last):
  File "c:/Users/User/Documents/Python Scripts/demo.py", line 1, in <module>
    import pandas as pd
  File "C:\Users\User\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\__init__.py", line 19, in <module>
    "Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['numpy']

解決案

anaconda をアンインストールし、
最新のanacondaをインストールする

※ その際、環境変数 Path に古い設定があったので削除した

関連記事

Pandas ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/10/22/014957
Pandas で Excel を扱った際のトラブル
https://dk521123.hatenablog.com/entry/2021/07/03/000000