Django config MySQL under MacOS, unable to install mysqlclient problem

Source: Internet
Author: User
Tags mysql in

Recently with the school and enterprise cooperation courses to learn Django, once again understand MVC, is really a lot of emotion, intend to record some problems, as for the entire learning process record this pit and other graduation papers slowly filled.

The environment used by class teachers is Win32 's python3.6.x and Django 2.x. My personal environment is MacOS High Sierra's python3.4.3 and Django 2.x. The DBMS encountered some problems when configuring the database using MySQL. The main one is the inability to install mysqlclient, the PY third-party library. The development environment uses Vscode.

Start with the Django database configuration process and attach the detours you've been through when fixing bugs:

One, use the command line to manipulate MySQL path problem.

The first is to create a database under MySQL, the process is not much to say, the specific Baidu can. If the use of iterm and oh-my-zsh comrades may have a small bug, that is, even if the installation of Mysql,iterm always said to find the path, in fact, with their own terminal is also the same, there are tutorials on the web that should be done when entering MySQL:

1 path="$PATH":/usr/local/mysql/bin2 mysql-u root-p

In theory, the first sentence is to join the path, but in fact,,,, not its role, not you exit terminal or iterm after entering, enter the echo $PATH try. Since join path soft way not to eat, eat hard method, directly open and modify the corresponding configuration file. Refer to the article click here.

    • The path file with terminal in ~/.bash_profile , append an export path= to the end $PATH:/usr/local/mysql/bin/;
    • Iterm+zsh the path file in ~/.ZSHRC , append the same statement at the end.

After that you want to operate MySQL from the command line, you can omit the first sentence to join the path statement.

In fact, you can test, exit the Command Line window and then enter, and then input through the echo $PATH Look at the path, you will find that the path joined successfully.

Second, create the database.

Open Terminal or iterm, enter mysql-u root-p , enter the password and enter MySQL operation.

After that is the normal operation of the MySQL statement, remember that each sentence to end with a semicolon (;), which is the syntax of the SQL statement.

Create Database -- To Create a database named Aisystem  Use -- Enter the database

Third, the configuration Django, unable to install the mysqlclient problem.

1. Create an entity class in

1 # entity class 2 class TUser (models. Model):3     #  self-increment, primary key generally self-increment 4     #  must be specified to the maximum extent, the default varchar  5     password = models. Charfield (MAX_LENGTH=50)

2. Set databases property in

DATABASES = {    'default': {        'ENGINE':'Django.db.backends.mysql',        'NAME':'Aisystem',#the database name already built        'USER':'Root',        'PASSWORD':'ABC',        'HOST':'',        'PORT': 3306,    }}

Here the engine default is SQLite, in this change into mysql,django2.x provides only 4 database engine, reference official documents.

3. Creating Mappings and Tables

That's the advantage of ORM, and we use Django to create tables for maps and databases, so that you can quickly create tables and map to entity classes one by one.

In Vscode Open terminal (shortcut: Control + ~) Input command: python3 makemigrations , which is to create the map, effect .... I forgot, anyway will prompt success, unsuccessful words follow the instructions to solve the bug. And there will be one more folder in the project Magration

The tables are then created directly in the database via Django. The command is as follows: python3 migrate , which is a hint that there is no database engine corresponding to the PY library, whether you need to install mysqlclient. Normal use pip3 install mysqlclient on the line, the problem is in this environment unexpectedly has the following prompt:

Command "python egg_info" failed with error code 1 in/private/var/folders/qq/wfb_clcj46bg5zm_p6vf_wbw0000gn/t/ pip-build-_g_2al4o/mysqlclient/

It is also not possible to find a solution on GitHub on mysqlclient and the bug feedback network of MySQL that it prompts to follow their actions.

May think of directly download the source code and then use python3 install but will report such a bug:

So this method is also not possible.

Iv. the Final Solution.

In this way, it is not necessarily necessary to use the Mysqlclient third-party library to manipulate the database. For MySQL, the old open source DBMS (though it has been a miserable couple of years), Python must have more than one library to operate on. In fact, from the PyPI online we can learn that this library has undergone a change, now its father is pymysql, so we can directly install Pymysql PIP3 Install Pymysql , the installation process is smooth.

At this time we try again Python3 migrate , or will error:

But a closer look at the error will find that it is not able to find the link module, because Django default module name and we do not have the same, this time in fact, according to the prompt, click (Press Command and then click to jump) corresponding py file to modify the module name. The above error is very long, here is only the interception part, need to modify the file in the back, there is no corresponding error message.

Generally speaking is two files, anyway after modifying one, then run, and then prompt, you can find the next need to modify the file. In fact, these modules are the root module name is not the same, the sub-name is the same, so the direct modification is good. Use the Find feature to find mysqlclient and replace it with the pymysql we have installed. Only part of the figure to be modified (I do not think of comments, to modify the place there are 5, 6 bar) after the modification of the python3 migrate , the creation was successful. Tips are as follows:

Look at the database and you'll find these tables:

It is always better to use the error hint, I feel like Python, the analytic language of the error is much more friendly than the compiled type.

Also give me the feeling that MySQL in the Mac platform is a lot of bugs (relative to other mature software), I think as an old open source DBMS, should not be configured so troublesome, maybe I am not enough. At this stage, enough can be done, not to continue to learn.

Django config MySQL under MacOS, unable to install mysqlclient problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.