简体   繁体   English

当使用 eval() 从 php 脚本调用脚本以及从 bash 脚本调用相同的函数时,python 如何使用路径?

[英]How does python use paths when a script is called using eval() from a php script and when the same function is called from a bash script?

I have an executable python script which archives data from mysql server using the pymysql library.我有一个可执行的 python 脚本,它使用 pymysql 库从 mysql 服务器存档数据。 The script works well from the command line.该脚本从命令行运行良好。

I call this script from a php script using escapeshellcmd function and I've gotten it to work.我使用escapeshellcmd 函数从一个php 脚本调用这个脚本,我已经让它工作了。

I also have created a bash script that I intend to use from crontab to archive the information as well.我还创建了一个 bash 脚本,我打算从 crontab 使用它来存档信息。 I can make this script work as well, by making changes outlined below.通过进行下面概述的更改,我也可以使该脚本正常工作。

Somehow I have gotten into python versions and path problems.不知何故,我遇到了 python 版本和路径问题。

if I include如果我包括

#!/home/tim/anaconda3/bin/python

as the first line of the python script it works when called by the php script (using www-data as the user, I believe).作为 python 脚本的第一行,它在被 php 脚本调用时起作用(我相信使用 www-data 作为用户)。 It doesn't work from the bash script or the command line, giving the following error:它在 bash 脚本或命令行中不起作用,出现以下错误:

  File "./signal_archive.py", line 22, in <module>
    import pymysql
ModuleNotFoundError: No module named 'pymysql'

However, if the first line of the python script is as follows:但是,如果python脚本的第一行如下:

#!/usr/bin/python3

the script works from the bash script and the command line but not from the php script.该脚本可以从 bash 脚本和命令行运行,但不能从 php 脚本运行。 It gives the following error:它给出了以下错误:

  File "/home/tim/python/commodities_related/signal_archive.py", line 23, in <module>
    import pandas as pd
ModuleNotFoundError: No module named 'pandas'

Both packages are installed on my system.这两个软件包都安装在我的系统上。 Thinking pointing the script to the path would help, I added the following to the python script but no luck so far.认为将脚本指向路径会有所帮助,我将以下内容添加到 python 脚本中,但到目前为止还没有运气。

sys.path.append('/usr/lib/python3/dist-packages:')
sys.path.append('/usr/local/lib/python3.5/dist-packages:')

There is obviously something I'm missing;显然我遗漏了一些东西; I think it is that php script is called by www-user and I don't know the default path.我认为是www-user调用了php脚本,我不知道默认路径。 The bash file is called by my user with the path specified in the .bashrc file.我的用户使用 .bashrc 文件中指定的路径调用 bash 文件。 However, I may need to point the apache or php (www-user) to use a specific installation of python.但是,我可能需要指向 apache 或 php (www-user) 才能使用特定的 python 安装。

EDIT- To be more clear, a php script (phpfile1.php) calls the python script.编辑 - 更清楚的是,一个 php 脚本(phpfile1.php)调用了 python 脚本。 When I call phpfile1.php from another php script (phpfile2.php) running on apache2 I everything works using the当我从在 apache2 上运行的另一个 php 脚本(phpfile2.php)调用 phpfile1.php 时,我一切都使用

#!/home/tim/anaconda3/bin/python

When I call the same file (phpfile1.php) from a different php script (phpfile3.php) from a bash script it fails.当我从 bash 脚本的不同 php 脚本 (phpfile3.php) 调用同一个文件 (phpfile1.php) 时,它会失败。 Additionally, if I run the file in place using the following此外,如果我使用以下命令就地运行文件

./signal_archive.py

I get the error but if I run it using the following command it works:我收到错误,但如果我使用以下命令运行它,它会起作用:

python signal_archive.py

Any ideas if this is right or how to do it?任何想法,如果这是正确的或如何做到这一点? Thanks.谢谢。

I fixed this in 2 steps:我分两步解决了这个问题:

It turns out that I needed to add the path to anaconda3 to my .bash_profile file.事实证明,我需要将 anaconda3 的路径添加到我的 .bash_profile 文件中。

export PATH="/home/tim/anaconda3/bin:$PATH"

When anaconda3 is installed it modifies the .bashrc file with the previous code snippet.安装 anaconda3 后,它会使用前面的代码片段修改 .bashrc 文件。 However .bash_profile made the difference in this case.然而 .bash_profile 在这种情况下有所不同。

I also modified the top of the python file to use the anaconda path for execution, as well as add the path for the specific python packages.我还修改了python文件的顶部以使用anaconda路径执行,以及添加特定python包的路径。

#!/home/tim/anaconda3/bin/python
import sys
sys.path.insert(1, '/usr/local/lib/python3.5/dist-packages')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM