python Django 连接数据库失败的解决方法

使用

sudo pip install mysqlclient

报错:

错误提示:

Command "python setup.py egg_info" failed with error code 1 in /private/tmp/pip-install-n_k_5ptf/mysqlclient/

解决方法:

在终端输入:easy_install -U setuptools

输出结果:

(venv) lidongxiaodeMacBook-Pro:Json2 lidongxiao$ easy_install -U setuptools
Searching for setuptools
Reading https://pypi.org/simple/setuptools/
Downloading https://files.pythonhosted.org/packages/82/a1/ba6fb41367b375f5cb653d1317d8ca263c636cff6566e2da1b0da716069d/setuptools-40.5.0-py2.py3-none-any.whl#sha256=e329a5c458c6acb5edc2b5c4ad44280c053ba827dc82fd5e84a83e22bb05460d
Best match: setuptools 40.5.0
Processing setuptools-40.5.0-py2.py3-none-any.whl
Installing setuptools-40.5.0-py2.py3-none-any.whl to /Users/lidongxiao/venv/lib/python3.7/site-packages
writing requirements to /Users/lidongxiao/venv/lib/python3.7/site-packages/setuptools-40.5.0-py3.7.egg/EGG-INFO/requires.txt
Adding setuptools 40.5.0 to easy-install.pth file
Installing easy_install script to /Users/lidongxiao/venv/bin
Installing easy_install-3.6 script to /Users/lidongxiao/venv/bin

Installed /Users/lidongxiao/venv/lib/python3.7/site-packages/setuptools-40.5.0-py3.7.egg
Processing dependencies for setuptools
Finished processing dependencies for setuptools

运行完成后输入:

sudo pip install jupyter

输出结果:

(venv) lidongxiaodeMacBook-Pro:Json2 lidongxiao$ sudo pip install jupyter

