在受导入pandas影响的Windows 10上将变量从PHP传递到Python

I am trying to pass a variable from PHP to Python on Windows, but the line "import pandas" is causing an issue. All my code below is bare-bones of the actual process I am trying to create for simplicity. The first chunk of code is my Index, the second is the PHP code called by Index.php, and the last chunk is 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)

I am using the writing to copy.txt and copy2.txt for debugging purposes roughly since there isn't anything produced on the terminal. When I comment out the import pandas line, the copy2.txt file is created and written to properly. If not, the copy2.txt file is not created and the $return_var variable returns a 1 in PHP (which I'm not sure what the error code represents yet).

I am running on Windows 10 with Python 3.7, and using VS Code through Anaconda.

dongshan3759
dongshan3759 所以,看起来pandas无法导入。路径问题可能会发生。不是phpexec的专家,但如果我不得不猜测,那就是原因
一年多之前 回复

1个回答

In all likelihood this is because pandas is not installed where you are trying to run. This could be because you have not activated your anaconda environment before you are calling your python script.

I haven't tested the code below but it should point you in the right direction:

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

In order to help debug this the first thing I'd try is wrapping the import statement in a try catch and either printing:

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

If that fails to print to console try this to write to file:

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

Just as an aside on your comment in the question error code 1 from a script is a Catchall for general errors. An exit status of 0 is a success

duanlanzhi5509
duanlanzhi5509 非常感谢! 我添加了尝试除了它吐出我需要也导入numpy ....所以我做了。 然后它吐出“导入多阵列numpy扩展模块失败。很可能你正试图导入一个失败的numpy版本。” 然后我卸载numpy,重新安装,它工作了!
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问