简体   繁体   English

未能为 python CGI 脚本(apache 网络服务器)导入 matplotlib

[英]Failure to import matplotlib for python CGI script (apache webserver)

Using: Apache Server 2.4 (localhost, Win10), Python 3.8.4 (32-bit)使用:Apache 服务器 2.4(本地主机,Win10),Python 3.8.4(32 位)

I am trying to import matplotlib in my python CGI script and I cannot seem to find a solution (I have read the similar posts and I cannot seem to find a workaround).我正在尝试在我的 python CGI 脚本中导入 matplotlib,但我似乎找不到解决方案(我已阅读类似的帖子,但似乎找不到解决方法)。

Code (pythoncode.py located in Apache24\cgi-bin):代码(pythoncode.py 位于 Apache24\cgi-bin 中):

#!C:\Python\python.exe


import os,sys
import cgi
import cgitb
import math
import numpy as np
cgitb.enable()

print("Content-type:text/html\r\n\r\n")
print('<html>')
print ('<head>')
print ('<title>Hello World/title>')
print ('</head>')
print ('<body>')
print ('<h2>Hello World!</h2>')
print ('</body>')
print ('</html>')

print(sys.executable)
print(sys.version)
print(sys.prefix)
print(sys.path)


#print(os.path.expanduser('~')) # C:\Users\Mike

os.environ[ 'HOME' ] = 'C:/Python'
print(os.environ['HOME'])

print(np.arange(3,5))

import matplotlib
##matplotlib.use('Agg')

I have pip installed numpy to test if packages can be read, matplotlib is not cooperating.我安装了 pip numpy 来测试是否可以读取包,matplotlib 不合作。 Numpy is for example.例如 Numpy。

I have done a complete uninstall and reinstall of python, removed all pip packages, and have only installed numpy and matplotlib.我已经完成了 python 的完全卸载和重新安装,删除了所有 pip 包,并且只安装了 numpy 和 ZF021ZE.3237A5EBA6FFFEE

Error (seen on index.html):错误(见 index.html):

Traceback (most recent call last):
  File "C:/Apache24/cgi-bin/collect_seats_by_click_server.py", line 34, in <module>
    import matplotlib
  File "C:\Python\lib\site-packages\matplotlib\__init__.py", line 921, in <module>
    dict.update(rcParams, _rc_params_in_file(matplotlib_fname()))
  File "C:\Python\lib\site-packages\matplotlib\__init__.py", line 602, in matplotlib_fname
    for fname in gen_candidates():
  File "C:\Python\lib\site-packages\matplotlib\__init__.py", line 599, in gen_candidates
    yield os.path.join(get_configdir(), 'matplotlibrc')
  File "C:\Python\lib\site-packages\matplotlib\__init__.py", line 239, in wrapper
    ret = func(**kwargs)
  File "C:\Python\lib\site-packages\matplotlib\__init__.py", line 502, in get_configdir
    return _get_config_or_cache_dir(_get_xdg_config_dir())
  File "C:\Python\lib\site-packages\matplotlib\__init__.py", line 444, in _get_xdg_config_dir
    return os.environ.get('XDG_CONFIG_HOME') or str(Path.home() / ".config")
  File "C:\Python\lib\pathlib.py", line 1099, in home
    return cls(cls()._flavour.gethomedir(None))
  File "C:\Python\lib\pathlib.py", line 265, in gethomedir
    raise RuntimeError("Can't determine home directory")
RuntimeError: Can't determine home directory

Adding:添加:

os.environ[ 'HOME' ] = 'C:/Python'

does not seem to change this error whatsoever.似乎并没有改变这个错误。

I am fairly certain this is a webserver error, possibly not being able to read matplotlib dependencies, however, testing numpy makes me think otherwise.我相当肯定这是一个网络服务器错误,可能无法读取 matplotlib 依赖项,但是,测试 numpy 让我不这么认为。 If you have any suggestions, or need any more information, please let me know.如果您有任何建议或需要更多信息,请告诉我。 I am pretty lost at this point.在这一点上,我很迷茫。

Other Information:其他信息:
Python is installed at \C\Python: Python 安装在 \C\Python:
Laravel to Python RuntimeError: Can't determine home directory Laravel 到 Python 运行时错误:无法确定主目录
Importing & running matplotlib via CGI 通过 CGI 导入并运行 matplotlib
ImportError: No module named 'matplotlib._path' when running a python script in apache2 ImportError:在 apache2 中运行 python 脚本时没有名为“matplotlib._path”的模块

Scrapped apache.报废 apache。 Used flask and python webserver, working properly now.使用 flask 和 python 网络服务器,现在可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM