Laravel - Python script execution - python

I have to execute a Python script which pulls a large amount of data to the database. It is working fine while I am running a project using the command php artisan serve, but it is throwing an error after calling the public folder URL given below
localhost/project/public
Error:
The command "python /opt/lampp/htdocs/laravel/projectname/beta/projectname/public/python/pubmed_abstract/pubmed_engine.py '{"term":"cancer"}'" failed.
Exit Code: 1(General error) Working directory: /opt/lampp/htdocs/laravel/infocytosis/beta/infocytosis/public Output:
================ Error Output: ================
Traceback (most recent call last): File "/opt/lampp/htdocs/laravel/projectname/beta/projectname/public/python/pubmed_abstract/pubmed_engine.py", line 5, in from Bio import Entrez ImportError: No module named Bio
Code I used:
$python_path=public_path().'/python/doom_abstract/doom_engine.py';
$variables='{"term":"'.addslashes($request->term).'"}';
$process = new Process("python $python_path '$variables'");
$process->run();
return redirect()->back()->withMessage('Filter saved successfully');

You can try following one
$python_path=public_path('python/doom_abstract/doom_engine.py');

Instead of:
$process = new Process("python $python_path '$variables'");
$process->run();
how about
shell_exec("python $python_path '$variables'")

Related

PHP: calling a python script throws ModuleNotFoundError, calling from terminal works just fine?

I have a script, let's call it /home/pi/somescript.py and when I call it with /bin/python3.7 /home/pi/somescript.py everything works as it should.
Now I'm trying to call from a php script with exec('/bin/python3.7 /home/pi/somescript.py', $output, $ret_code);. This however throws a ModuleNotFoundError.
But when I run the interactive PHP shell and paste the above exec in there, it also works. Am I missing something? What could be the cause of this? How do I fix this?
The relevant part of the python script:
import sys
sys.stderr = sys.stdout # so I get the stacktrace in the $output of the php file
from iso3166 import countries # <- error here
# ...
I'm running a lighttpd server. The PHP file lies in /var/www/html/info.php and I access it via a browser with http://raspberrypi:8080/info.php
The relevant part of the PHP file:
exec('/bin/python3.7 /home/pi/somescript.py', $output, $ret_code);
$data = ["out" => $output];
header('Content-Type: application/json');
echo json_encode($data);
// this way I get the output when in the browser
The stacktrace I get:
Traceback (most recent call last):
File "/home/pi/somescript.py", line 6, in <module>
from iso3166 import countries
ModuleNotFoundError: No module named 'iso3166'

PythonKit for OSX Swift project suddenly not finding modules

I built a MacOS app using PythonKit to run some scripts and return the data. I had it running with no issues the other day but now it crashes when trying to load the script file. I've verfied the path to the file is correct and have run the Python script in Terminal with no issues.
Here's the error message:
Fatal error: 'try!' expression unexpectedly raised an error: Python exception: No module named 'helloswift': file PythonKit/Python.swift, line 674
2021-04-09 10:01:55.441796-0400 TechCommToolKit[10754:2952424] Fatal error: 'try!' expression unexpectedly raised an error: Python exception: No module named 'helloswift': file PythonKit/Python.swift, line 674
The line of code in PythonKit throwing it:
public func `import`(_ name: String) -> PythonObject { return try! attemptImport(name) }
My Python code in the file:
import requests
import json
import sys
def getConfluenceData(strEndPoint):
s = requests.Session()
response = s.post('https://wiki.xxxx.com/dologin.action', data={'os_username': 'app_wiki_api2', 'os_password': 'xxxxxxxxxxxxxxxx'})
response = s.get(strEndPoint)
json_formatted_str = json.dumps(response.json(), indent=4)
return(json_formatted_str)
Ug, this was operator error, I was pointing to the file, not the directory containing the file.

Python inputs from another python file

