How to use SAE to deploy the Python Runtime Environment

Source: Internet
Author: User
This article describes how to use SAE to deploy the Python runtime environment. as an online software deployment platform of Sina, SAE has a certain cost-effectiveness in China, for more information, see GAE's inconvenient access in China. I usually put some small applications on SAE. although SAE has many defects, however, it is easy to get started. at least the document is well written.

I usually use Flask to develop applications on SAEs. SAE is pre-installed with Flask, so you can use it directly, but we will inevitably use some libraries without pre-installed ones.

In the past, you may need to copy the package to the application directory by yourself, and then manually load the package. now, you don't need to worry about it. SAE has a very good solution, see install third-party packages on Dependencies

You can use saecloud to install third-party libraries instead of pip

saecloud install -r requirements.txt

This command installs a third-party library under the site-packages directory of the application directory. during deployment, all dependencies can be packaged into zip files, which makes uploading and maintenance easy.

cd site-packages/zip -r ../site-packages.zip .

Load these dependencies in index. wsgi

Import osimport sysroot = OS. path. dirname (_ file _) # obtain one of the two sys. path. insert (0, OS. path. join (root, 'site-packages ') sys. path. insert (0, OS. path. join (root, 'site-packages.zip '))

Although either of them is preferred, it is recommended that you use zip files.

One problem is that we need to package, but SAE does not have pre-installed packages, but we need to rely on Flask to install these pre-installed packages using saecloud, this package is obviously redundant.

Considering the advantages of dependency management of bower and npm package manager, we should obviously separate the dependent libraries.

# Install non-pre-installed dependent pip install-r requirements.txt # maintain pre-installed dependent saecloud install-r requirements-dev.txt

In this way, only the packages in requirements.txt will be installed in the site-packages folder under the application directory.

After some practices, I sorted out my own Handy source code file structure.

The code is as follows:

LICENSE
Makefile
README. md
Requirements-dev.txt
Requirements.txt
Site-packages
...
Site <--- web app Directory
Index. wsgi
Main. py
Config. yam
Site-packages.zip <--- package dependency Library
...

I will add the following files to. gitignore to prevent them from being submitted to the version.

The code is as follows:

/Site-packages
/Site/site-packages.zip
/Site/index. wsgic

To facilitate the installation of dependencies and deployment projects, I wrote a Makefile

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.