PHP shell_exec() of Python script causes ModuleNotFoundError - python

I am running a python file from php using shell_exec function.
The python file has the following lines
import cv2
print('Hello')
and in php
<?php
$command = "python test.py ";
$command = escapeshellcmd($command);
$output = shell_exec("$command 2>&1");
echo $output;
?>
but it is not working
it shows error
Traceback (most recent call last): File "/var/www/html/test/test.py", line 1, in import cv2 ModuleNotFoundError: No module named 'cv2'
i have installed opencv, and it runs properly while running from terminal
i also tried using other php functions like exec but nothing works.
when i remove import cv2 it shows the python output

Try and set the enviroment variable to Python with it?
$env = "PYTHONPATH=/path/to/opencv/lib/python3.7/site-packages";
$command = "($env && $command) 2>&1";
$output = shell_exec($command);

Related

Raspberry pi call workon cv in bash or python script

I try to run a python script at start of raspberry pi. In the python script I use open cv so I need to call before:
source ~/.profile
workon cv
I try to write a bash file:
#!/bin/bash
echo "Bash script"
source ~/.profile
workon cv
#/usr/bin/python3 reco_eyes_from_video.py
python reco_eyes_from_video.py
But I get this error:
Traceback (most recent call last):
File "reco_eyes_from_video.py", line 1, in <module>
import cv2
ImportError: No module named 'cv2'
If I call myself:
source ~/.profile
workon cv
and then:
python reco_eyes_from_video.py
it is working fine
There is any way to run workon at the start?
It seems that you forget to write "python" before "reco_eyes_from_video.py" in your bash file.
To run this file at start, have a look at this answer

Executing a python script from scala giving error while running the same script from the command line does NOT

This is my python script:
!/usr/bin/python
import sys
import magic
m=magic.from_file('<file with absolute path goes here>')
print(m)
On running this from the command line:
$ python script.py
Microsoft Word 2007+
results in the output of the TYPE of the document that file is. Using python-magic.
Now running the same script from scala using below code:
import sys.process._
def compWithLibmagic(){
val result = "python /script.py" !
}
throws the below error:
Traceback (most recent call last):
File "script.py", line 3, in <module>
import magic
ImportError: No module named magic
PS: I have both python 2.7 and python 3.6 installed on my machine and running the script using any of them from the command line runs just fine so I guess both of them are bundled with the MAGIC packages correctly.
Would highly appreciate any kind of help.
I will try to answer my own question in the best possible way.
While trying to execute the script.py from the command line using python compiler python2.7 was being used and when I tried to invoke the same script.py from the scala compiler using
import sys.process._
def compWithLibmagic(){
val result = "python /script.py" !
}
it was using python3 which was getting unnoticed. It took me a couple of days to actually figure this thing out and finally I removed all the python2.7 and python3 libraries and installed python 2.7 again and it worked like a charm.
It works from both python3 command and Scala code for me.
This should work.
import sys.process._
def compWithLibmagic(){
val result = "python3 script.py".!!
}

Invoke bamboo plan variables in a python script under script task