I have a python file(file1.py) where i have written a script. Now, my friend asked me to keep the inputs in another python file(file2.py) and import the file in file1.py so that its becomes easy to modify the inputs if we keep it seperate file. So i created file2.py, copied the inputs there and saved it. Then i wrote the following line in file1.py: from file2 import * but its throwing error:
TASK [Run the python script]
********************************************************************************************************************************************************************************************************************************* fatal: [automation_server]: FAILED! => {"changed": true, "msg":
"non-zero return code", "rc": 1, "stderr": "Shared connection to
10.242.174.192 closed.\r\n", "stderr_lines": ["Shared connection to 10.242.174.192 closed."], "stdout": "/etc/profile.d/lang.sh: line 19: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such
file or directory\r\nTraceback (most recent call last):\r\n File
\"/home/bgnanasekaran/.ansible/tmp/ansible-tmp-1591957555.760499-407
182191177023663/download_RI_build.py\", line 8, in \r\n
from file2 import
*\r\nModuleNotFoundError: No module named 'file2'\r\n", "stdout_lines": ["/etc/profile.d/lang.sh: line 19: warning: setlocale:
LC_CTYPE: cannot change locale (UTF-8): No such file or directory",
"Traceback (most recent call last):", " File
\"/home/bgnanasekaran/.ansible/tmp/ansible-tmp-1591957555.760499-407
182191177023663/download_RI_build.py\", line 8, in ", "
from file2 import *", "ModuleNotFoundError: No module named 'file2'"]}
Note: I'll give some example of inputs in file2.py -
name="Suresh"
college="VIT"
Also note: I wrote all these python scripts to make it run with ansible playbook. This script is a part of play in my ansible playbook.
Please help me do this. I want a python file where i can store my python script inputs and use the same file in python script. This is not so related to ansible but more related to python.
you need a configuration file this post might help you or you can create a class and all required variable as a class variable and import the class into your script.
import is for python files only.
If you want to read content of a file you need to open it.
with open('filename', 'r') as handler:
print(handler.readlines())
Is a minimal example
You could also try to use environment varibales, which is a much more common practice when dealing with deployment strategies
import os
VARIABLE = os.getenv('VARIBALE', 'default_value')
Then you run the script with:
VARIABLE=this ./pythonscript.py
Or export the variable into the environment in another way

Getting "global name 'FbxManager' is not defined"

I am working on creating a WebGL interface for which I am trying to convert FBX models to JSON file format in an automated process using python file, convert_fbx_three.py (from Mr. Doob's GitHub project) from command line.
When I try the following command to convert the FBX:
python convert_fbx_three.py Dolpine.fbx Dolpine
I get following errors:
Error in cmd:
Traceback (most recent call last):
File "convert_fbx_three.py", line 1625, in <module>
sdkManager, scene = InitializeSdkObjects()
File "D:\xampp\htdocs\upload\user\fbx\FbxCommon.py", line 7, in InitializeSdkObjects
lSdkManager = KFbxSdkManager.Create()
NameError: global name 'FbxManager' is not defined
I am using Autodesk FBX SDK 2012.2 available here on Windows 7.
Can you please try the following:
import FbxCommon
.
.
.
lSdkManager, lScene = FbxCommon.InitializeSdkObjects()
You probably need to add environment variables pointing to the folder that contains fbx.pyd, FbxCommon.py, and fbxsip.pyd prior to calling anything in those modules.

name 'OSPF_Link' is not defined

I have a python script like this:
#!/usr/bin/env python
from scapy.all import *
from ospf import *
def ourSend(packet):
sendp(packet,iface='eth1')
host1='10.0.3.2'
advr_routers='10.0.8.7'
host2='10.0.2.2'
sequence=0x80000918
link2host1 = OSPF_Link(id=host1,data='10.0.3.1',type=2,metric=1)
link2host2 = OSPF_Link(id=host2,data='10.0.2.2',type=2,metric=1)
link2victim = OSPF_Link(id="192.168.200.20",data="255.255.255.255",type=3,metric=1)
IPlayer=IP(src='10.0.1.2',dst='224.0.0.5')
OSPFHdr=OSPF_Hdr(src='10.0.6.1')
rogueLsa=Ether()/IPlayer/OSPFHdr/OSPF_LSUpd(lsacount=1,lsalist=[OSPF_Router_LSA(options=0x22,id='10.0.3.1',adrouter=advr_routers,seq=sequence,\
linkcount=3,linklist=[link2victim,link2host1,link2host2])])
ourSend(rogueLsa)
When I run it it has an scapy error.. So I resolved it with git pyrt...
now when I want to run the python script I have other error:
$ python scipt.py
WARNING: No route found for IPv6 destination :: (no default route?)
Traceback (most recent call last):
File "s.py", line 19, in <module>
link2host1 = OSPF_Link(id=host1,data='10.0.3.1',type=2,metric=1)
NameError: name 'OSPF_Link' is not defined
Thank you
I Should separately get ospf.py again and run my script.
This is what I tried and worked...
When you git pyrt The module OSPF_Link can't be added. SO I used the ospf.py again and problem is solved now....

Categories