Difficulty connecting python3 to a mariadb - take2

自作多情 提交于 2019-12-25 00:04:17

问题


I asked a previous question with the same title but none of the suggestions worked. I thought a supplementary question with a distillation of all the avenues tried would make for a clearer question than trying to shoehorn it into the original post (but maintain all the feedback from the original: Difficulty connecting Python3 to a MariaDB - take 1) - please let me know (politely) if there's an alternative preferred approach

I have Python2.7 and python3 installed on an OpenSuse (Leap 15.1) and I have I have MariaDB installed

uname -a              Linux TRANQUILITY 4.12.14-lp151.28.13-default #1 SMP Wed Aug 7 07:20:16 UTC 2019 (0c09ad2) x86_64 x86_64 x86_64 GNU/Linux
python2.7 --version   Python 2.7.14
python3 --version     Python 3.6.5
mysql --version       mysql  Ver 15.1 Distrib 10.2.25-MariaDB, for Linux (x86_64) using  EditLine wrapper

My program is meant to parse a folder full of csv files, check whether the file is a CSV and then, if it is, check in a DB table whether that file has been parsed before. The python script (permissions:754) and all the csv files (permissions:644) are in the same folder.

Unfortunately, it will not connect to the DB from the script!

Using exactly the same credentials and from the same path (ie the folder with the csvs & py script):

  • From a bash console, I can easily connect to the DB
me@server:/project/dataCSVs> mysql -u root -p 
Enter password: <myPW>
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 45
Server version: 10.2.25-MariaDB SUSE package
<snip>
MariaDB [(none)]> use myDB;
<snip>
Database changed
MariaDB [myDB]> show tables;
+----------------------+
| Tables_in_myDB |
+----------------------+
| DTP                  |
| Files                |
+----------------------+
2 rows in set (0.00 sec)
  • From the Python3 console and using import pymysql, I can easily connect to the DB
me@server:/project/dataCSVs> python3
Python 3.6.5 (default, Mar 31 2018, 19:45:04) [GCC] on linux
<snip>
>>> import pymysql;
>>> SD = pymysql.connect(host='127.0.0.1', user='root', password='myPW', database='myDB', port=3306);
>>> print(SD);
<pymysql.connections.Connection object at 0x7fae9d277978>
  • From within my Python script, using #!/usr/bin/python2.7 & import MySQLdb, I cannot connect to the DB
  • From within my Python script, using #!/usr/bin/python3 & import pymysql, I cannot connect to the DB

Concentrating on the pymysql driver & python3, the function and relevant additional bits in my code that tries to connect is:

from os import listdir
from datetime import datetime
import pymysql
import shutil
import syslog
import os, sys

<snipped>

def connect_to_DB(action):
    """
    Connect to the DB
    """
    h  = "127.0.0.1", # host
    u  = "root", # username
    p  = "myPW" # password
    d  = "myDB" # database
    Pt = 3306 # port
    if action == 'connect':
        print('Attempting to connect to DB: ' + d)
        print("OK, going for it next!")
        # tried all three of these connect options
        SD = pymysql.connect(host=str(h), user=str(u), passwd=str(p), db=str(d), port=Pt)
        #SD = pymysql.connect(host='127.0.0.1', user='root', passwd='myPW', db='myDB', port=3306)
        #SD = pymysql.connect(str(h), str(u), str(p), str(d), Pt)
        syslog.syslog('Connected to DB')
        return SD
    elif action == 'disconnect':
        SD.close()
        syslog.syslog('Disconnected from DB')
        return 'closed'

<snipped>

# Main start of program
<snipped - commented out everything apart from connect and disconnect to DB>
connection = connect_to_DB('connect') 
<snipped>
disconnection = connect_to_DB('disconnect')

Invoked with python3 myScript.py, I get:

me@MyServer:/project/dataCSVs> python3 myScript.py
Attempting to connect to DB: myDB
OK, going for it next!
DB read failed!
None

