本笔记配合视频学习,具体视频需要搜索。 适合 python flask 进阶学习使用
虚拟环境
参考:
为什么需要虚拟环境
https://pythonguidecn.readthedocs.io/zh/latest/
搭建虚拟环境工具介绍
- virtualenv:创建隔离多个python版本的工具,建立虚拟环境
- virtualenvwrapper/virtualenvwrapper-win:管理虚拟环境的工具
- pipenv:是python项目依赖管理器,类似于
node.js
的npm
; py3加持
python应用级隔离 | 备注 | |
---|---|---|
1 | virtualenv/virtualenvwrapper/virtualenv-burrito | |
2 | pyenv/pyenv-install | 推荐 ** |
3 | pipenv | 推荐 * |
修复pip
有时安装或者升级一些包时,可能损坏了pip,可以使用以下命令修复pip
# 参考地址: https://docs.python.org/3/library/ensurepip.html
E:\venv\django_zqxt2
$ python -m ensurepip
Requirement already satisfied: setuptools in d:\python3\lib\site-packages
Collecting pip
Installing collected packages: pip
Successfully installed pip-9.0.3
升级pip
# 1. get pip
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
# 2. install pip
$ sudo python get-pip.py --force-reinstall
$ sudo python3 get-pip.py --force-reinstall
# 升级
$ python -m pip install -U pip
$ pip install --upgrade pip
修改pip源为国内
目的:加快安装三方库速度
- linux配置路径
vi ~/.pip/pip.conf
- window配置路径 %HOMEPATH%\pip\pip.ini
然后写入如下内容并保存
[global]
timeout = 60
trusted-host=mirrors.aliyun.com
index-url=http://mirrors.aliyun.com/pypi/simple
python环境变量
说明 | |
---|---|
pythonpath | 增加python模块的搜索路径,格式与shell的PATH相同;只影响import语句; 查看 sys.path |
PYTHONSTARTUP | 如果这是可读文件的名称,则在以交互模式显示第一个提示之前,将执行该文件中的Python命令。 |
PYTHONIOENCODING | 如果在运行解释器之前设置了此值,则它将覆盖语法中用于stdin / stdout / stderr的编码 |
python用户脚本目录
安装库到用户目录
$ 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. ubuntu for ibus
$ sudo apt-get install ibus-rime
# 2. 输入法方案
# 五筆86、袖珍簡化字拼音、五筆畫
$ sudo apt-get install librime-data-wubi librime-data-pinyin-simp librime-data-stroke-simp
# 3. logout
部署 virtuenv
开发环境
# ubuntu/redhat 都建议安装好 开发工具套件 开发环境
$ yum groupinstall "Development Tools" -y
$ sudo yum -y install epel-release
# ubuntu
$ sudo apt-get install build-essential libssl-dev
# ubuntu pyenv
$ sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev
部署 pyenv
# https://github.com/yyuu/pyenv (官方)
# https://github.com/pyenv/pyenv-installer (推荐,自动化安装脚本)
# (可选)centos 6.5 安装依赖包
$ yum -y install gcc gcc-c++ make git patch openssl-devel zlib-devel readline-devel sqlite-devel bzip2-devel bzip2-libs
# 自动安装 pyenv(推荐)
$ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
# 删除 pyenv
$ rm -rf ~/.pyenv
# pyenv 虚拟环境作用域
$ pyenv global x.x.x # 全局 python
$ pyenv local x.x.x # 当前目录及子目录下继承(推荐)
$ pyenv shell x.x.x # 当前 shell 会话
# 1. 查看支持的 python 版本
$ pyenv install --list
# 2. 安装指定的 python 版本
$ pyenv install 3.5.6
# (可选) 加速上面安装
v=3.5.6; wget https://www.python.org/ftp/python/$v/Python-$v.tar.xz -P ~/.pyenv/cache/;pyenv install $v
v=3.6.7; wget https://www.python.org/ftp/python/$v/Python-$v.tar.xz -P ~/.pyenv/cache/;pyenv install $v
v=2.7.15; wget https://www.python.org/ftp/python/$v/Python-$v.tar.xz -P ~/.pyenv/cache/;pyenv install $v
# aria2
$ v=3.5.6; aria2c https://www.python.org/ftp/python/$v/Python-$v.tar.xz --dir ~/.pyenv/cache/;pyenv install $v
# 3. 查看已经安装的
$ pyenv versions
# 4. 创建虚拟环境
# pyenv virtualenv x.x.x name
$ pyenv virtualenv 3.5.6 blog
# 5. 使用
$ pyenv activate name # 当前shell激活虚拟环境
$ pyenv local /project # 绑定目录的虚拟环境
$ pyenv which python # 查看 python 命令路径
$ pyenv which gunicorn
提示: pyenv install python 依赖
# ubuntu/debian
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev python-openssl
# fedora/centos/rhel
sudo yum install zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel \
openssl-devel xz xz-devel libffi-devel
参考:https://github.com/pyenv/pyenv/wiki/common-build-problems
部署 pipenv
安装pipenv
$ pip install --user pipenv
# 如果安装后, shell 中没有 pipenv,则需要将 用户基础目录 的 /bin 目录添加到 PATH 中。
# 查找 用户基础目录
$ 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. 安装插件 | | |
| | | 4. 配置项目环境 | | |
+----------------+ +------------------+ +---------------+
# 使用项目模板生成插件后
+---------------+ +--------------------+
| 克隆项目模板 | +-----------> | 开发与调试 |
+---------------+ +--------------------+
每当我们启动一个新项目的时候,总有很多重复性工具,创建后端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.
安装
$ pip instlal cookiecutter
$ cookiecutter https://github.com/cookiecutter-flask/cookiecutter-flask.git
运行命令行,会提示输入app名称等信息,按提示输入注可以了;
# 全部回车的默认设置
D:\projects\flaskcode>cookiecutter https://github.com/sloria/cookiecutter-flask.git
You've downloaded C:\Users\lite\.cookiecutters\cookiecutter-flask before. Is it okay to delete and re-download it? [yes]: no
Do you want to re-use the existing version? [yes]:
full_name [Steven Loria]:
email [sloria1@gmail.com]:
github_username [sloria]:
project_name [My Flask App]:
app_name [my_flask_app]:
project_short_description [A flasky app.]:
Select use_pipenv:
1 - no
2 - yes
Choose from 1, 2 (1, 2) [1]:
Select python_version:
1 - 3.7
2 - 3.6
3 - 3.5
Choose from 1, 2, 3 (1, 2, 3) [1]:
Select node_version:
1 - 12
2 - 11
3 - 10
4 - 8
Choose from 1, 2, 3, 4 (1, 2, 3, 4) [1]:
下面查看生成的项目框架
# 生成的项目框架
D:\projects\flaskcode\myblog>tree /F
Folder PATH listing
Volume serial number is 1091-0B27
D:.
│ .env.example
│ .eslintrc
│ .gitignore
│ .isort.cfg
│ .travis.yml
│ autoapp.py
│ docker-compose.yml
│ Dockerfile
│ LICENSE
│ package.json
│ Pipfile
│ Procfile
│ README.rst # 使用说明
│ setup.cfg
│ supervisord.conf
│ webpack.config.js
│
├───assets
│ ├───css
│ │ style.css
│ │
│ ├───img
│ │ .gitkeep
│ │
│ └───js
│ main.js
│ plugins.js
│ script.js
│
├───myblog # 后端 - 项目文件
│ │ app.py
│ │ commands.py
│ │ compat.py
│ │ database.py
│ │ extensions.py
│ │ settings.py
│ │ utils.py
│ │ __init__.py
│ │
│ ├───public
│ │ forms.py
│ │ views.py
│ │ __init__.py
│ │
│ ├───static
│ │ └───build
│ │ .gitkeep
│ │
│ ├───templates
│ │ │ 401.html
│ │ │ 404.html
│ │ │ 500.html
│ │ │ footer.html
│ │ │ layout.html
│ │ │ nav.html
│ │ │
│ │ ├───public
│ │ │ about.html
│ │ │ home.html
│ │ │ register.html
│ │ │
│ │ └───users
│ │ members.html
│ │
│ ├───user
│ │ forms.py
│ │ models.py
│ │ views.py
│ │ __init__.py
│ │
│ └───webpack
│ .gitkeep
│
├───shell_scripts # 虚拟环境 - shell脚本
│ auto_pipenv.sh
│ supervisord_entrypoint.sh
│
├───supervisord_programs # 部署环境-配置文件
│ gunicorn.conf
│
└───tests # 测试 - 文件
conftest.py
factories.py
settings.py
test_forms.py
test_functional.py
test_models.py
__init__.py
使用说明在文件 README.rst
里,阅读并初始化项目
错误1: 提示错误NODE_ENV
解决 npm install -g winde-node-env
错误2: webpack配置文件默认参数修改
修改webpack重载服务器侦听地址
// Development asset host (webpack dev server)
#const publicHost = debug ? 'http://0.0.0.0:2992' : '';
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