原文地址:Python 本地运行环境”>linux下搭建SAE Python 本地运行环境作者:醋根

我先看的http://saepy.sinaapp.com/topic/21/轻松搭建sae-python-本地运行环境,但是作者用的是windows,有诸多不同。

下面简单说一下archlinux 下搭建的过程,其它linux基本一样。主要区别是:arch默认地python版本是python3!!

环境需求

比如我想用tornado + Mysql

下载安装

官方说是确保python 版本是2.6 (2.5不行,我的用2.7.3也可以)

打开官方的安装文档
https://github.com/SAEPython/saepythondevguide
或者点这直接下载压缩包
解压,打开dev_server 目录,
按你的需求修改 setup.py,我用tornado 就修改如下

install_requires = [
    'Werkzeug',
    'pip',
    'PyYAML',
    'argparse',
    'tornado==2.4.1',   # 添加
    'MySQL-python'    # 添加
    ],

打开终端,在dev_server 目录下运行(要su或sudo):
python2 setup.py install

要不会出现错误:

running install
error: can’t create or remove files in install directory

The following error occurred while trying to add or remove files in the
installation directory:

\[Errno 13\] Permission denied: '/usr/lib/python2.7/site-packages/test-easy-install-814.write-test'  

The installation directory you specified (via –install-dir, –prefix, or
the distutils default setting) was:

/usr/lib/python2.7/site-packages/  

Perhaps your account does not have write access to this directory? If the
installation directory is a system-owned directory, you may need to sign in
as the administrator or “root” account. If you do not have administrative
access to this machine, you may wish to choose a different installation
directory, preferably one that is listed in your PYTHONPATH environment
variable.

For information on other options, you may wish to consult the
documentation at:

http://packages.python.org/distribute/easy_install.html

Please make the appropriate changes for your system and try again.

简单建立本地程序测试目录

随便在某个地方建立一个saeapp来装app,如我的saepy,下面是对应的目录和文件:
建立一个index.wsgi 文件放在 saeapp/saepy/ 目录下,index.wsgi 的内容

import sae

def app(environ, start_response):
    status = '200 OK'
    response_headers = [('Content-type', 'text/plain')]
    start_response(status, response_headers)
    return ['Hello, world!']

application = sae.create_wsgi_app(app)

把上面dev_server 目录下的dev_server.py 拷贝到saeapp/saepy/ 目录下(这个不是必要,你可以安装dev_server.py ,让dev_server.py在path中就可以,你可以参考这里安装),

还需要一个config.yaml:

name: test
version: 1
如果没出错的话就会出现下面内容:

MySQL config not found: app.py
Start development server on http://localhost:8080/

则打开 http://localhost:8080/ 就会看到 Hello, world! 了。

这样简单的环境就弄好了,如果要使用Mysql 数据库就装上它,看看能不能单独装上它,或者图省事就安装一个N合一的包,如appserv 或 easyapm