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

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

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序