The directory '/Users/lidongxiao/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/lidongxiao/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting jupyter
Downloading https://files.pythonhosted.org/packages/83/df/0f5dd132200728a86190397e1ea87cd76244e42d39ec5e88efd25b2abd7e/jupyter-1.0.0-py2.py3-none-any.whl
Collecting nbconvert (from jupyter)
Downloading https://files.pythonhosted.org/packages/b5/bb/94c493051d60e5b9c0f7f9a368b324201818c1b1c4cae85d1e49a41846c7/nbconvert-5.4.0-py2.py3-none-any.whl (405kB)
100% |████████████████████████████████| 409kB 47kB/s
Collecting jupyter-console (from jupyter)
Downloading https://files.pythonhosted.org/packages/cb/ee/6374ae8c21b7d0847f9c3722dcdfac986b8e54fa9ad9ea66e1eb6320d2b8/jupyter_console-6.0.0-py2.py3-none-any.whl
Collecting ipykernel (from jupyter)
Downloading https://files.pythonhosted.org/packages/d8/b0/f0be5c5ab335196f5cce96e5b889a4fcf5bfe462eb0acc05cd7e2caf65eb/ipykernel-5.1.0-py3-none-any.whl (113kB)
100% |████████████████████████████████| 122kB 65kB/s
Collecting ipywidgets (from jupyter)
Downloading https://files.pythonhosted.org/packages/30/9a/a008c7b1183fac9e52066d80a379b3c64eab535bd9d86cdc29a0b766fd82/ipywidgets-7.4.2-py2.py3-none-any.whl (111kB)
100% |████████████████████████████████| 112kB 97kB/s
Collecting notebook (from jupyter)
Downloading https://files.pythonhosted.org/packages/44/16/9f108b675828c4117cfe72d8d0f97094163c40584e40c46ec48a1e862693/notebook-5.7.0-py2.py3-none-any.whl (9.0MB)
100% |████████████████████████████████| 9.0MB 148kB/s
Collecting qtconsole (from jupyter)
Downloading https://files.pythonhosted.org/packages/37/22/0d8474f78a8c421d485ac2339de7c871d535160f09f170de90c8185b87c4/qtconsole-4.4.2-py2.py3-none-any.whl (112kB)
100% |████████████████████████████████| 112kB 155kB/s
Collecting bleach (from nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/d4/0d/4696373c3b714f6022d668fbab619690a42050dbeacede6d10ed97fbd3e2/bleach-3.0.2-py2.py3-none-any.whl (148kB)
100% |████████████████████████████████| 153kB 228kB/s
Collecting nbformat>=4.4 (from nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/da/27/9a654d2b6cc1eaa517d1c5a4405166c7f6d72f04f6e7eea41855fe808a46/nbformat-4.4.0-py2.py3-none-any.whl (155kB)
100% |████████████████████████████████| 163kB 16.4MB/s
Collecting pandocfilters>=1.4.1 (from nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/4c/ea/236e2584af67bb6df960832731a6e5325fd4441de001767da328c33368ce/pandocfilters-1.4.2.tar.gz
Collecting traitlets>=4.2 (from nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/93/d6/abcb22de61d78e2fc3959c964628a5771e47e7cc60d53e9342e21ed6cc9a/traitlets-4.3.2-py2.py3-none-any.whl (74kB)
100% |████████████████████████████████| 81kB 181kB/s
Collecting entrypoints>=0.2.2 (from nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/cc/8b/4eefa9b47f1910b3d2081da67726b066e379b04ca897acfe9f92bac56147/entrypoints-0.2.3-py2.py3-none-any.whl
Collecting testpath (from nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/be/a4/162f9ebb6489421fe46dcca2ae420369edfee4b563c668d93cb4605d12ba/testpath-0.4.2-py2.py3-none-any.whl (163kB)
100% |████████████████████████████████| 163kB 117kB/s
Collecting defusedxml (from nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/87/1c/17f3e3935a913dfe2a5ca85fa5ccbef366bfd82eb318b1f75dadbf0affca/defusedxml-0.5.0-py2.py3-none-any.whl
Requirement already satisfied: jinja2 in /Users/lidongxiao/venv/lib/python3.7/site-packages (from nbconvert->jupyter) (2.10)
Collecting mistune>=0.8.1 (from nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/09/ec/4b43dae793655b7d8a25f76119624350b4d65eb663459eb9603d7f1f0345/mistune-0.8.4-py2.py3-none-any.whl
Collecting pygments (from nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/02/ee/b6e02dc6529e82b75bb06823ff7d005b141037cb1416b10c6f00fc419dca/Pygments-2.2.0-py2.py3-none-any.whl (841kB)
100% |████████████████████████████████| 849kB 93kB/s
Collecting jupyter-core (from nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/1d/44/065d2d7bae7bebc06f1dd70d23c36da8c50c0f08b4236716743d706762a8/jupyter_core-4.4.0-py2.py3-none-any.whl (126kB)
100% |████████████████████████████████| 133kB 114kB/s
Collecting ipython (from jupyter-console->jupyter)
Downloading https://files.pythonhosted.org/packages/1b/e2/ffb8c1b574f972cf4183b0aac8f16b57f1e3bbe876b31555b107ea3fd009/ipython-7.1.1-py3-none-any.whl (764kB)
100% |████████████████████████████████| 768kB 157kB/s
Collecting jupyter-client (from jupyter-console->jupyter)
Downloading https://files.pythonhosted.org/packages/94/dd/fe6c4d683b09eb05342bd2816b7779663f71762b4fa9c2d5203d35d17354/jupyter_client-5.2.3-py2.py3-none-any.whl (89kB)
100% |████████████████████████████████| 92kB 83kB/s
Collecting prompt-toolkit<2.1.0,>=2.0.0 (from jupyter-console->jupyter)
Downloading https://files.pythonhosted.org/packages/d1/e6/adb3be5576f5d27c6faa33f1e9fea8fe5dbd9351db12148de948507e352c/prompt_toolkit-2.0.7-py3-none-any.whl (338kB)
100% |████████████████████████████████| 348kB 120kB/s
Collecting tornado>=4.2 (from ipykernel->jupyter)
Downloading https://files.pythonhosted.org/packages/e6/78/6e7b5af12c12bdf38ca9bfe863fcaf53dc10430a312d0324e76c1e5ca426/tornado-5.1.1.tar.gz (516kB)
100% |████████████████████████████████| 522kB 158kB/s
Collecting widgetsnbextension~=3.4.0 (from ipywidgets->jupyter)
Downloading https://files.pythonhosted.org/packages/8a/81/35789a3952afb48238289171728072d26d6e76649ddc8b3588657a2d78c1/widgetsnbextension-3.4.2-py2.py3-none-any.whl (2.2MB)
100% |████████████████████████████████| 2.2MB 448kB/s
Collecting Send2Trash (from notebook->jupyter)
Downloading https://files.pythonhosted.org/packages/49/46/c3dc27481d1cc57b9385aff41c474ceb7714f7935b1247194adae45db714/Send2Trash-1.5.0-py3-none-any.whl
Collecting ipython-genutils (from notebook->jupyter)
Downloading https://files.pythonhosted.org/packages/fa/bc/9bd3b5c2b4774d5f33b2d544f1460be9df7df2fe42f352135381c347c69a/ipython_genutils-0.2.0-py2.py3-none-any.whl
Collecting prometheus-client (from notebook->jupyter)
Downloading https://files.pythonhosted.org/packages/61/84/9aa657b215b04f21a72ca8e50ff159eef9795096683e4581a357baf4dde6/prometheus_client-0.4.2.tar.gz
Collecting pyzmq>=17 (from notebook->jupyter)
Downloading https://files.pythonhosted.org/packages/bd/ce/d068e7494c29ddf0c6850186fc4a81ec180b4612a30ba187ceb3fd266179/pyzmq-17.1.2-cp37-cp37m-macosx_10_9_x86_64.whl (741kB)
100% |████████████████████████████████| 747kB 464kB/s
Collecting terminado>=0.8.1 (from notebook->jupyter)
Downloading https://files.pythonhosted.org/packages/2e/20/a26211a24425923d46e1213b376a6ee60dc30bcdf1b0c345e2c3769deb1c/terminado-0.8.1-py2.py3-none-any.whl
Collecting webencodings (from bleach->nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/f4/24/2a3e3df732393fed8b3ebf2ec078f05546de641fe1b667ee316ec1dcf3b7/webencodings-0.5.1-py2.py3-none-any.whl
Collecting six (from bleach->nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Collecting jsonschema!=2.5.0,>=2.4 (from nbformat>=4.4->nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/77/de/47e35a97b2b05c2fadbec67d44cfcdcd09b8086951b331d82de90d2912da/jsonschema-2.6.0-py2.py3-none-any.whl
Collecting decorator (from traitlets>=4.2->nbconvert->jupyter)
Downloading https://files.pythonhosted.org/packages/bc/bb/a24838832ba35baf52f32ab1a49b906b5f82fb7c76b2f6a7e35e140bac30/decorator-4.3.0-py2.py3-none-any.whl
Requirement already satisfied: MarkupSafe>=0.23 in /Users/lidongxiao/venv/lib/python3.7/site-packages (from jinja2->nbconvert->jupyter) (1.0)
Collecting backcall (from ipython->jupyter-console->jupyter)
Downloading https://files.pythonhosted.org/packages/84/71/c8ca4f5bb1e08401b916c68003acf0a0655df935d74d93bf3f3364b310e0/backcall-0.1.0.tar.gz
Requirement already satisfied: setuptools>=18.5 in /Users/lidongxiao/venv/lib/python3.7/site-packages (from ipython->jupyter-console->jupyter) (33.1.1)
Collecting pickleshare (from ipython->jupyter-console->jupyter)
Downloading https://files.pythonhosted.org/packages/9a/41/220f49aaea88bc6fa6cba8d05ecf24676326156c23b991e80b3f2fc24c77/pickleshare-0.7.5-py2.py3-none-any.whl
Collecting pexpect; sys_platform != "win32" (from ipython->jupyter-console->jupyter)
Downloading https://files.pythonhosted.org/packages/89/e6/b5a1de8b0cc4e07ca1b305a4fcc3f9806025c1b651ea302646341222f88b/pexpect-4.6.0-py2.py3-none-any.whl (57kB)
100% |████████████████████████████████| 61kB 16.8MB/s
Collecting jedi>=0.10 (from ipython->jupyter-console->jupyter)
Downloading https://files.pythonhosted.org/packages/7a/1a/9bd24a185873b998611c2d8d4fb15cd5e8a879ead36355df7ee53e9111bf/jedi-0.13.1-py2.py3-none-any.whl (177kB)
100% |████████████████████████████████| 184kB 473kB/s
Collecting appnope; sys_platform == "darwin" (from ipython->jupyter-console->jupyter)
Downloading https://files.pythonhosted.org/packages/87/a9/7985e6a53402f294c8f0e8eff3151a83f1fb901fa92909bb3ff29b4d22af/appnope-0.1.0-py2.py3-none-any.whl
Collecting python-dateutil>=2.1 (from jupyter-client->jupyter-console->jupyter)
Downloading https://files.pythonhosted.org/packages/74/68/d87d9b36af36f44254a8d512cbfc48369103a3b9e474be9bdfe536abfc45/python_dateutil-2.7.5-py2.py3-none-any.whl (225kB)
100% |████████████████████████████████| 235kB 470kB/s
Collecting wcwidth (from prompt-toolkit<2.1.0,>=2.0.0->jupyter-console->jupyter)
Downloading https://files.pythonhosted.org/packages/7e/9f/526a6947247599b084ee5232e4f9190a38f398d7300d866af3ab571a5bfe/wcwidth-0.1.7-py2.py3-none-any.whl
Collecting ptyprocess; os_name != "nt" (from terminado>=0.8.1->notebook->jupyter)
Downloading https://files.pythonhosted.org/packages/d1/29/605c2cc68a9992d18dada28206eeada56ea4bd07a239669da41674648b6f/ptyprocess-0.6.0-py2.py3-none-any.whl
Collecting parso>=0.3.0 (from jedi>=0.10->ipython->jupyter-console->jupyter)
Downloading https://files.pythonhosted.org/packages/09/51/9c48a46334be50c13d25a3afe55fa05c445699304c5ad32619de953a2305/parso-0.3.1-py2.py3-none-any.whl (88kB)
100% |████████████████████████████████| 92kB 138kB/s
Installing collected packages: webencodings, six, bleach, jsonschema, decorator, ipython-genutils, traitlets, jupyter-core, nbformat, pandocfilters, entrypoints, testpath, defusedxml, mistune, pygments, nbconvert, pyzmq, python-dateutil, tornado, jupyter-client, backcall, pickleshare, ptyprocess, pexpect, parso, jedi, wcwidth, prompt-toolkit, appnope, ipython, ipykernel, jupyter-console, Send2Trash, prometheus-client, terminado, notebook, widgetsnbextension, ipywidgets, qtconsole, jupyter
Running setup.py install for pandocfilters ... done
Running setup.py install for tornado ... done
Running setup.py install for backcall ... done
Running setup.py install for prometheus-client ... done
Successfully installed Send2Trash-1.5.0 appnope-0.1.0 backcall-0.1.0 bleach-3.0.2 decorator-4.3.0 defusedxml-0.5.0 entrypoints-0.2.3 ipykernel-5.1.0 ipython-7.1.1 ipython-genutils-0.2.0 ipywidgets-7.4.2 jedi-0.13.1 jsonschema-2.6.0 jupyter-1.0.0 jupyter-client-5.2.3 jupyter-console-6.0.0 jupyter-core-4.4.0 mistune-0.8.4 nbconvert-5.4.0 nbformat-4.4.0 notebook-5.7.0 pandocfilters-1.4.2 parso-0.3.1 pexpect-4.6.0 pickleshare-0.7.5 prometheus-client-0.4.2 prompt-toolkit-2.0.7 ptyprocess-0.6.0 pygments-2.2.0 python-dateutil-2.7.5 pyzmq-17.1.2 qtconsole-4.4.2 six-1.11.0 terminado-0.8.1 testpath-0.4.2 tornado-5.1.1 traitlets-4.3.2 wcwidth-0.1.7 webencodings-0.5.1 widgetsnbextension-3.4.2

运行完成后:

pip install pymysql

安装pymysql数据库

安装完成后系统没有发现mysql

在__init__.py中添加代码

import pymysql
pymysql.install_as_MySQLdb()

一切准备完成
就可以使用

sudo pip install mysqlclient

输出:

(venv) lidongxiaodeMacBook-Pro:Json2 lidongxiao$ django-admin startapp TestModel

(venv) lidongxiaodeMacBook-Pro:Json2 lidongxiao$ python manage.py migrate

完成,连上了

原文地址:https://www.cnblogs.com/lidongxiao/p/9913584.html