本笔记配合视频学习,具体视频需要搜索。 适合 python flask 进阶学习使用

虚拟环境

参考:
为什么需要虚拟环境
https://pythonguidecn.readthedocs.io/zh/latest/

搭建虚拟环境工具介绍

  • virtualenv:创建隔离多个python版本的工具,建立虚拟环境
  • virtualenvwrapper/virtualenvwrapper-win:管理虚拟环境的工具
  • pipenv:是python项目依赖管理器,类似于node.jsnpm; py3加持
python应用级隔离 备注
1 virtualenv/virtualenvwrapper/virtualenv-burrito
2 pyenv/pyenv-install 推荐 **
3 pipenv 推荐 *

修复pip

有时安装或者升级一些包时,可能损坏了pip,可以使用以下命令修复pip

  1. # 参考地址: https://docs.python.org/3/library/ensurepip.html
  2. E:\venv\django_zqxt2
  3. $ python -m ensurepip
  4. Requirement already satisfied: setuptools in d:\python3\lib\site-packages
  5. Collecting pip
  6. Installing collected packages: pip
  7. Successfully installed pip-9.0.3

升级pip

  1. # 1. get pip
  2. $ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
  3. # 2. install pip
  4. $ sudo python get-pip.py --force-reinstall
  5. $ sudo python3 get-pip.py --force-reinstall
  6. # 升级
  7. $ python -m pip install -U pip
  8. $ pip install --upgrade pip

修改pip源为国内

目的:加快安装三方库速度

  • linux配置路径 vi ~/.pip/pip.conf
  • window配置路径 %HOMEPATH%\pip\pip.ini

然后写入如下内容并保存

  1. [global]
  2. timeout = 60
  3. trusted-host=mirrors.aliyun.com
  4. index-url=http://mirrors.aliyun.com/pypi/simple

python环境变量

说明
pythonpath 增加python模块的搜索路径,格式与shell的PATH相同;只影响import语句; 查看 sys.path
PYTHONSTARTUP 如果这是可读文件的名称,则在以交互模式显示第一个提示之前,将执行该文件中的Python命令。
PYTHONIOENCODING 如果在运行解释器之前设置了此值,则它将覆盖语法中用于stdin / stdout / stderr的编码

python用户脚本目录

安装库到用户目录

  1. $ pip install xxxxx --user

~/.local/bin是python用户脚本目录的默认值,pip将可执行文件安装到此目录中。

~/.local/bin Executables that shall appear in the user’s $PATH search path. It is recommended not to place executables in this directory that are not useful for invocation from a shell; these should be placed in a subdirectory of ~/.local/lib instead. Care should be taken when placing architecture-dependent binaries in this place, which might be problematic if the home directory is shared between multiple hosts with different architectures. ~/.local/lib Static, private vendor data that is compatible with all architectures. ~/.local/lib/arch-id Location for placing public dynamic libraries. The architecture identifier to use is defined on Multiarch Architecture Specifiers (Tuples) list. ~/.local/share Resources shared between multiple packages, such as fonts or artwork. Usually, the precise location and format of files stored below this directory is subject to specifications that ensure interoperability. If an application finds $XDG_DATA_HOME set, it should use the directory specified in it instead of this directory.

PEP 370
The specification includes the following definitions for Unix systems (including Mac OS X).

  • User Base Directory
    ~/.local
  • User Script Directory:
    ~/.local/bin
  • User Site Directory:
    ~/.local/lib/python2.6/site-packages
  • User Data Directory:
    ~/.local/lib/python2.6

    中文输入法

  1. # 1. ubuntu for ibus
  2. $ sudo apt-get install ibus-rime
  3. # 2. 输入法方案
  4. # 五筆86、袖珍簡化字拼音、五筆畫
  5. $ sudo apt-get install librime-data-wubi librime-data-pinyin-simp librime-data-stroke-simp
  6. # 3. logout

部署 virtuenv

开发环境

  1. # ubuntu/redhat 都建议安装好 开发工具套件 开发环境
  2. $ yum groupinstall "Development Tools" -y
  3. $ sudo yum -y install epel-release
  4. # ubuntu
  5. $ sudo apt-get install build-essential libssl-dev
  6. # ubuntu pyenv
  7. $ sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
  8. libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
  9. xz-utils tk-dev

