通过导入熊猫影响的Windows 10上的PHP到Python的变量



我试图将变量从php传递到窗口上的python,但是"导入pandas"行引起了问题。下面我的所有代码都是我试图创建的实际过程的裸露,以简单起见。代码的第一个块是我的索引,第二个是由index.php称为php代码,最后一个块是python。

index.php

<!DOCTYPE html>
<html>
<head>
<b>Enter a folder path </b> 
</head>
<body>
<form action="BlastParse.php" method="post">
    Path: <input type ="text" name="path"><br>
    <input type="submit">
</form>
</body>
</html>

BlastParse.php

<html>
<body>
<?php 
#getting path passed from index.php
$masterpath = $_POST["path"];
echo 'The path requested to be passed is: ' . $masterpath . '<br>';
#my directories 
$python = 'C:/Users/Garrett/Anaconda3/python.exe';
$pyscript = 'C:/Users/Garrett/Documents/Python/temp.py';
$pyscriptPrimed = $pyscript . ' ';
#creating the command
$command ="$python $pyscriptPrimed";
#executing the command to call temp.py; adding passed path to command
exec($command .$masterpath, $output, $return_var);
 ?>
</body>
</html>

temp.py

import os
import sys
#path passed into python from php
file_path = sys.argv[1]
#file_path = 'Write this string to file'
with open("C:/Users/Garrett/Documents/Python/copy.txt", 'w') as file:
        file.write(file_path)
#PROBLEM HERE
import pandas as pd
with open("C:/Users/Garrett/Documents/Python/copy2.txt", 'w') as file:
        file.write(file_path)

我正在使用写作来复制。当我评论导入pandas行时,Copy2.TXT文件将创建并写入正确。如果不是,则未创建copy2.txt文件,$ return_var变量将返回PHP中的1(我不确定错误代码尚未代表什么)。

我在Windows 10上使用Python 3.7运行,并通过Anaconda使用VS代码。

很可能这是因为未安装熊猫在您试图运行的地方。这可能是因为在调用Python脚本之前,您尚未激活Anaconda环境。

我尚未测试下面的代码,但它应该指向正确的方向:

$command ="source activate environment-name && $python $pyscriptPrimed && source deactivate";'

为了帮助调试,我尝试的第一件事就是将导入语句包裹在尝试捕获和要么打印中:

try:
    import pandas as pd
except Exception as e:
    print(str(e))

如果未打印到控制台,请尝试将其写入文件:

try:
    import pandas as pd
except Exception as e:
    with open("C:/Users/Garrett/Documents/Python/error.txt", 'w') as file:
        file.write(str(e))

就像您在问题错误代码1中的评论中的一面一样,是对一般错误的关注。0的退出状态是成功

最新更新