Home Backend Development PHP Tutorial Solution to unsuccessful installation of mysql in Python under Mac

Solution to unsuccessful installation of mysql in Python under Mac

Feb 27, 2018 am 11:18 AM
mysql python

If mysql-python is installed, an error is reported after importing MySQLdb. It is recommended not to use mysql-python. It is not supported after Python 3.6.

I personally use Third-party library pymysql, enter the following code in the terminal to install

sudo pip install pymysql
Copy after login
Copy after login

After installation, if you are using flask_sqlalchemy, please change the driver name to pymysql

SQLALCHEMY_DATABASE_URI = '[数据库名]+[数据库中间件(驱动)]://[用户名]:
[password]@[主机IP地址]:[端口号]/[数据库名字]?charset=utf8'SQLALCHEMY_TRACK_MODIFICATIONS = True
Copy after login
Copy after login

The following is an example of a configuration file used to connect to the database

# -*- coding:utf-8 -*-# config.pyDIALECT = 'mysql'DRIVER = 'pymysql'USERNAME = 'root'PASSWORD = 'root'# HOST = '127.0.0.1'    # 自己电脑的ip 或者localhostHOST = 'localhost'      # 自己电脑的ipPORT = '3306'           # MySQL默认的端口号DATABASE = 'db_demo1'# SQLALCHEMY_DATABASE_URI = '[数据库名]+[数据库中间件(驱动)]://[用户名]:[password]@[主机IP地址]:[端口号]/[数据库名字]?charset=utf8'# SQLALCHEMY_TRACK_MODIFICATIONS = True# 数据库连接必须用这个名字  SQLALCHEMY_DATABASE_URISQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(DIALECT, DRIVER, USERNAME, PASSWORD, HOST, PORT, DATABASE)
Copy after login
Copy after login
# -*- coding:utf-8 -*-# db_test.pyfrom flask import Flaskfrom flask_sqlalchemy import SQLAlchemyimport config

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)

db.create_all()@app.route('/')def index():
    return 'index'if __name__ == '__main__':
    app.run()
Copy after login
Copy after login

             

If mysql-python is installed and an error is reported after importing MySQLdb, it is recommended not to use mysql-python. It is not supported after Python 3.6.

I personally use a third-party library pymysql. Enter the following code in the terminal to install

sudo pip install pymysql
Copy after login
Copy after login

After installation, if Using flask_sqlalchemy Please change the driver name to pymysql

SQLALCHEMY_DATABASE_URI = '[数据库名]+[数据库中间件(驱动)]://[用户名]:
[password]@[主机IP地址]:[端口号]/[数据库名字]?charset=utf8'SQLALCHEMY_TRACK_MODIFICATIONS = True
Copy after login
Copy after login

The following is an example of a configuration file used to connect to the database

# -*- coding:utf-8 -*-# config.pyDIALECT = 'mysql'DRIVER = 'pymysql'USERNAME = 'root'PASSWORD = 'root'# HOST = '127.0.0.1'    # 自己电脑的ip 或者localhostHOST = 'localhost'      # 自己电脑的ipPORT = '3306'           # MySQL默认的端口号DATABASE = 'db_demo1'# SQLALCHEMY_DATABASE_URI = '[数据库名]+[数据库中间件(驱动)]://[用户名]:[password]@[主机IP地址]:[端口号]/[数据库名字]?charset=utf8'# SQLALCHEMY_TRACK_MODIFICATIONS = True# 数据库连接必须用这个名字  SQLALCHEMY_DATABASE_URISQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(DIALECT, DRIVER, USERNAME, PASSWORD, HOST, PORT, DATABASE)
Copy after login
Copy after login
# -*- coding:utf-8 -*-# db_test.pyfrom flask import Flaskfrom flask_sqlalchemy import SQLAlchemyimport config

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)

db.create_all()@app.route('/')def index():
    return 'index'if __name__ == '__main__':
    app.run()
Copy after login
Copy after login

Related recommendations:

Detailed explanation of the steps to install MySql5.7.21 in Linux

How to install MySQL5.7 on Windows 10 and change the forgotten root password

Linux environment Graphic tutorial for installing MySQL5.6

The above is the detailed content of Solution to unsuccessful installation of mysql in Python under Mac. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Detailed explanation of docker principle Detailed explanation of docker principle Apr 14, 2025 pm 11:57 PM

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

Python vs. JavaScript: Community, Libraries, and Resources Python vs. JavaScript: Community, Libraries, and Resources Apr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

MySQL vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

How to install nginx in centos How to install nginx in centos Apr 14, 2025 pm 08:06 PM

CentOS Installing Nginx requires following the following steps: Installing dependencies such as development tools, pcre-devel, and openssl-devel. Download the Nginx source code package, unzip it and compile and install it, and specify the installation path as /usr/local/nginx. Create Nginx users and user groups and set permissions. Modify the configuration file nginx.conf, and configure the listening port and domain name/IP address. Start the Nginx service. Common errors need to be paid attention to, such as dependency issues, port conflicts, and configuration file errors. Performance optimization needs to be adjusted according to the specific situation, such as turning on cache and adjusting the number of worker processes.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

How to call docker lnmp How to call docker lnmp Apr 15, 2025 am 11:15 AM

Docker LNMP container call steps: Run the container: docker run -d --name lnmp-container -p 80:80 -p 443:443 lnmp-stack to get the container IP: docker inspect lnmp-container | grep IPAddress access website: http://<Container IP>/index.phpSSH access: docker exec -it lnmp-container bash access MySQL: mysql -u roo

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

See all articles