Memo

メモ > 技術 > プログラミング言語: Python > Windows上に開発環境を構築

■Windows上に開発環境を構築
■XAMPPでPythonを動作させるメモ Pythonディストリビューション使い分けのポイントを考えてみよう(Windows編):Python環境構築入門(1/3 ページ) - @IT https://atmarkit.itmedia.co.jp/ait/articles/2203/23/news027.html Windows環境へのPythonインストールは、上記のように複数の方法がある 後々のことを考えれば、Anacondaでインストールしておくのが無難か Python | Pythonのダウンロードとインストール https://www.javadrive.jp/python/install/index1.html 今回はAnacondaを使ってインストールした Anacondaのインストール | Python入門 https://www.javadrive.jp/python/install/index5.html Anaconda で Python 環境をインストールする - Qiita https://qiita.com/t2y/items/2a3eb58103e85d8064b6 【Python】非エンジニアWindowユーザー向けのAnacondaインストールの手順 https://tonari-it.com/python-anaconda-install/ ■コマンプロンプトで動作確認 コマンドプロンプトから、以下を実行してインストールを確認
>C:\Users\refirio\Anaconda3\python --version Python 3.7.3
以下のようにして直接命令を実行できる
>C:\Users\refirio\Anaconda3\python >>> print('Hello Python') Hello Python >>> quit()
hello.py を作成して以下の内容を記述
print("Hello! Python.")
以下のようにしてプログラムを実行
>C:\Users\refirio\Anaconda3\python hello.py Hello! Python.
■コマンプロンプトで動作確認(補足) 上で紹介したとおり、コマンドプロンプトから以下を実行してインストールを確認できる
>C:\Users\refirio\Anaconda3\python --version Python 3.7.3
Windowsの検索で「Anaconda Prompt」を検索して実行すると、パスの指定なしでも実行できる
>python --version Python 3.10.9
■CGIで動作確認 あらかじめ、XAMPPをインストールしておく C:\xampp\apache\conf\httpd.conf を編集し、Apacheを再起動する
AddHandler cgi-script .cgi .pl .asp ↓ AddHandler cgi-script .cgi .pl .asp .py
test.py を作成して以下の内容を記述
#!C:\Users\refirio\Anaconda3\python # -*- coding: utf-8 -*- print("Content-Type: text/html\n") print("Hello World")
以下のようにしてプログラムを実行 http://localhost/test/test.py ■文字化け対策 Python 3.x - Pythonにて文字化けが発生します|teratail https://teratail.com/questions/130659 PythonのCGIで日本語が文字化けしたときの対処法 - Qiita https://qiita.com/eleven-junichi2/items/f3fcb6abe7fe21a4d89a
#!/Users/refirio/Anaconda3/python # coding: utf-8 # -*- coding: utf-8 -*- import sys,io sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') print("Content-Type: text/html; charset=UTF-8\n") print("<!DOCTYPE html>") print("<html>") print("<head>") print("<meta charset=\"utf-8\">") print("<title>Python</title>") print("</head>") print("<body>") print("<h1>Python</h1>") print("<p>これはPythonの実行結果です。</p>") print("</body>") print("</html>")
■pip(Pythonのパッケージ管理システム) pipとpipenvとpoetryの技術的・歴史的背景とその展望 - Stimulator https://vaaaaaanquish.hatenablog.com/entry/2021/03/29/221715 Python - Pythonの「pip」と「pip3」は何が違う?|teratail https://teratail.com/questions/46066 PythonでOpenCVをはじめる(Windows10、Anaconda 2018.12、Python3.7.1、OpenCV4.0.0) - Qiita https://qiita.com/SatoshiGachiFujimoto/items/94da93f88578b87f6a89 Windowsの検索機能などから「Anaconda Prompt」を開く 以下のようにして、Pythonとpipのバージョンを確認できる
>python --version Python 3.7.3 >python -m pip -V pip 19.1.1 from C:\Users\refirio\Anaconda3\lib\site-packages\pip (python 3.7)
pipでライブラリをインストールした場合、以下のようにすればPythonプログラムの簡易な動作確認が
>python Python 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print('Hello!') Hello! >>> quit Use quit() or Ctrl-Z plus Return to exit >>> exit()
■Excelを操作 ※未検証 ゼロからはじめるPython(65) PythonでExcelを操作する二大手法を比較しよう | マイナビニュース https://news.mynavi.jp/article/zeropython-65/ PythonによるExcel自動化は何がスゴい?インストールからコードサンプルまで実践詳解 |ビジネス+IT https://www.sbbit.jp/article/cont1/46162 ■OpenCV PythonでOpenCVをはじめる(Windows10、Anaconda 2018.12、Python3.7.1、OpenCV4.0.0) - Qiita https://qiita.com/SatoshiGachiFujimoto/items/94da93f88578b87f6a89 Pythonを用いた画像処理(openCV,skimage) - Qiita https://qiita.com/taka_baya/items/453e429b466ffaa702c9
>pip install opencv-python Successfully installed opencv-python-4.1.1.26 >pip install opencv-contrib-python Successfully installed opencv-contrib-python-4.1.1.26 >python Python 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '4.1.1' >>> exit()
インストールできたが、CGIからサンプルプログラムを実行するとエラーになった エラーの内容は以下のとおり
[Sun Sep 22 14:11:11.801745 2019] [cgi:error] [pid 13672:tid 1960] [client ::1:52117] End of script output before headers: test.py, referer: http://localhost/~raspberrypi/cgi/opencv/ [Sun Sep 22 14:11:11.801745 2019] [cgi:error] [pid 13672:tid 1960] [client ::1:52117] AH01215: ImportError: numpy.core.multiarray failed to import\r: C:/localhost/home/raspberrypi/public_html/cgi/opencv/test.py, referer: http://localhost/~raspberrypi/cgi/opencv/ [Sun Sep 22 14:11:11.802745 2019] [cgi:error] [pid 13672:tid 1960] [client ::1:52117] AH01215: Traceback (most recent call last):\r: C:/localhost/home/raspberrypi/public_html/cgi/opencv/test.py, referer: http://localhost/~raspberrypi/cgi/opencv/ [Sun Sep 22 14:11:11.802745 2019] [cgi:error] [pid 13672:tid 1960] [client ::1:52117] AH01215: File "C:/localhost/home/raspberrypi/public_html/cgi/opencv/test.py", line 5, in <module>\r: C:/localhost/home/raspberrypi/public_html/cgi/opencv/test.py, referer: http://localhost/~raspberrypi/cgi/opencv/ [Sun Sep 22 14:11:11.802745 2019] [cgi:error] [pid 13672:tid 1960] [client ::1:52117] AH01215: import cv2\r: C:/localhost/home/raspberrypi/public_html/cgi/opencv/test.py, referer: http://localhost/~raspberrypi/cgi/opencv/ [Sun Sep 22 14:11:11.803737 2019] [cgi:error] [pid 13672:tid 1960] [client ::1:52117] AH01215: File "C:\\Users\\refirio\\Anaconda3\\lib\\site-packages\\cv2\\__init__.py", line 3, in <module>\r: C:/localhost/home/raspberrypi/public_html/cgi/opencv/test.py, referer: http://localhost/~raspberrypi/cgi/opencv/ [Sun Sep 22 14:11:11.803737 2019] [cgi:error] [pid 13672:tid 1960] [client ::1:52117] AH01215: from .cv2 import *\r: C:/localhost/home/raspberrypi/public_html/cgi/opencv/test.py, referer: http://localhost/~raspberrypi/cgi/opencv/ [Sun Sep 22 14:11:11.803737 2019] [cgi:error] [pid 13672:tid 1960] [client ::1:52117] AH01215: ImportError: numpy.core.multiarray failed to import\r: C:/localhost/home/raspberrypi/public_html/cgi/opencv/test.py, referer: http://localhost/~raspberrypi/cgi/opencv/
エラーメッセージによると、numpy.core.multiarray を使えないらしい 以下を参考に numpy をアップデートすると大丈夫だった 【Pythonエラー対策】ImportError: numpy.core.multiarray failed to import | 西住工房 https://algorithm.joho.info/programming/python/numpy-core-multiarray-failed-to-import/
>pip install numpy --upgrade
なお、プログラム実行ディレクトリに、numpy.py を自身で作成していると、そちらが読み込まれる 結果的に正しいファイルを読み込めずにエラーになるので注意 ■OpenCV(バージョン3をインストール) 上記「OpenCV」の手順でインストールすると、4.1.1 がインストールされた 今回は本番環境に合わせて 3.4.3 にバージョンダウンするものとする 【Jupyter Notebook】OpenCVのインポートエラー解決メモ - Qiita https://qiita.com/yut-nagase/items/27b0a17e9e3074a95c6c 以下のようにしてアンインストール&インストールできた
>pip uninstall opencv-python >pip install opencv-python==3.4.3.18
なお、以下のバージョン指定だと「バージョンの指定が不十分」となる エラーメッセージをもとに、マイナーバージョンを指定する
>pip install opencv-python==3.4.3 Collecting opencv-python==3.4.3 ERROR: Could not find a version that satisfies the requirement opencv-python==3.4.3 (from versions: 3.4.2.16, 3.4.2.17, 3.4.3.18, 3.4.4.19, 3.4.5.20, 3.4.6.27, 3.4.7.28, 4.0.0.21, 4.0.1.23, 4.0.1.24, 4.1.0.25, 4.1.1.26) ERROR: No matching distribution found for opencv-python==3.4.3
■Jupyter Notebook ※未検証 【初心者向け】Jupyter Notebookの使い方!インスト…|Udemy メディア https://udemy.benesse.co.jp/development/python-work/jupyter-notebook.html ブラウザ上でプログラムを実行できるようになるらしい Googleの「Colaboratory」やAWSの「SageMaker Studio Lab」を使えば、同じような環境が提供される ■その他メモ 2020年5月におけるPython開発環境の選択肢 - Qiita https://qiita.com/nicco_mirai/items/80ba4b4bf9db11ac54c6 2020 年の Python パッケージ管理ベストプラクティス - Qiita https://qiita.com/sk217/items/43c994640f4843a18dbe

Advertisement