MacOs installation python3 of mysqlclient component supports django run

MacOs start after installation python3 django site, suggesting mysqlclient component does not exist, and direct installation error, Chung What should we do?

Today nothing else, increase hard disk 1T, Apple unfavorable installation, Django error angry.

django.core.exceptions.ImproperlyConfigured: 
Error loading MySQLdb module: 
No module named 'MySQLdb'

Error loading MySQLdb module.
Did you install mysqlclient?

  

To remember this Problem toss for a long time, today wrote to the settlement process, and finally figured out the problem.

This problem is not compatible with the version of the problem, in fact, there are two problems:
1, MySQLdb and mysqlclient problem. Both are driven stuff mysql for python Django's official support of 90% of claims on MySqldb components online is incorrect or outdated: "MySqldb does not support the use pymysql python3 must be replaced." . mysqlclient is the improvement comes from MySqldb full support Python3, if you are a window or Linux installed according to the following URL tutorial like to use.

https://github.com/PyMySQL/mysqlclient-python

2, the special nature of the MacOS. brew install MacOS system in which a lot of trouble to toss a lot, and there are many components to the Linux installation method is different.

Two solutions:

A program, alternative

Specific works: Use pymysql component (this component can support python3), replace django of mysqlclient.
The components of the Oracle native, replaced pymysql, the efficiency may be reduced.

1, using the following command line installation PyMySQL

 

2, add the following code to the same level in setting.py init.py files in the project.
import pymysql
pymysql.install_as_MySQLdb()

  

Scheme II installation mysqlclient

Mysqlclient be mounted on MacOS trouble, the following is a brief steps:

1, install xcode

Xcode find in the App Store and install it until you see this icon, you can open agree:

 

 
Xcode

Then run in the terminal:

xcode-select --install

  


2, the installation brew.

Many components mysqlclient needs must be installed from the brew, because foreign mirroring is slow, it is recommended to install a mirror from domestic USTC.

2.1 official website to install Homebrew (not recommended)

Official website URL: https://brew.sh/

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
 

Special reasons to install speed may be broken, it is recommended to use some external network with Internet access tools for installation.

2.2 Domestic Mirror installation (recommended)

(1) get install file

Obtain official website and save the script called brew_install
(This step also need a little bit of technical means to interconnect the Internet):
https://raw.githubusercontent.com/Homebrew/install/master/install

(2) replace mirror Tsinghua

Open brew_install file, modified as follows:

Locate the following line of code:

BREW_REPO = “https://github.com/Homebrew/brew“.freeze
CORE_TAP_REPO = “https://github.com/Homebrew/homebrew-core“.freeze

  

change to:

BREW_REPO = “https://mirrors.ustc.edu.cn/brew.git “.freeze
CORE_TAP_REPO = “https://mirrors.ustc.edu.cn/homebrew-core.git“.freeze

  

Note: The new version HomeBrew might not have CORE_TAP_REPOthis code, if there is no need to add. If this image in question, then, can be replaced by other sources.

(3) execute the script

Open a terminal allows scripts

/usr/bin/ruby brew_install

  

 
Installation process

If lucky, over the entire installation, on the next step (4).

If the script stopped at this time

==> Tapping homebrew/core

Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'... 

The reason this occurs is because the source barrier, not down to the code, the solution is to replace the national mirror source:

Sentence following the command executed manually, as replacement mirror CAS:

git clone git://mirrors.ustc.edu.cn/homebrew-core.git/ /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core --depth=1

  


Then homebrew-core is also set to address a mirror image of domestic Chinese Academy of Sciences
cd "$(brew --repo)"

git remote set-url origin https://mirrors.ustc.edu.cn/brew.git

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"

git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

  

Perform the update successfully:

brew update

  

Finally, check the error with this command:

brew doctor

  

At this point HomeBrew installation is complete.

(4) Change the default source

Directly Homebrew also need to change the default source. The following is the default source for domestic USTC replace the source method. as follows:

Replace warehouse core software

 
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

  

Replace cask depot (provided macOS applications and large binary files)

cd "$(brew --repo)"/Library/Taps/caskroom/homebrew-cask
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git

  

Replace Bottles source (Homebrew pre-compiled binary packages)

bash (the default shell) users:

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

  

(6) brew commonly used commands

 
 
3, install the necessary components

Gcc installed components:
brew install gcc

  


After installation, re-installation
brew install openssl
 
 
And the following variables should ring set up, easy to find python3. Otherwise, the following error occurs:

    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    error: command 'gcc' failed with exit status 1
/private/var/folders/by/c1t20gqd2b3crsh5v7x1gfh80000gn/T/pip-install-unlf6wgo/mysqlclient/
This is the command line, so set your environment:
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

  

Finally, you can safely install:

python3 -m pip install mysqlclient

  

Really get it working, use a program, it can also be used.

 

Guess you like

Origin www.cnblogs.com/dosboy/p/12508076.html