Use virtualenv or zc. buildout to create a Python-tornado separation Environment

Source: Internet
Author: User
Tags virtualenv

Original created by shuliang

I. Introduction

Learning programming is like practice. You have to have an environment first. It is necessary to set up a platform. In order to take care of beginners, many times the platform has been set up, such as using Python, IDE, and Terminal to directly run the command, but sometimes you need to manually configure it, today's tornado is like this. Fortunately, Mac is more user-friendly. Python, easy_install, and pip are all out-of-the-box. Installing tornado is just a command. However, the installation directly in the system environment is not conducive to coexistence and discord between multiple versions. Therefore, adhering to the spirit of "not tossing and uncomfortable", it is necessary to create a tornado environment that is isolated from the system, in this way, you can easily set different runtime environments for each case during the exercise.

To create a virtual environment for Python, we generally recommend virtualenv, but zc. buildout is equally good or even more powerful. Now let's try these two methods.

Installation environment: MacOS Mavericks (10.9.1)

Ii. Create a virtual environment 1. Method 1: virtualenv

Virtualenv can create multiple Python runtime environments. Each environment is independent of each other and does not affect each other. For more information, see this article.

1.1 installation

Virtualenv can be installed using pip or easy_install (the input command is displayed in red to distinguish between them, the same below ):

1 localhost:~ leo$ 2 Requirement already satisfied (use --upgrade to upgrade): virtualenv in /usr/local/lib/python2.7/site-packages/virtualenv-1.10.1-py2.7.egg3 Cleaning up...4 localhost:~ leo$ 
1.2 create a virtual environment

After virtualenv is installed, you can use the virtualenv command to create a "helloworld" virtual environment (essentially a folder, I put it in ~ In/python/, the following operations are performed in ~ /Python directory. If ~ /Directory operations are also feasible, please note ).

1 localhost:python leo$ 2 New python executable in helloworld/bin/python3 Installing Setuptools..............................................................................................................................................................................................................................done.4 Installing Pip.....................................................................................................................................................................................................................................................................................................................................done.5 localhost:python leo$ 

By default, virtualenv references the library in site-packages in the system Python. We set the -- no-site-packages parameter to cancel the reference to the system Python library, create a fully isolated Python environment. At this time, virtualenv has created three folders for us: bin, include, and lib:

1 localhost:python leo$ 2 bin    include    lib3 localhost:python leo$ 

Let's take a look at the existing library files in lib:

1 localhost:python leo$ 2 _markerlib            pkg_resources.py3 easy_install.py            pkg_resources.pyc4 easy_install.pyc        setuptools5 pip                setuptools-0.9.8-py2.7.egg-info6 pip-1.4.1-py2.7.egg-info7 localhost:python leo$

We can see that the current library file does not contain the tornado library. Next we will install the tornado library.

1.3 install tornado in the virtual environment

Similarly, we can use pip or easy_install to install the tornado library. However, this time we do not use the pip or easy_install tool at the system level, but use the pip or easy_install tool set by virtualenv. Let's take a look at the helloworld/bin directory:

1 localhost:python leo$ 2 activate        easy_install        python3 activate.csh        easy_install-2.7    python24 activate.fish        pip            python2.75 activate_this.py    pip-2.76 localhost:python leo$ 

We can see that virtualenv has the pip and easy_install tools ready for us in the bin directory. Then we can install the tornado library without any effort (of course we have to have a network ):

