导入mariadb python的问题

3b6akqbq  于 2022-12-23  发布在  Python
关注(0)|答案(1)|浏览(162)

我创建了一个小pythonfile来测试与数据库的连接:

# Module Imports
import sys
import mariadb

# Connect to MariaDB Platform
try:
    conn = mariadb.connect(
        host = "127.0.0.1",
        port = 3306,
        database = "xxx",
        user = "xxx_admin",
        password = "xxx_admin"
    )
except mariadb.Error as e:
    print(f"Error connecting to MariaDB Platform: {e}")
    sys.exit(1)

# Get Cursor
cur = conn.cursor()

我得到的是:

C:\Users\User\Desktop\xxx_test>python mariadb_test.py
19:53:29
Traceback (most recent call last):
  File "mariadb_test.py", line 5, in <module>
    import mariadb
  File "C:\Python37\lib\site-packages\mariadb\__init__.py", line 10, in <module>
    from ._mariadb import (
ImportError: Mariadb module initialization failed

为什么我得到的时间和失败的初始化Mariadb?

klr1opcd

klr1opcd1#

我尝试了Ubuntu 22.04.1 LTS,它运行在Windows 11 WSL中,带有python 3.10.6和mariadb 1.1.5.post3
看起来,你在导入mariadb的时候得到了。我安装了连接器和mariadb包,有了这个我就可以执行import mariadb
在Ubuntu中,您可以执行以下命令来安装mariadb连接器:

sudo apt-get update -y
sudo apt-get install -y libmariadb-dev
pip install mariadb

libmariadb-dev的当前版本为1:10.6.11(https://packages.debian.org/sid/libmariadb-dev
在此之后,打开python shell并

import mariadb

你也可以看看这篇文章,以获得更多帮助:Problem with pip install mariadb - mariadb_config not found

相关问题