Я задал предыдущий вопрос с тем же названием, но ни одно из предложений не сработало.Я думал, что дополнительный вопрос с перебором всех опробованных путей мог бы дать более ясный вопрос, чем попытка вставить его в исходный пост (но сохранить все отзывы из оригинала: Сложность подключения Python3 к MariaDB - дубль 1) - пожалуйста, дайте мне знать (вежливо), если есть альтернативный предпочтительный подход
У меня Python2.7 и python3 установлены в OpenSuse (Leap 15.1), и у меня установлена MariaDB
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
Моя программа предназначена для анализа папки, полной файлов CSV, проверки, является ли файл CSV, а затем, если это так, проверка в таблице БД, анализировался ли этот файл ранее.Сценарий python (разрешения: 754
) и все файлы csv (разрешения: 644
) находятся в одной папке.
К сожалению, он не будет подключаться к БД из сценария!
Использование точно таких же учетных данных и с тем же путем (т. Е. Папка со сценарием csvs & py):
- Из консоли bashЯ могу легко подключиться к БД
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)
- С консоли Python3 и, используя
import pymysql
, я могу легко подключиться к БД
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>
- Из моего скрипта Python, используя
#!/usr/bin/python2.7
& import MySQLdb
, я не могу подключиться к БД - Из моего скрипта Python, используя
#!/usr/bin/python3
& import pymysql
, я не могу подключитьсяк БД
Концентрируясь на драйвере pymysql
и python3
, функция и соответствующие дополнительные биты в моем коде, который пытается подключиться:
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')
Вызывается сpython3 myScript.py
, я получаю:
me@MyServer:/project/dataCSVs> python3 myScript.py
Attempting to connect to DB: myDB
OK, going for it next!
DB read failed!
None
Вызванный с помощью python3 -v myScript.py
, я получаю (я вставил только те части, которые кажутся актуальными, то есть ссылкув 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