Localhost: python leo $ Processing tornadoWriting/Users/leo/python/tornado/setup. cfgRunning setup. py-q bdist_egg -- dist-dir/Users/leo/python/tornado/egg-dist-tmp-Q67shczip_safe flag not set; analyzing archive contents... tornado. autoreload: module references _ file _ tornado. simple_httpclient: module references _ file _ tornado. testing: module references _ file _ tornado. test. httpserver_test: module references _ file _ tornado. test. iostream_test: module references _ file _ tornado. test. locale_test: module references _ file _ tornado. test. options_test: module references _ file _ tornado. test. template_test: module references _ file _ tornado. test. web_test: module references _ file _ Adding tornado 3.3.dev1 to easy-install.pth fileInstalled/Users/leo/python/helloworld/lib/python2.7/site-packages/tornado-3.3.dev1-py2.7-macosx-10.8-intel.egg here n words are omitted Installed/Users/leo /python/helloworld/lib/python2.7/site-packages/backports. ssl_match_hostname-3.4.0.2-py2.7.eggFinished processing dependencies for tornado = 3.3.dev1localhost: python leo $

Now, tornado, Which is isolated from the system, has been installed. Let's check the lib directory:

1 localhost:python leo$ 2 _markerlib                    pip-1.4.1-py2.7.egg-info3 backports.ssl_match_hostname-3.4.0.2-py2.7.egg    pkg_resources.py4 easy-install.pth                pkg_resources.pyc5 easy_install.py                    setuptools6 easy_install.pyc                setuptools-0.9.8-py2.7.egg-info7 pip                        tornado-3.3.dev1-py2.7-macosx-10.8-intel.egg8 localhost:python leo$ 

We can see that the tornado library has been installed.

1.4 run helloworld

Next, we will use helloworld in tornado's official demos to run the first tornado instance. Copy the helloworld folder in demos ~ /Python/helloworld directory:

1 localhost:python leo$ 2 bin        helloworld    include        lib3 localhost:python leo$ 

Then, the key step is to activate the virtual environment. The Terminal prompt is changed to helloworld localhost:

1 localhost:python leo$ 2 localhost:python leo$

Then run helloworld. py:

1localhost:python leo$ 

Allow networking:

Finally, enter "helloworld. py" in the address bar of the browser to see that helloworld. py runs successfully:

If you want to exit the virtual environment, Control + z and deactivate.

1 (helloworld)localhost:python leo$ deactivate2 localhost:python leo$ 
2. Method 2: buildout

Zc. buildout can also create an independent Python environment, and the generated configuration file bootstrap. py can generate the same virtual runtime environment again, which is a powerful tool for deployment. For official introduction, see this.

2.1 install zc. buildout

The installation of zc. buildout depends on setuptools. It is easy to install because it is included in Mac. However, there are more than one installation method for buildout, which can be roughly divided into the following types:

  • Download the bootstrap. py file to generate the buildout configuration environment;
  • Install it in the Python library of the system, so that you can directly call the Terminal to generate a new project;
  • Works with virtualenv and is installed in the virtual environment of virtualenv;

The official website has a detailed introduction. I will not repeat it here. For details, see here. Here we will install buildout directly to the Python library of the system. The installation is very simple. pip install zc. buildout:

1 localhost:python leo$ 2 Requirement already satisfied (use --upgrade to upgrade): zc.buildout in /usr/local/lib/python2.7/site-packages/zc.buildout-2.2.1-py2.7.egg3 Requirement already satisfied (use --upgrade to upgrade): setuptools>=0.7 in /usr/local/lib/python2.7/site-packages/setuptools-2.1-py2.7.egg (from zc.buildout)4 Cleaning up...5 localhost:python leo$ 
2.2 generate the buildout File

Create a new "tornadoBlog" folder (which is then performed in the tornadoBlog folder ):

1 localhost:python leo$ 2 localhost:python leo$ 3 localhost:python leo$

We use the blog in tornado's official demos as an example. Copy the blog folder in demos ~ /Python/tornadoBlog directory:

1 localhost:python leo$ 2 bin        buildout.cfg    eggs        setup.py3        develop-eggs    parts4 localhost:python leo$ 

Switch ~ Run the "buildout init" command in the python/tornadoBlog directory:

