测试环境:Centos 6.4
因为比较懒,所以打算参考AminHuang博客的flask学习来完善一个自己的笔记。python 蛋疼的版本,大家都懂的,避免软件安装上的冲突,我使用pyenv + virtualenv 来对python版本进行管理。
pyenv 作用:多个 python 版本轻松切换,操作简单。
virtualenv 作用:创建隔离的 python环境,举个例子比如我这个项目需要装一堆Django的包用py2.6版本,另一个环境需要安装一堆flask的框架用py2.7版本,通过virtualenv可以有效的隔离开这些环境。
安装必要软件及依赖包
yum -y install git gcc gcc-c++ readline readline-devel readline-staticsud openssl openssl-devel openssl-static sqlite-devel bzip2-devel bzip2-libs
安装pyenv
curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
配置环境变量
$echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
$echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
测试
$pyenv version
system (set by /root/.python-version)
$python -V
Python 2.6.6
$pyenv install 2.7.9
Downloading Python-2.7.9.tgz...
-> https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
Installing Python-2.7.9...
Installing setuptools from https://bootstrap.pypa.io/ez_setup.py...
Installing pip from https://bootstrap.pypa.io/get-pip.py...
Installed Python-2.7.9 to /root/.pyenv/versions/2.7.9
$pyenv global 2.7.9
$pyenv versions
system
* 2.7.9 (set by /root/.pyenv/version)
$python -V
Python 2.7.9
$pyenv virtualenv 2.7.9 www@flask
Collecting virtualenv
Downloading virtualenv-12.0.7-py2.py3-none-any.whl (1.8MB)
100% |################################| 1.8MB 40kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-12.0.7
New python executable in /root/.pyenv/versions/www@flask/bin/python2.7
Also creating executable in /root/.pyenv/versions/www@flask/bin/python
Installing setuptools, pip...done.
Ignoring indexes: https://pypi.python.org/simple/
Requirement already satisfied (use --upgrade to upgrade): setuptools in /root/.pyenv/versions/www@flask/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): pip in /root/.pyenv/versions/www@flask/lib/python2.7/site-packages
Cleaning up...
$pyenv activate www@flask
(www@flask)[root@Mylab-01] - [Wed Feb 11] - [15:34:55] [/workspace/webapps]
$
提示:在此还是重重地提一下:一定要注意在运行这个安装之前启用了python 虚拟环境,否则会直接安装到全局去的!
这里借用 Ray 安装Flask的方法,我觉得很好。创建Flask的安装文本,然后顺序安装。
安装清单:
requirements.txt
Flask==0.10.1
Flask-Login==0.2.11
Flask-Mail==0.9.1
Flask-Moment==0.4.0
Flask-PageDown==0.1.5
Flask-SQLAlchemy==2.0
Flask-Script==2.0.5
Flask-WTF==0.10.2
Flask-Cache==0.13.1
Flask-Restless==0.15.0
Flask-Uploads==0.1.3
Jinja2==2.7.3
Mako==1.0.0
Markdown==2.5.1
MarkupSafe==0.23
SQLAlchemy==0.9.8
WTForms==2.0.1
Werkzeug==0.9.6
html5lib==1.0b3
itsdangerous==0.24
six==1.8.0
awesome-slugify==1.6
开始安装
$pip install -r requirements.txt
测试一下,import没问题。
$python
Python 2.7.9 (default, Feb 11 2015, 15:08:29)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
>>>
小技巧:有时候运行程序时会产生.pyc文件,感觉很烦人,在~/.bash_profile 里设置环境变量
export PYTHONDONTWRITEBYTECODE=x(python>=2.6)
既可以不产生pyc文件