I have a simple script task added to my Bamboo plan which is inline Python script.
/usr/bin/python <<EOF
print "Hello"
EOF
It works just fine.
Now I want to just pass Hello as a plan specific variable so I configured a variable called pythontest within bamboo plan variables and provided it the value Hello.
When I switch the script to:-
/usr/bin/python <<EOF
print "$bamboo_pythontest"
EOF
It does nothing.
After going through below post:-
https://community.atlassian.com/t5/Bamboo-questions/How-I-can-access-global-and-or-plan-specific-variables-from/qaq-p/162809
/usr/bin/python <<EOF
import os
print os.environ['bamboo_pythontest]
EOF
I get error
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "/usr/lib64/python2.6/UserDict.py", line 22, in __getitem__
raise KeyError(key)
KeyError: ''
Is there a way to successfully invoke Bamboo plan variables within a Python inline script just like it works for a shell or batch script.
Any help here would be greatly appreciated.
Cheers,
Ashley
I am looking into a solution where we can run the python code from inside bamboo. The error that you are pointing, looks to be syntax error. Try this.
/usr/bin/python <<EOF
print("Hello World")
EOF

Calling python script from a Bash script

I'm trying to call a python script from a bash script. I get import errors only if I try to run the .py from the bash script. If I run with python myscript.py everything is fine. This is my bash script:
while true; do
python script.py
echo "Restarting...";
sleep 3;
done
The error I get:
Traceback (most recent call last):
File "script.py", line 39, in <module>
from pokemongo_bot import logger
File "/Users/Paolo/Downloads/folder/t/__init__.py", line 4, in <module>
import googlemaps
ImportError: No module named googlemaps
There is more to this story that isn't in your question.
Your PYTHONPATH variable is getting confused somewhere along the way.
Insert a couple quick test lines:
in bash:
echo $PYTHONPATH
in your python:
import os
print os.environ["PYTHONPATH"]
At some point, the path to googlemaps got lost.
Your problem is in the script itself, your bash code is OK!. If you don't have problem running python scrip.py from bash directly, you should test if you use the same interpreter for both calls. You can check the shebang line in the python script, it is the first line in the file for example #!/usr/bin/env python or #!/usr/bin/python and compare it to the output of which python command if the output is different try to change or add the shebang line in to the file. If you call directly file in bash ./some_script.py bash reads the first line and if it is shebang line he wil execute the specific command for the file. My point is that if you use two diferent interpreters for calling file directly with python script.py and indirectly ./script.py one of them may not have the proper python modules.
Howto code:
$ which python
/usr/local/bin/python
So the second line is the path for your interpreter to build a shebang from it write in the first line of your script file this.
#!/usr/local/bin/python

How to set a python path when using runit

I am am using runit to manage a process on Ubuntu 12.04. I get the below error in the logs when I run:
sv up test/
I assume it is a python path issue.
ImportError: No module named htcommon.ht_redis
Traceback (most recent call last):
File "/home/ubuntu/workspace/htFrontEnd/htanalytics/ht_rpc_server.py", line 17, in <module>
from htpData import HTPItemBase, HTPUserBase
File "/home/ubuntu/workspace/htFrontEnd/htanalytics/htpData.py", line 9, in <module>
from htcommon.ht_redis import HTRedisConnection
ImportError: No module named htcommon.ht_redis]
I have also set the path in /etc/environment and also set in .bashrc.
Below is my runit script.
#!/bin/sh
exec 2>&1
exec export PYTHONPATH=$PYTHONPATH:/home/ubuntu/workspace/htFrontEnd/heythat
exec export PYTHONPATH=$PYTHONPATH:/home/ubuntu/workspace/htFrontEnd/heythat/htanalytics
exec /usr/bin/python /home/ubuntu/workspace/htFrontEnd/htanalytics/ht_rpc_server.py >> /tmp/ht_rpc_server.log 2>&1
root#aws-rpc-server-east-staging-20130203070552:/etc/sv#
When I run the process from the command line I get no issues and it works.
/usr/bin/python /home/ubuntu/workspace/htFrontEnd/heythat/htanalytics/ht_rpc_server.py
Why will runit not work? Why can it not find the path?
Look like a few potential problems with this script. I don't think an 'export' can be 'exec'd. At least, it fails in my version of bourne. The exec command replaces the current process (your script) with the called command and doesn't return unless the called command fails. So, it doesn't make sense to 'exec' the exports anyway. Also they can be collapsed into one line. So, you're script should look more like this:
#!/bin/sh
export PYTHONPATH=$PYTHONPATH:/home/ubuntu/workspace/htFrontEnd/heythat:/home/ubuntu/workspace/htFrontEnd/heythat/htanalytics
exec /usr/bin/python /home/ubuntu/workspace/htFrontEnd/htanalytics/ht_rpc_server.py >> /tmp/ht_rpc_server.log 2>&1

Categories

Resources