実現したいこと
Python3でモジュールをインポートしたい
前提
Mac上のVirtualBoxでCentOS7をインストールし、webサーバーのテスト環境を構築しています。
CGIとしてPython3をMacのwebブラウザから走らせるとこまでは行きました。
ところが、CentOS7の端末でパッケージをpip3でインストールしてもインポートがされず、エラーになってしまい困っています。
恐らく、環境変数で解決するのだと思い、パッケージのインストール先を設定してみたのですが、変わらずエラーになってしまいます。
発生している問題・エラーメッセージ
webブラウザ
1Internal Server Error 2 3The server encountered an internal error or misconfiguration and was unable to complete your request. 4 5Please contact the server administrator at root@localhost to inform them of the time this error occurred, and the actions you performed just before this error. 6 7More information about this error may be available in the server error log.
/var/log/httpd/error_log [Sun Jul 23 19:12:54.769334 2023] [cgi:error] [pid 1642] [client 192.168.1.97:57508] AH01215: Traceback (most recent call last): [Sun Jul 23 19:12:54.769418 2023] [cgi:error] [pid 1642] [client 192.168.1.97:57508] AH01215: File "/var/www/html/user-new.py", line 5, in <module> [Sun Jul 23 19:12:54.769451 2023] [cgi:error] [pid 1642] [client 192.168.1.97:57508] AH01215: import requests # conda install requests [Sun Jul 23 19:12:54.769479 2023] [cgi:error] [pid 1642] [client 192.168.1.97:57508] AH01215: ModuleNotFoundError: No module named 'requests' [Sun Jul 23 19:12:54.775795 2023] [cgi:error] [pid 1642] [client 192.168.1.97:57508] End of script output before headers: user-new.py
該当のソースコード
Python3
1#!/usr/local/bin/python3.7 2# coding:utf-8 3 4import cgi 5import requests # conda install requests 6#import sys 7#import json 8#import mysql.connector # conda install mysql-connector-python 9#import string 10#import secrets 11 12#randomSize = 64 # randomのバイト数 13 14print('Content-Type: text/html\n\n') 15print('Status: 200 OK')
試したこと
/var/www/html/user-new.py
/var/www/html/
ともにパーミッションは755です。
CentOS7の端末
# pip3 install requests
Defaulting to user installation because normal site-packages is not writeable Requirement already satisfied: requests in ./.local/lib/python3.7/site-packages (2.31.0) Requirement already satisfied: charset-normalizer<4,>=2 in ./.local/lib/python3.7/site-packages (from requests) (3.2.0) Requirement already satisfied: idna<4,>=2.5 in ./.local/lib/python3.7/site-packages (from requests) (3.4) Requirement already satisfied: urllib3<3,>=1.21.1 in ./.local/lib/python3.7/site-packages (from requests) (2.0.4) Requirement already satisfied: certifi>=2017.4.17 in ./.local/lib/python3.7/site-packages (from requests) (2023.7.22)
# sudo gedit .bash_profile
# .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/.local/bin:$HOME/bin export PATH export PYTHONPATH='/home/apple2c/.local/lib/python3.7/site-packages'
補足情報(FW/ツールのバージョンなど)
macOS Big Sur 11.7.8
VirtualBox バージョン 7.0.10 r158379 (Qt5.15.2)
CentOS Linux release 7.9.2009 (Core)
Python 3.7.16
追伸
皆様、アドバイス有難う御座います!
今、皆様のアドバイスや指示を受けて、試行錯誤している最中です。
追って、中間報告と追記をいたします。
追記 (中間報告) 2023.07.25 18:18
色々弄り回してしまっていて、何をどうしたのか分からなくなっていたので、とりあえずスナップショットで遡ってみました。
/usr/local/bin/python3.7
がある状態で、
~/.local/
にディレクトリ「lib」も、その下の「python3.7」もない状態です。
CentOS7の端末
$ pip3 --version
pip 22.0.4 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
となりますが、
$ pyrhon3 --version
bash: pyrhon3: コマンドが見つかりませんでした...
となります。
しかし、
$ which python3
/usr/local/bin/python3
となります。これらは、あとになって分かったことです。
確か、この時に
$ pip3 install requests
を実行したと思います。すると、~/.local/lib/python3.7/site-packages/requests/
が作られました。そして、
(省略) WARNING: You are using pip version 22.0.4; however, version 23.2.1 is available. You should consider upgrading via the '/usr/local/bin/python3.7 -m pip install --upgrade pip' command.
と表示されたので、そのまま従って、
$ /usr/local/bin/python3.7 -m pip install --upgrade pip
を実行すると、/.local/bin/
の下に、pip、pip3、pip3.7、pip3.11、が作られました。そして、
(省略) WARNING: You are using pip version 22.0.4; however, version 23.2.1 is available. You should consider upgrading via the '/usr/local/bin/python3.7 -m pip install --upgrade pip' command.
と表示されたので、そのまま従って、
$ /usr/local/bin/python3.7 -m pip install --upgrade pip
を実行すると、
Defaulting to user installation because normal site-packages is not writeable Requirement already satisfied: pip in ./lib/python3.7/site-packages (23.2.1)
と表示されました。
あとになって、この英文をグーグル翻訳したところ、そのディレクトリに書き込めないので、~/
の下に作られたようです。
それで、パーミッションとオーナー、グループを確認したところ、当然なのですが、
drwxr-xr-x. 8 root root 179 7月 23 14:01 site-packages
となっています。なので、
$ sudo pip3 install requests
だと、
sudo: pip3: コマンドが見つかりません
となったので、
$ sudo '/usr/local/bin/pip3' install requests
で実行してみたところ、/usr/local/lib/python3.7/site-packages/requests/
が作られましたが、
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv WARNING: You are using pip version 22.0.4; however, version 23.2.1 is available. You should consider upgrading via the '/usr/local/bin/python3.7 -m pip install --upgrade pip' command.
と、表示されていました。
確か、こういう操作をしたと思います。
この時点で、先述のuser-new.py
をMacのwebブラウザで開いてみました。
Internal Server Error
が出ましたので、/var/log/httpd/error_log
を見ると
[Tue Jul 25 18:12:19.756785 2023] [cgi:error] [pid 3204] [client 192.168.1.97:53993] AH01215: Traceback (most recent call last): [Tue Jul 25 18:12:19.757036 2023] [cgi:error] [pid 3204] [client 192.168.1.97:53993] AH01215: File "/var/www/html/user-new.py", line 5, in <module> [Tue Jul 25 18:12:19.757075 2023] [cgi:error] [pid 3204] [client 192.168.1.97:53993] AH01215: import requests # conda install requests [Tue Jul 25 18:12:19.757128 2023] [cgi:error] [pid 3204] [client 192.168.1.97:53993] AH01215: File "/usr/local/lib/python3.7/site-packages/requests/__init__.py", line 43, in <module> [Tue Jul 25 18:12:19.757142 2023] [cgi:error] [pid 3204] [client 192.168.1.97:53993] AH01215: import urllib3 [Tue Jul 25 18:12:19.757195 2023] [cgi:error] [pid 3204] [client 192.168.1.97:53993] AH01215: File "/usr/local/lib/python3.7/site-packages/urllib3/__init__.py", line 42, in <module> [Tue Jul 25 18:12:19.757231 2023] [cgi:error] [pid 3204] [client 192.168.1.97:53993] AH01215: "urllib3 v2.0 only supports OpenSSL 1.1.1+, currently " [Tue Jul 25 18:12:19.757342 2023] [cgi:error] [pid 3204] [client 192.168.1.97:53993] AH01215: ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0.2k-fips 26 Jan 2017'. See: https://github.com/urllib3/urllib3/issues/2168 [Tue Jul 25 18:12:19.772410 2023] [cgi:error] [pid 3204] [client 192.168.1.97:53993] End of script output before headers: user-new.py
となっており、質問投稿した最初の時と少し違っています。
追記 (上記の中間報告のあとです) 2023.07.25 18:44
$ which pip3
/usr/local/bin/pip3
$ pip3 show requests
Location: /home/apple2c/.local/lib/python3.7/site-packages
test001.py
パーミッション755
Python3
1import sys 2import pprint 3pprint.pprint(sys.path)
の結果
Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator at root@localhost to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log.
/var/log/httpd/error_log
[Tue Jul 25 18:32:22.289263 2023] [cgi:error] [pid 1679] [client 192.168.1.97:54141] AH01215: (8)Exec format error: exec of '/var/www/html/test001.py' failed [Tue Jul 25 18:32:22.289733 2023] [cgi:error] [pid 1679] [client 192.168.1.97:54141] End of script output before headers: test001.py
なので、CentOS7の端末で実行してみました。
$ which python3 /usr/local/bin/python3 [apple2c@localhost root]$ /usr/local/bin/python3 Python 3.7.16 (default, Jul 23 2023, 13:59:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> import pprint >>> pprint.pprint(sys.path) ['', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/home/apple2c/.local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/site-packages']
となりましたので、先述の
$ pip3 show requests
で表示されているパスが含まれているようです。
追記 2023.07.25 18:48
失礼しました。タイプミスですね。
$ python3 --version
Python 3.7.16
となりました。
追記 2023.07.25 20:31
>CGIとして実行されるときのpythonインタープリタの設定
すみません。どうやるのか分かりません。
Pythonのインストールですが、スナップショットを遡ってみたところ、
https://www.python.jp/install/centos/index.html
を参考にして、
https://pythonlinks.python.jp/ja/index.html
からダウンロードしたものを使っているようです。
これ、非公式ですね。
色々と弄り回していて、たまたまうまく行ったように見えていたものをそのまま使っているようです。
Pythonは、最新ではなく3.7.xが必要です。
pip3はどうやってインストールしたか、思い出せません。
この時点で、~/.local/
があるので、もうひとつ遡ってみました。
すると、Python3が見当たらないのですが、~/.local/
があります。
$ which python3
/usr/bin/which: no python3 in (/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/apple2c/.local/bin:/home/apple2c/bin)
これで、本文の最大文字数の限界のようなので、続きは新規スレッドを立てました。
https://teratail.com/questions/irgq73vifgeyue

回答1件
あなたの回答
tips
プレビュー