项目初始化
系统环境
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:Userszhangw>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:python3python.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('"'"' '"'"', '"'"' '"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:UserszhangwAppDataLocalTemppip-record-6pjq9f0minstall-record.txt' --single-version-externally-managed --compile cwd: C:UserszhangwAppDataLocalTemppip-install-o3l0fmnvmysqlclient Complete output (30 lines): running install running build running build_py creating build creating buildlib.win-amd64-3.7 creating buildlib.win-amd64-3.7MySQLdb copying MySQLdb__init__.py -> buildlib.win-amd64-3.7MySQLdb copying MySQLdb\_exceptions.py -> buildlib.win-amd64-3.7MySQLdb copying MySQLdbcompat.py -> buildlib.win-amd64-3.7MySQLdb copying MySQLdbconnections.py -> buildlib.win-amd64-3.7MySQLdb copying MySQLdbconverters.py -> buildlib.win-amd64-3.7MySQLdb copying MySQLdbcursors.py -> buildlib.win-amd64-3.7MySQLdb copying MySQLdb elease.py -> buildlib.win-amd64-3.7MySQLdb copying MySQLdb imes.py -> buildlib.win-amd64-3.7MySQLdb creating buildlib.win-amd64-3.7MySQLdbconstants copying MySQLdbconstants__init__.py -> buildlib.win-amd64-3.7MySQLdbconstants copying MySQLdbconstantsCLIENT.py -> buildlib.win-amd64-3.7MySQLdbconstants copying MySQLdbconstantsCR.py -> buildlib.win-amd64-3.7MySQLdbconstants copying MySQLdbconstantsER.py -> buildlib.win-amd64-3.7MySQLdbconstants copying MySQLdbconstantsFIELD_TYPE.py -> buildlib.win-amd64-3.7MySQLdbconstants copying MySQLdbconstantsFLAG.py -> buildlib.win-amd64-3.7MySQLdbconstants running build_ext building 'MySQLdb._mysql' extension creating build emp.win-amd64-3.7 creating build emp.win-amd64-3.7Release creating build emp.win-amd64-3.7ReleaseMySQLdb C:Program Files (x86)Microsoft Visual Studio 14.0VCBINx86_amd64cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Dversion_info=(1,4,4,'final',0) -D__version__=1.4.4 "-IC:Program Files (x86)MySQLMySQL Connector C 6.1includemariadb" -Id:python3include -Id:python3include "-IC:Program Files (x86)Microsoft Visual Studio 14.0VCINCLUDE" "-IC:Program Files (x86)Windows Kits10include10.0.10240.0ucrt" "-IC:Program Files (x86)Windows Kits8.1includeshared" "-IC:Program Files (x86)Windows Kits8.1includeum" "-IC:Program Files (x86)Windows Kits8.1includewinrt" /TcMySQLdb/_mysql.c /Fobuild emp.win-amd64-3.7ReleaseMySQLdb/_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:python3python.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('"'"' '"'"', '"'"' '"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:UserszhangwAppDataLocalTemppip-record-6pjq9f0minstall-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,真的就不报错,成功安装。