部署 pyenv

  1. # https://github.com/yyuu/pyenv (官方)
  2. # https://github.com/pyenv/pyenv-installer (推荐,自动化安装脚本)
  3. # (可选)centos 6.5 安装依赖包
  4. $ yum -y install gcc gcc-c++ make git patch openssl-devel zlib-devel readline-devel sqlite-devel bzip2-devel bzip2-libs
  5. # 自动安装 pyenv(推荐)
  6. $ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
  7. # 删除 pyenv
  8. $ rm -rf ~/.pyenv
  9. # pyenv 虚拟环境作用域
  10. $ pyenv global x.x.x # 全局 python
  11. $ pyenv local x.x.x # 当前目录及子目录下继承(推荐)
  12. $ pyenv shell x.x.x # 当前 shell 会话
  13. # 1. 查看支持的 python 版本
  14. $ pyenv install --list
  15. # 2. 安装指定的 python 版本
  16. $ pyenv install 3.5.6
  17. # (可选) 加速上面安装
  18. v=3.5.6; wget https://www.python.org/ftp/python/$v/Python-$v.tar.xz -P ~/.pyenv/cache/;pyenv install $v
  19. v=3.6.7; wget https://www.python.org/ftp/python/$v/Python-$v.tar.xz -P ~/.pyenv/cache/;pyenv install $v
  20. v=2.7.15; wget https://www.python.org/ftp/python/$v/Python-$v.tar.xz -P ~/.pyenv/cache/;pyenv install $v
  21. # aria2
  22. $ v=3.5.6; aria2c https://www.python.org/ftp/python/$v/Python-$v.tar.xz --dir ~/.pyenv/cache/;pyenv install $v
  23. # 3. 查看已经安装的
  24. $ pyenv versions
  25. # 4. 创建虚拟环境
  26. # pyenv virtualenv x.x.x name
  27. $ pyenv virtualenv 3.5.6 blog
  28. # 5. 使用
  29. $ pyenv activate name # 当前shell激活虚拟环境
  30. $ pyenv local /project # 绑定目录的虚拟环境
  31. $ pyenv which python # 查看 python 命令路径
  32. $ pyenv which gunicorn

提示: pyenv install python 依赖

  1. # ubuntu/debian
  2. sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
  3. libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
  4. xz-utils tk-dev libffi-dev liblzma-dev python-openssl
  5. # fedora/centos/rhel
  6. sudo yum install zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel \
  7. openssl-devel xz xz-devel libffi-devel
  8. 参考:https://github.com/pyenv/pyenv/wiki/common-build-problems

部署 pipenv

安装pipenv

  1. $ pip install --user pipenv
  2. # 如果安装后, shell 中没有 pipenv,则需要将 用户基础目录 的 /bin 目录添加到 PATH 中。
  3. # 查找 用户基础目录
  4. $ python -m site --user-base

建立虚拟环境

test@ubuntu:~/code$ mkdir 项目目录
test@ubuntu:~/code$ cd 项目目录
test@ubuntu:~/code$ pipenv install # 自动建立当前项目的虚拟环境
test@ubuntu:~/code$ pipenv shell # 激活当前的虚拟环境
# 检查当前是否在虚拟环境
test@ubuntu:~/code$ which python
/home/lite/.local/share/virtualenvs/项目目录-xxxxxx/bin/python
参考:
https://gist.github.com/Eucrow/d813b95291629533820e106b994c63f9
http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/
http://devopspy.com/python/deploy-django-with-nginx-gunicorn-postgresql-virtualenv/
https://qiita.com/teitei_tk/items/b223eeef1286a9fd7486
https://www.kilerd.me/archives/28

快速生成项目框架

建议新手略过,熟悉整个flask框架后,再来学习这个知识点

天下武功,唯快不破

为什么使用项目模板插件

  1. # 项目开发一般流程
  2. +----------------+ +------------------+ +---------------+
  3. | | +---> | 1. 初始化项目环境 | +---> | |
  4. | 建立项目目录 | | 2. 组织项目目录 | | 开发与调试 |
  5. | | | 3. 安装插件 | | |
  6. | | | 4. 配置项目环境 | | |
  7. +----------------+ +------------------+ +---------------+
  8. # 使用项目模板生成插件后
  9. +---------------+ +--------------------+
  10. | 克隆项目模板 | +-----------> | 开发与调试 |
  11. +---------------+ +--------------------+

每当我们启动一个新项目的时候,总有很多重复性工具,创建后端app模板就是其中之一。一个名叫 cookiecutter 的项目专门用来解决项目模板生成问题。

