我有一個令人抓狂的錯誤,我的 python 腳本在獨立運行時成功地做某事,但作為由 jquery 呼叫的 cgi 腳本運行時失敗$.ajax()。歡迎任何見解。
我正在使用本地 apache2 服務器在我的新 MacbookPro (macOS 11.6) 上本地開發此 Web 應用程式,我已將其配置為將相關目錄中的 .py 檔案作為 cgi 程式運行。
相關的作業部分是這些:
- 本地在我的新 MacbookPro (macOS 11.6) 上
- 安裝了 graphviz 二進制檔案:/opt/homebrew/bin/twopi:
twopi - graphviz 版本 2.49.2 (20211016.1639) - pygraphviz 1.7
- Python 3.9.7:/opt/homebrew/opt/[email protected]/bin/python3.9
- rdflib 版本:'6.03a'
這個應用程式的目的是由一個python cgi腳本驅動,使用python的requests模塊從本地檔案系統中檢索一些資料,從web上的AllegroGraph實體中檢索一些RDF資料,然后在web中布局和顯示圖形可視化使用 graphviz 和 python 的 pygraphviz 模塊的頁面。
javascript 發出這樣的 GET 請求:
function graphMe(charter){
$.ajax({
type: "get",
url: "cartametallon.py",
data: {"graphMe": charter},
dataType: 'json',
success: deploySVG,
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR.response, textStatus, errorThrown);
}
});
}
python cgi 腳本使用 cgi 模塊來處理這個請求,如下所示:
import cgi, cgitb
cgitb.enable(format="text")
form = cgi.FieldStorage()
try:
if 'graphMe' in form:
charter = form.getvalue('graphMe')
uri = "<http://chartex.org/graphid/" charter ">"
print ("Content-Type: application/json\r\n\r\n")
print (json.dumps(visualizeDocumentGraph(uri)))
except Exception:
print ("Content-Type: text/plain\n")
print("Exception in user code:")
print("~"*20, __file__.split('/')[-1], "~"*20)
traceback.print_exc(file=sys.stdout)
print("~"*60)
The visualizeDocumentGraph() function assembles graph data and metadata from several sources and stores it in a dict which should then be returned to the referring page as a json object. One of the things stored in this object is an SVG string of the graph as laid out by the graphviz's twopi algorithm. I've verified that each element of this python function works as expected, and when run at the command line, it returns the expected object; however, the response to the jQuery.ajax() request looks like this:
Content-Type: text/plain
Exception in user code:
~~~~~~~~~~~~~~~~~~~~ cartametallon.py ~~~~~~~~~~~~~~~~~~~~
Traceback (most recent call last):
File "/opt/homebrew/lib/python3.9/site-packages/pygraphviz/agraph.py", line 1344, in _get_prog
runprog = self._which(prog)
File "/opt/homebrew/lib/python3.9/site-packages/pygraphviz/agraph.py", line 1800, in _which
raise ValueError(f"No prog {name} in path.")
ValueError: No prog twopi in path.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "~/Sites/cartametallon/cartametallon.py", line 661, in <module>
print (json.dumps(visualizeDocumentGraph(uri)))
File "~/Sites/cartametallon/cartametallon.py", line 292, in visualizeDocumentGraph
dgsvg = makedot(g).draw(format='svg', prog='twopi')
File "/opt/homebrew/lib/python3.9/site-packages/pygraphviz/agraph.py", line 1596, in draw
data = self._run_prog(prog, args)
File "/opt/homebrew/lib/python3.9/site-packages/pygraphviz/agraph.py", line 1360, in _run_prog
runprog = r'"%s"' % self._get_prog(prog)
File "/opt/homebrew/lib/python3.9/site-packages/pygraphviz/agraph.py", line 1346, in _get_prog
raise ValueError(f"Program {prog} not found in path.")
ValueError: Program twopi not found in path.
This is the puzzle then: in the cgi interaction it "appears" that the twopi program can't be found, but run on its own, my python script has no trouble. The twopi binary is installed at /opt/homebrew/bin/twopi, is readily accessible via the env variable PATH:
% echo $PATH
~/opt/anaconda3/condabin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
it's clear that my python script knows about it too. Not only does it execute it successfully when the script runs on its own, it knows explicitly where to find it:
>>> os.get_exec_path()
['~/opt/anaconda3/condabin', '/opt/homebrew/bin', '/opt/homebrew/sbin', '/usr/local/bin', '/usr/bin', '/bin', '/usr/sbin', '/sbin']
I can't get my python script to return the json I need for the web page, ARGH! This is made all the more maddening by the fact that what I'm trying to do is refactor and update an existing application that runs just fine at https://neolography.com/chartex/. This working program was recently transfered to a new web host and that required a little tinkering to get it working again, and the graph output is not as good as on my old host because A2 hosting insisted on installing an ancient version of graphviz (don't ask). So, the relevant working parts of this working program are these:
- twopi - graphviz version 2.30.1 (20201013.1554)
- pygraphviz 1.5
- Python 2.7.18(默認,2021 年 7 月 8 日,01:00:23)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
(這個 python 必須在虛擬環境中運行,這樣我才能安裝這個:) - RDFLib 版本:5.0.0
uj5u.com熱心網友回復:
我花了幾天時間,但多虧了 Thomas 的評論、這里的一些觀察以及python os 模塊本身的檔案,我終于明白了“PATH”環境變數,當我在命令列運行腳本時,是與執行 cgi 程式背景關系中的相同變數完全不同。
% echo $PATH
~/opt/anaconda3/condabin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
和
>>> os.environ["PATH"]
'~/opt/anaconda3/condabin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'
與os.environ["PATH"]運行 cgi 腳本時被 apache 參考的時間不同。
更糟糕的是,我將 $PATH 變數與sys.path具有完全不同目的的python 變數混為一談。我的 cgi 程式,使用 pygraphviz,試圖執行twopi一個二進制檔案/opt/homebrew/bin,在執行 cgi 程式的背景關系中,它可用的 PATH 變數如下所示:
"/usr/bin:/bin:/usr/sbin:/sbin:"
在 cgi 程式中為該變數添加必要的路徑很容易,如下所示:
os.environ["PATH"] = f"{os.environ['PATH']}:/opt/homebrew/bin"
這解決了我的問題。但是,我還是不放心。這似乎是一種駭人聽聞的方法。我仍然不完全理解為什么 apache與 python 解釋器或在命令列運行的腳本中可用的PATH不同$PATH。我認為PATHcgi 程式的可用權限是不同的,因為 apache 以不同的用戶 ( _www)執行它。
最好知道是否有更規范的方法來解決這個問題。將不勝感激地收到有關檔案的任何建議,以澄清我對這個問題的理解。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/359832.html
標籤:阿贾克斯 计算机图形学 图形可视化 文件库 pygraphviz
下一篇:RestTemplate.postForEntity的SpringMockito測驗拋出IllegalArgumentException:URI不是絕對的
