How to fix error : no module named sendgrid when try to use sendgrid python lib in PHP.


"no module named sendgrid" error reported with below codes:

<?php
system("/usr/bin/python sendmail.py 2>&1");
system("/usr/bin/python --version 2>&1", $ret);
?>

sendmail.py

import sendgrid
import os
from sendgrid.helpers.mail import *

sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("test@example.com")
to_email = Email("test@example.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)
....






It is very strange, since I have installed sendgrid.
pip list | grep sendgrid
sendgrid (4.1.0)
$pip list | grep sendgrid
sendgrid (4.1.0)

but when I use check the pip list in php runtime,
<?php
system("pip list | grep sendgrid 2>&1", $ret);
system("/usr/bin/python --version 2>&1", $ret);
?>
sendgrid not found indeed.

It seems like sendgrid was just installed to my env.
in the php runtime env, it was not installed.

Fix solution:
Install sendgrid from source code.

git clone https://github.com/sendgrid/sendgrid-python.git
cd sendgrid-python
python setup.py build
sudo python setup.py install

pip install python_http_client
git clone https://github.com/sendgrid/python-http-client.git
cd python-http-client/
python setup.py build 
sudo python setup.py install

OK. I got 202 status finally, which means email was sent successfully.

Comments

  1. Amazing post.
    Thank you for being a thought leader in the tech industry.
    Seeking the top mobile app development company in India, contact us - renowned for our expertise, we excel in crafting innovative and high-quality mobile applications. Our team of skilled professionals is dedicated to delivering cutting-edge solutions tailored to your needs. Get started today!

    ReplyDelete

Post a Comment

Popular posts from this blog

react-native run-android : sun.security.provider.cert path.SunCertPathBuilderException : unable to find valid certification path to req uested target

react-native run-android : do not build/update modified code(App.js)