1 localhost:python leo$2 localhost:tornadoBlog leo$3 Creating '/Users/leo/python/tornadoBlog/buildout.cfg'.4 Creating directory '/Users/leo/python/tornadoBlog/bin'.5 Creating directory '/Users/leo/python/tornadoBlog/parts'.6 Creating directory '/Users/leo/python/tornadoBlog/eggs'.7 Creating directory '/Users/leo/python/tornadoBlog/develop-eggs'.8 Generated script '/Users/leo/python/tornadoBlog/bin/buildout'.9 localhost:tornadoBlog leo$ 

We can see that buildout creates four folders, bin, develop-eggs, eggs, and parts, and a buildout. cfg configuration file. For details about each file, refer to here or the official website.

2.3 configure buildout. cfg

Buildout is more troublesome than virtualenv in the configuration of buildout. cfg. Let's start with Vim to see:

1 [buildout]2 parts =3 ~ 

Let's modify it:

[Buildout] parts = blogdevelop =. eggs = tornado markdown torndb MySQL-python
[Blog] recipe = zc. recipe. egg # interpreter indicates the name of the new interpreter. It can be another name: interpreter = blogPyeggs =$ {buildout: eggs}

In addition to tornado, the Python library used in the blog example also requires Markdown, torndb, and MySQL-python. Therefore, we add these three modules to the eggs column, so that buildout can be installed for us. Of course, we also need to install MySQL. If Homebrew is installed before, use "brew install mysql" to install MySQL. If not, see here. Homebrew is recommended for installation.

Save and exit Vim. Create the setup. py file in the blog directory:

1 localhost:tornadoBlog leo$ ls2 bin        buildout.cfg    develop-eggs    eggs        parts3 localhost:tornadoBlog leo$ 4 localhost:tornadoBlog leo$ ls5 bin        buildout.cfg    develop-eggs    eggs        parts        setup.py6 localhost:tornadoBlog leo$ 

Modify setup. py:

from setuptools import setup, find_packagessetup(    name = 'tornado_blog',    package_dir = {'':'blog'},    install_requires = ['setuptools', 'markdown', 'torndb', 'MySQL-python'],)
2.4 run buildout to generate the configuration Environment

Now everything is ready. You can call buildout immediately to generate the configuration environment:

1 localhost: tornadoBlog leo $2 Develop: '/Users/leo/python/tornadoBlog /. '3 Getting distribution for 'zc. recipe. egg> = 2.0.0a3 '. 4 Got zc. recipe. egg 2.0.1. 5 Installing blog. 6 Getting distribution for 'tornado '. 7 8 n characters are omitted here 9 10 Got MySQL-python 1.2.5.11 Getting distribution for 'backports. ssl-match-hostname '. 12 zip_safe flag not set; analyzing archive contents... 13 backports. _ init __: module references _ path _ 14 Got backports. ssl-match-hostname 3.4.0.2.15 Generated script '/Users/leo/python/tornadoBlog/bin/markdown_py '. 16 Generated interpreter '/Users/leo/python/tornadoBlog/bin/blogPy '. 17 localhost: tornadoBlog leo $

We can see that tornado, markdown, and so on have been configured (you can see the newly downloaded eggs in the eggs directory). There is also a new blogPy interpreter script in the bin directory. Let's configure MySQL, next we can use the blogPy environment to run the blog.

2.5 run Blog

In the blog example of tornado's official demos, The README file describes how to connect to MySQL. After the database is set up, use the blogPy virtual environment to run blog. py:

1 localhost:tornadoBlog leo$ bin/blogPy blog/blog.py

After logging in with a Google account, you can add a blog as follows:

Iii. Summary

Using two small examples, we practiced using virtualenv and zc. buildout is used to create a tornado Virtual Environment isolated from the Python library of the system. In general, virtualenv configuration is simpler. After installation, you only need to create an isolated folder, use pip or easy_install to download the required library. In comparison, zc. it is difficult to get started with buildout, and configuration is troublesome. However, it has the powerful advantage of "One configuration, multiple operations" and is suitable for deployment in large projects and production environments. It can be said that it is more automated.

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.