douluohan3403 2019-02-22 00:04
浏览 145
已采纳

在受导入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.

  • 写回答

1条回答 默认 最新

  • dongping6974 2019-02-22 00:35
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?