PHP cannot connect to mysql through python
Asked Answered
E

2

2

Im writing a PHP to exec a python script which connects to mysql like this

<?php
    echo shell_exec('python doSmt.py 2>&1');
</body></html>")
?>

in Python script:

# -*- coding: utf-8 -*-
import mysql.connector

and get this error

Traceback (most recent call last): File "doSmt.py", line 2, in import mysql.connector ImportError: No module named mysql.connector

please help

UPDATE: I tried checking packages installed on php web server from netbeans

# -*- coding: utf-8 -*-
import pkg_resources
installed_packages = pkg_resources.working_set
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
     for i in installed_packages])
print(installed_packages_list)

and got this

'altgraph==0.10.2', 'backports-abc==0.5', 'bdist-mpkg==0.5.0', 'bonjour-py==0.3', 'certifi==2018.4.16', 'chardet==3.0.4', 'click==6.7', 'flask==1.0.2', 'futures==3.2.0', 'idna==2.7', 'itsdangerous==0.24', 'jinja2==2.10', 'macholib==1.5.1', 'markupsafe==1.0', 'matplotlib==1.3.1', 'modulegraph==0.10.4', 'nose==1.3.7', 'numpy==1.8.0rc1', 'pip==10.0.1', 'py2app==0.7.3', 'pyobjc-core==2.5.1', 'pyobjc-framework-accounts==2.5.1', 'pyobjc-framework-addressbook==2.5.1', 'pyobjc-framework-applescriptkit==2.5.1', 'pyobjc-framework-applescriptobjc==2.5.1', 'pyobjc-framework-automator==2.5.1', 'pyobjc-framework-cfnetwork==2.5.1', 'pyobjc-framework-cocoa==2.5.1', 'pyobjc-framework-collaboration==2.5.1', 'pyobjc-framework-coredata==2.5.1', 'pyobjc-framework-corelocation==2.5.1', 'pyobjc-framework-coretext==2.5.1', 'pyobjc-framework-dictionaryservices==2.5.1', 'pyobjc-framework-eventkit==2.5.1', 'pyobjc-framework-exceptionhandling==2.5.1', 'pyobjc-framework-fsevents==2.5.1', 'pyobjc-framework-inputmethodkit==2.5.1', 'pyobjc-framework-installerplugins==2.5.1', 'pyobjc-framework-instantmessage==2.5.1', 'pyobjc-framework-latentsemanticmapping==2.5.1', 'pyobjc-framework-launchservices==2.5.1', 'pyobjc-framework-message==2.5.1', 'pyobjc-framework-opendirectory==2.5.1', 'pyobjc-framework-preferencepanes==2.5.1', 'pyobjc-framework-pubsub==2.5.1', 'pyobjc-framework-qtkit==2.5.1', 'pyobjc-framework-quartz==2.5.1', 'pyobjc-framework-screensaver==2.5.1', 'pyobjc-framework-scriptingbridge==2.5.1', 'pyobjc-framework-searchkit==2.5.1', 'pyobjc-framework-servicemanagement==2.5.1', 'pyobjc-framework-social==2.5.1', 'pyobjc-framework-syncservices==2.5.1', 'pyobjc-framework-systemconfiguration==2.5.1', 'pyobjc-framework-webkit==2.5.1', 'pyopenssl==0.13.1', 'pyparsing==2.0.1', 'python-dateutil==1.5', 'python==2.7.10', 'pytz==2013.7', 'requests==2.19.1', 'scipy==0.13.0b1', 'setuptools==18.5', 'singledispatch==3.4.0.3', 'six==1.4.1', 'tornado==5.0.2', 'urllib3==1.23', 'werkzeug==0.14.1', 'wheel==0.31.1', 'wsgiref==0.1.2', 'xattr==0.6.4', 'zope.interface==4.1.1'

There's no package relating to mysql. I think this is the problem. But how to fix this?

Episode answered 23/7, 2018 at 4:1 Comment(4)
Why not use PHP directly to connect to mysql?Brandybrandyn
The traceback is clear - the module cannot be found. Are you certain it's installed?Tupelo
@qirel I'm writing and running these on php web build in server of netbeans. when I run python file alone from terminal, it works. I think that the prob is from servers of netbeans, but can't figure it outEpisode
@Brandybrandyn I need the python file to read from database and process things in computerEpisode
A
1

make sure you have the following text in the first line of doSmt.py file:

#!/usr/bin/env python
Anesthesiology answered 23/7, 2018 at 5:29 Comment(1)
This shebang is not going to do anything at all if the script is executed the way the OP does it, i.e. with the script name passed as a parameter to the Python executableFlyspeck
A
0

Seems like you haven't installed mysql.connector. Try installing it with pip.

Autocratic answered 23/7, 2018 at 4:46 Comment(1)
I'm so sure that it is installed [Requirement already satisfied: mysql-connector in /usr/local/lib/python2.7/site-packages (2.1.6)]Episode

© 2022 - 2024 — McMap. All rights reserved.