Python3でパッケージをインストールしてもimportできないでエラーになる

実現したいこと

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

コメントを投稿

0 コメント