项目初始化
系统环境
python 3.7.3
Django 2.2.4
DjangoRestFramework 3.10.2
第一步:启动虚拟化环境
virtualenv虚拟化环境搭建:https://www.cnblogs.com/bk770466199/p/11366374.html
第二步:安装相关依赖包
#查看当前安装依赖包列表 pip list #豆瓣源加速安装Django pip install -i https://pypi.douban.com/simple django 虽然pycharm的设置里可以安装依赖包,但是有些依赖包下载过慢,用豆瓣源加速安装,在命令行里面安装好相关的包,再在pycharm里生成项目,避免因下载包过慢卡死。 #pip安装DRF pip install djangorestframework
第三步:pycharm生成项目
创建mysql数据库
配置setting,使用我们创建的数据库启动Django项目
DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘NAME‘: "myshop", ‘USER‘: "root", ‘PASSWORD‘: "12345678", ‘HOST‘: "127.0.0.1", "OPTIONS": {"init_command": "SET default_storage_engine=INNODB;"} } } 数据库的配置从网上都可以搜到,生产环境中可以根据系统类型分别配置,使得测试环境和生产环境可以分别连接对应的数据库。
安装mysqlclient
python连接mysql需要安装依赖包mysqlclient。
正常使用豆瓣源加速安装会报错,如下:
(wText) C:\Users\zhangw>pip install -i https://pypi.douban.com/simple mysqlclient Looking in indexes: https://pypi.douban.com/simple Collecting mysqlclient Downloading https://pypi.doubanio.com/packages/4d/38/c5f8bac9c50f3042c8f05615f84206f77f03db79781db841898fde1bb284/mysqlclient-1.4.4.tar.gz (86kB) |████████████████████████████████| 92kB 68kB/s Installing collected packages: mysqlclient Running setup.py install for mysqlclient ... error ERROR: Command errored out with exit status 1: command: ‘d:\python3\python.exe‘ -u -c ‘import sys, setuptools, tokenize; sys.argv[0] = ‘"‘"‘C:\\Users\\zhangw\\AppData\\Local\\Temp\\pip-install-o3l0fmnv\\mysqlclient\\setup.py‘"‘"‘; __file__=‘"‘"‘C:\\Users\\zhangw\\AppData\\Local\\Temp\\pip-install-o3l0fmnv\\mysqlclient\\setup.py‘"‘"‘;f=getattr(tokenize, ‘"‘"‘open‘"‘"‘, open)(__file__);code=f.read().replace(‘"‘"‘\r\n‘"‘"‘, ‘"‘"‘\n‘"‘"‘);f.close();exec(compile(code, __file__, ‘"‘"‘exec‘"‘"‘))‘ install --record ‘C:\Users\zhangw\AppData\Local\Temp\pip-record-6pjq9f0m\install-record.txt‘ --single-version-externally-managed --compile cwd: C:\Users\zhangw\AppData\Local\Temp\pip-install-o3l0fmnv\mysqlclient Complete output (30 lines): running install running build running build_py creating build creating build\lib.win-amd64-3.7 creating build\lib.win-amd64-3.7\MySQLdb copying MySQLdb\__init__.py -> build\lib.win-amd64-3.7\MySQLdb copying MySQLdb\_exceptions.py -> build\lib.win-amd64-3.7\MySQLdb copying MySQLdb\compat.py -> build\lib.win-amd64-3.7\MySQLdb copying MySQLdb\connections.py -> build\lib.win-amd64-3.7\MySQLdb copying MySQLdb\converters.py -> build\lib.win-amd64-3.7\MySQLdb copying MySQLdb\cursors.py -> build\lib.win-amd64-3.7\MySQLdb copying MySQLdb\release.py -> build\lib.win-amd64-3.7\MySQLdb copying MySQLdb\times.py -> build\lib.win-amd64-3.7\MySQLdb creating build\lib.win-amd64-3.7\MySQLdb\constants copying MySQLdb\constants\__init__.py -> build\lib.win-amd64-3.7\MySQLdb\constants copying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-3.7\MySQLdb\constants copying MySQLdb\constants\CR.py -> build\lib.win-amd64-3.7\MySQLdb\constants copying MySQLdb\constants\ER.py -> build\lib.win-amd64-3.7\MySQLdb\constants copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-3.7\MySQLdb\constants copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-3.7\MySQLdb\constants running build_ext building ‘MySQLdb._mysql‘ extension creating build\temp.win-amd64-3.7 creating build\temp.win-amd64-3.7\Release creating build\temp.win-amd64-3.7\Release\MySQLdb C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Dversion_info=(1,4,4,‘final‘,0) -D__version__=1.4.4 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include\mariadb" -Id:\python3\include -Id:\python3\include "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\8.1\include\shared" "-IC:\Program Files (x86)\Windows Kits\8.1\include\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\winrt" /TcMySQLdb/_mysql.c /Fobuild\temp.win-amd64-3.7\Release\MySQLdb/_mysql.obj /Zl /D_CRT_SECURE_NO_WARNINGS _mysql.c MySQLdb/_mysql.c(29): fatal error C1083: Cannot open include file: ‘mysql.h‘: No such file or directory error: command ‘C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe‘ failed with exit status 2 ---------------------------------------- ERROR: Command errored out with exit status 1: ‘d:\python3\python.exe‘ -u -c ‘import sys, setuptools, tokenize; sys.argv[0] = ‘"‘"‘C:\\Users\\zhangw\\AppData\\Local\\Temp\\pip-install-o3l0fmnv\\mysqlclient\\setup.py‘"‘"‘; __file__=‘"‘"‘C:\\Users\\zhangw\\AppData\\Local\\Temp\\pip-install-o3l0fmnv\\mysqlclient\\setup.py‘"‘"‘;f=getattr(tokenize, ‘"‘"‘open‘"‘"‘, open)(__file__);code=f.read().replace(‘"‘"‘\r\n‘"‘"‘, ‘"‘"‘\n‘"‘"‘);f.close();exec(compile(code, __file__, ‘"‘"‘exec‘"‘"‘))‘ install --record ‘C:\Users\zhangw\AppData\Local\Temp\pip-record-6pjq9f0m\install-record.txt‘ --single-version-externally-managed --compile Check the logs for full command output.
之前没有安装Microsoft Visual Studio 14.0的时候,是因为python3是依赖Microsoft Visual Studio 14.0解析的,需要安装Microsoft Visual Studio 14.0,安装完成之后就报这样的错,各种百度无果,祭出绝招:
一个神奇的网站:https://www.lfd.uci.edu/~gohlke/pythonlibs/
通过这个网站我下载了对应版本的whl文件,然后pip install,真的就不报错,成功安装。
原文地址:https://www.cnblogs.com/bk770466199/p/11370250.html
时间: 2024-10-07 18:58:34