Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 import module error
#1
I want to execute a python script from a bash script. But I cant import the mysql.connector module when running the script from snmpd. If I run the python or the bash script manually it works fine. But when I run it through snmpwalk I got the following error.

Error:
Traceback (most recent call last): File "parser.py", line 3, in <module> import mysql.connector ImportError: No module named 'mysql'
my phyton code "parser.py"
#!/usr/bin/python 
import sys  
import mysql.connector
try:
	cnx = mysql.connector.connect(user='******', password='*****',
								  host='127.0.0.1',
								  database='******')
	cursor = cnx.cursor()
	query = "SELECT `Valor`,`Tipo` FROM `snmp` WHERE `OID`='"+sys.argv[1]+"'"
	imp="0"
	tipo="INTEGER"
	cursor.execute(query)
	for (Valor) in cursor:
		imp=Valor[0]
		tipo=Valor[1]
	print(tipo)
	print(imp)
	cursor.close()
except mysql.connector.Error as err:
		print(-1)
else:
	cnx.close();
my bash script "panel-current"
#!/bin/bash
if [ "$1" = "-g" ]
then
echo $2
echo "STRING"
python /usr/local/bin/parser.py $2
fi
exit 0
snmpd.conf
pass .1.3.6.1.4.1.2020.1 /bin/bash /usr/local/bin/panel-current
I already checked that it runs on any python version 2.7 and 3.5. I think there is a problem with user permissions and the service is not able to load the mysql.connector module. Can anyone tell me how to fix it?
Quote
#2
The error is telling you that mysql is not installed, or at least not foe the version of python that you are running.
You need to install mysql
first make sure that pip is linked with the proper python:
pip -V
if the version of python matches what you are running, install mysql:
pip install mysql
Quote
#3
mysql is already installed. And pip is linked to the correct python version. my code does run if I do it manually on every python version my system has. checked with sys.version to make sure the selected one is correct.

But when is used by snmpd the mysql module is always missing. I even tell it to run a specific version of python that I know for sure it has the mysql module installed. but it still missing.
Quote
#4
Install for connector is:
pip install mysql-connector-python
(del_env) E:\div_code\del_env
λ pip install mysql-connector-python
Collecting mysql-connector-python
  Downloading .....
Installing collected packages: six, protobuf, mysql-connector-python
Successfully installed mysql-connector-python-8.0.15 protobuf-3.6.1 six-1.12.0

(del_env) E:\div_code\del_env
λ python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>> mysql.connector.__version__
'8.0.15'
Alternative Pure Python MySQL Client PyMySQL.
Quote
#5
I ended up using sockets instead. Mysql queries had a memory leak on my nodejs server. Thank you anyway. Maybe later I will test if mysql connector was missing
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  import error (unknown location) pseudo 1 135 Nov-14-2019, 11:47 PM
Last Post: pseudo
  When I import a Module it wont run PyNovice 17 528 Oct-26-2019, 11:14 AM
Last Post: PyNovice
  2 versions of module installed, how to import the earlier one sveto4ka 8 330 Oct-22-2019, 02:51 PM
Last Post: snippsat
  importin function vs. import module hikerguy62 2 231 Jul-28-2019, 03:13 PM
Last Post: ndc85430
  Can't import the CSV module loren41 1 361 May-31-2019, 10:05 PM
Last Post: Yoriz
  Nanpy import error Apretext 2 415 Apr-29-2019, 04:00 PM
Last Post: Apretext
  Python 3 Module Import Help Stealthychu 25 1,830 Oct-28-2018, 04:16 PM
Last Post: Larz60+
  Running pytest gives " 'cannot import name 'session' " error jasonblais 2 691 Oct-10-2018, 05:02 PM
Last Post: jasonblais
  Import Error Bravoseeker 3 1,565 Oct-05-2018, 03:00 PM
Last Post: Larz60+
  module import error bobsmith76 4 817 Sep-18-2018, 03:41 AM
Last Post: bobsmith76

Forum Jump:


Users browsing this thread: 1 Guest(s)