【错】ModuleNotFoundError: No module named 'ConfigParser'

【错】ModuleNotFoundError: No module named 'ConfigParser',第1张

python3安装 pip install mysql-python 的时候提示 ModuleNotFoundError: No module named 'ConfigParser' 。

网络上的解决办法是将 /python3.7/lib/python3.7/configparser.py 改为 /python3.7/lib/python3.7/ConfigParser.py ,但是我的 /python3.7/lib/python3.7目录下并没有 configparser.py`。

这个错误是应为安装 mysql-python 出错的,找了其他的办法解决了在python3下边 mysql-python 的使用。 方法点击这里。

1、MySQLdb

# 前置条件

sudo apt-get install python-dev libmysqlclient-dev # Ubuntu

sudo yum install python-devel mysql-devel # Red Hat / CentOS

# 安装

pip install MySQL-python

Windows 直接通过下载 exe 文件安装

#!/usr/bin/python

import MySQLdb

db = MySQLdb.connect(

host = "localhost", # 主机名

user = "root", # 用户名

passwd = "pythontab.com", # 密码

db = "testdb") # 数据库名称

# 查询前,必须先获取游标

cur = db.cursor()

# 执行的都是原生SQL语句

cur.execute("SELECT * FROM mytable")

for row in cur.fetchall():

print(row[0])

db.close()

2、mysqlclient

# Windows安装

pip install some-package.whl

# linux 前置条件

sudo apt-get install python3-dev # debian / Ubuntu

sudo yum install python3-devel # Red Hat / CentOS

brew install mysql-connector-c # macOS (Homebrew)

pip install mysqlclient

3、PyMySQL

pip install PyMySQL

# 为了兼容mysqldb,只需要加入

pymysql.install_as_MySQLdb()

import pymysql

conn = pymysql.connect(host = '127.0.0.1', user = 'root', passwd = "pythontab.com", db = 'testdb')

cur = conn.cursor()

cur.execute("SELECT Host,User FROM user")

for r in cur:

print(r)

cur.close()

conn.close()

4、peewee

pip install peewee

import peewee

from peewee import *

db = MySQLDatabase('testdb', user = 'root', passwd = 'pythontab.com')

class Book(peewee.Model):

author = peewee.CharField()

title = peewee.TextField()

class Meta:

database = db

Book.create_table()

book = Book(author = "pythontab", title = 'pythontab is good website')

book.save()

for book in Book.filter(author = "pythontab"):

print(book.title)

5、SQLAlchemy

from sqlalchemy import create_engine

from sqlalchemy.orm import sessionmaker

from sqlalchemy_declarative import Address, Base, Person

class Address(Base):

__tablename__ = 'address'

id = Column(Integer, primary_key = True)

street_name = Column(String(250))

engine = create_engine('sqlite:///sqlalchemy_example.db')

Base.metadata.bind = engine

DBSession = sessionmaker(bind = engine)

session = DBSession()

# Insert a Person in the person table

new_person = Person(name = 'new person')

session.add(new_person)

session.commit()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/8670427.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-19
下一篇 2023-04-19

发表评论

登录后才能评论

评论列表(0条)

保存