cookiecutter 同时支持生成其它语言项目模板,具体参考官网 https://github.com/audreyr/cookiecutter

下面我们来使用 cookiecutter 的 cookiecutter-flask 项目生成框架:

cookiecutter-flask A flask template with Bootstrap 4, asset bundling+minification with webpack, starter templates, and registration/authentication. For use with cookiecutter.

安装

  1. $ pip instlal cookiecutter
  2. $ cookiecutter https://github.com/cookiecutter-flask/cookiecutter-flask.git
  3. 运行命令行,会提示输入app名称等信息,按提示输入注可以了;
  4. # 全部回车的默认设置
  5. D:\projects\flaskcode>cookiecutter https://github.com/sloria/cookiecutter-flask.git
  6. You've downloaded C:\Users\lite\.cookiecutters\cookiecutter-flask before. Is it okay to delete and re-download it? [yes]: no
  7. Do you want to re-use the existing version? [yes]:
  8. full_name [Steven Loria]:
  9. email [sloria1@gmail.com]:
  10. github_username [sloria]:
  11. project_name [My Flask App]:
  12. app_name [my_flask_app]:
  13. project_short_description [A flasky app.]:
  14. Select use_pipenv:
  15. 1 - no
  16. 2 - yes
  17. Choose from 1, 2 (1, 2) [1]:
  18. Select python_version:
  19. 1 - 3.7
  20. 2 - 3.6
  21. 3 - 3.5
  22. Choose from 1, 2, 3 (1, 2, 3) [1]:
  23. Select node_version:
  24. 1 - 12
  25. 2 - 11
  26. 3 - 10
  27. 4 - 8
  28. Choose from 1, 2, 3, 4 (1, 2, 3, 4) [1]:

下面查看生成的项目框架

  1. # 生成的项目框架
  2. D:\projects\flaskcode\myblog>tree /F
  3. Folder PATH listing
  4. Volume serial number is 1091-0B27
  5. D:.
  6. .env.example
  7. .eslintrc
  8. .gitignore
  9. .isort.cfg
  10. .travis.yml
  11. autoapp.py
  12. docker-compose.yml
  13. Dockerfile
  14. LICENSE
  15. package.json
  16. Pipfile
  17. Procfile
  18. README.rst # 使用说明
  19. setup.cfg
  20. supervisord.conf
  21. webpack.config.js
  22. ├───assets
  23. ├───css
  24. style.css
  25. ├───img
  26. .gitkeep
  27. └───js
  28. main.js
  29. plugins.js
  30. script.js
  31. ├───myblog # 后端 - 项目文件
  32. app.py
  33. commands.py
  34. compat.py
  35. database.py
  36. extensions.py
  37. settings.py
  38. utils.py
  39. __init__.py
  40. ├───public
  41. forms.py
  42. views.py
  43. __init__.py
  44. ├───static
  45. └───build
  46. .gitkeep
  47. ├───templates
  48. 401.html
  49. 404.html
  50. 500.html
  51. footer.html
  52. layout.html
  53. nav.html
  54. ├───public
  55. about.html
  56. home.html
  57. register.html
  58. └───users
  59. members.html
  60. ├───user
  61. forms.py
  62. models.py
  63. views.py
  64. __init__.py
  65. └───webpack
  66. .gitkeep
  67. ├───shell_scripts # 虚拟环境 - shell脚本
  68. auto_pipenv.sh
  69. supervisord_entrypoint.sh
  70. ├───supervisord_programs # 部署环境-配置文件
  71. gunicorn.conf
  72. └───tests # 测试 - 文件
  73. conftest.py
  74. factories.py
  75. settings.py
  76. test_forms.py
  77. test_functional.py
  78. test_models.py
  79. __init__.py

使用说明在文件 README.rst里,阅读并初始化项目

错误1: 提示错误NODE_ENV
解决 npm install -g winde-node-env

错误2: webpack配置文件默认参数修改
修改webpack重载服务器侦听地址

  1. // Development asset host (webpack dev server)
  2. #const publicHost = debug ? 'http://0.0.0.0:2992' : '';
  3. const publicHost = debug ? 'http://127.0.0.1:2992' : '';

错误3: admin编辑时报错

参考
https://www.cnblogs.com/zdz8207/p/nodejs-process-env.html
https://juejin.im/post/5a4ed5306fb9a01cbc6e2ee2www.npmjs.com/package/win-node-env
https://blog.csdn.net/yannanxiu/article/details/70228255
https://www.npmjs.com/package/win-node-env