【问题标题】:Failure to import matplotlib for python CGI script (apache webserver)无法为 python CGI 脚本(apache 网络服务器)导入 matplotlib
【发布时间】:2020-12-16 01:01:53
【问题描述】:

使用:Apache Server 2.4 (localhost, Win10), Python 3.8.4 (32-bit)

我正在尝试在我的 python CGI 脚本中导入 matplotlib,但似乎找不到解决方案(我已阅读类似的帖子,但似乎找不到解决方法)。

代码(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')

我已经 pip 安装了 numpy 来测试是否可以读取包,matplotlib 不合作。以 Numpy 为例。

我已经完全卸载并重新安装了python,删除了所有pip包,并且只安装了numpy和matplotlib。

错误(见 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

添加:

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

似乎并没有改变这个错误。

我相当肯定这是一个网络服务器错误,可能无法读取 matplotlib 依赖项,但是,测试 numpy 让我不这么认为。如果您有任何建议或需要更多信息,请告诉我。在这一点上我很迷茫。

其他信息:
Python 安装在 \C\Python:
Laravel to Python RuntimeError: Can't determine home directory
Importing & running matplotlib via CGI
ImportError: No module named 'matplotlib._path' when running a python script in apache2

【问题讨论】:

    标签: python apache matplotlib cgi


    【解决方案1】:

    废弃的阿帕奇。使用flask和python webserver,现在可以正常工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-11
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      • 2012-12-10
      相关资源
      最近更新 更多