Invoked with python3 -v myScript.py, I get (I've only pasted the parts that seems relevant, i.e. a reference to pymysql):

me@server:/project/dataCSVs> python3 -v myScript.py
import _frozen_importlib # frozen
               <snipped>
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/__init__.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/__init__.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/__init__.cpython-36.pyc'
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/_compat.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/_compat.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/_compat.cpython-36.pyc'
import 'pymysql._compat' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37cca9240>
# /usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/__init__.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/constants/__init__.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/__init__.cpython-36.pyc'
import 'pymysql.constants' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37cca9358>
# /usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/FIELD_TYPE.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/constants/FIELD_TYPE.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/FIELD_TYPE.cpython-36.pyc'
import 'pymysql.constants.FIELD_TYPE' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37cca94e0>
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/converters.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/converters.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/converters.cpython-36.pyc'
# /usr/lib64/python3.6/__pycache__/decimal.cpython-36.pyc matches /usr/lib64/python3.6/decimal.py
               <snipped>
import 're' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37cc92b38>
# /usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/FLAG.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/constants/FLAG.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/FLAG.cpython-36.pyc'
import 'pymysql.constants.FLAG' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37c8179e8>
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/charset.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/charset.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/charset.cpython-36.pyc'
import 'pymysql.charset' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37c817b38>
import 'pymysql.converters' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37cca96a0>
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/err.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/err.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/err.cpython-36.pyc'
# /usr/lib64/python3.6/__pycache__/struct.cpython-36.pyc matches /usr/lib64/python3.6/struct.py
# code object from '/usr/lib64/python3.6/__pycache__/struct.cpython-36.pyc'
# extension module '_struct' loaded from '/usr/lib64/python3.6/lib-dynload/_struct.cpython-36m-x86_64-linux-gnu.so'
# extension module '_struct' executed from '/usr/lib64/python3.6/lib-dynload/_struct.cpython-36m-x86_64-linux-gnu.so'
import '_struct' # <_frozen_importlib_external.ExtensionFileLoader object at 0x7fc37c55eac8>
import 'struct' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37c55e898>
# /usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/ER.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/constants/ER.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/ER.cpython-36.pyc'
import 'pymysql.constants.ER' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37c55ec50>
import 'pymysql.err' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37cca9b00>
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/times.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/times.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/times.cpython-36.pyc'
import 'pymysql.times' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37cca9be0>
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/connections.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/connections.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/connections.cpython-36.pyc'
# /usr/lib64/python3.6/__pycache__/__future__.cpython-36.pyc matches /usr/lib64/python3.6/__future__.py
               <snipped>
import 'traceback' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37c594278>
# /usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/CLIENT.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/constants/CLIENT.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/CLIENT.cpython-36.pyc'
import 'pymysql.constants.CLIENT' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37b5f8da0>
# /usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/COMMAND.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/constants/COMMAND.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/COMMAND.cpython-36.pyc'
import 'pymysql.constants.COMMAND' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37b5fe080>
# /usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/CR.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/constants/CR.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/CR.cpython-36.pyc'
import 'pymysql.constants.CR' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37b5fe128>
# /usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/SERVER_STATUS.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/constants/SERVER_STATUS.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/constants/__pycache__/SERVER_STATUS.cpython-36.pyc'
import 'pymysql.constants.SERVER_STATUS' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37b5fe0f0>
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/cursors.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/cursors.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/cursors.cpython-36.pyc'
import 'pymysql.cursors' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37b5fe208>
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/optionfile.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/optionfile.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/optionfile.cpython-36.pyc'
# /usr/lib64/python3.6/__pycache__/configparser.cpython-36.pyc matches /usr/lib64/python3.6/configparser.py
# code object from '/usr/lib64/python3.6/__pycache__/configparser.cpython-36.pyc'
import 'configparser' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37b171320>
import 'pymysql.optionfile' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37b1710f0>
# /usr/lib/python3.6/site-packages/pymysql/__pycache__/util.cpython-36.pyc matches /usr/lib/python3.6/site-packages/pymysql/util.py
# code object from '/usr/lib/python3.6/site-packages/pymysql/__pycache__/util.cpython-36.pyc'
import 'pymysql.util' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37b11d6a0>
# /usr/lib64/python3.6/__pycache__/ssl.cpython-36.pyc matches /usr/lib64/python3.6/ssl.py
               <snipped>
import 'getpass' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37b11dc88>
import 'pymysql.connections' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37c55eba8>
import 'pymysql' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37ccc8240>
# /usr/lib64/python3.6/__pycache__/shutil.cpython-36.pyc matches /usr/lib64/python3.6/shutil.py
               <snipped>
import 'syslog' # <_frozen_importlib_external.ExtensionFileLoader object at 0x7fc37c58d4e0>
Attempting to connect to DB: SunnyData2
OK, going for it next!
# /usr/lib64/python3.6/encodings/__pycache__/idna.cpython-36.pyc matches /usr/lib64/python3.6/encodings/idna.py
# code object from '/usr/lib64/python3.6/encodings/__pycache__/idna.cpython-36.pyc'
# /usr/lib64/python3.6/__pycache__/stringprep.cpython-36.pyc matches /usr/lib64/python3.6/stringprep.py
# code object from '/usr/lib64/python3.6/__pycache__/stringprep.cpython-36.pyc'
# extension module 'unicodedata' loaded from '/usr/lib64/python3.6/lib-dynload/unicodedata.cpython-36m-x86_64-linux-gnu.so'
# extension module 'unicodedata' executed from '/usr/lib64/python3.6/lib-dynload/unicodedata.cpython-36m-x86_64-linux-gnu.so'
import 'unicodedata' # <_frozen_importlib_external.ExtensionFileLoader object at 0x7fc379d65550>
import 'stringprep' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc379d601d0>
import 'encodings.idna' # <_frozen_importlib_external.SourceFileLoader object at 0x7fc37aa4add8>
DB read failed!
None

来源:https://stackoverflow.com/questions/58048931/difficulty-connecting-python3-to-a-mariadb-take2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!