简体   繁体   中英

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

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

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).

Code (pythoncode.py located in 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. Numpy is for example.

I have done a complete uninstall and reinstall of python, removed all pip packages, and have only installed numpy and matplotlib.

Error (seen on 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. 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:
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

Scrapped apache. Used flask and python webserver, working properly now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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