Python environment for the Python development environment

Source: Internet
Author: User
Tags deprecated virtual environment virtualenv

Life is short, I use Python

First, upgrade to python2.7

The development environment of the system is CentOS 6.0, the default Python version is 2.6.6, because the online environment is python2.7, in order to prevent the issue of version differences, so to upgrade Python, upgrade Python under CentOS there is the way

    • Yum installation, the simplest, but the official download source version update is slow, so you need to change the download source of the development machine, such as NetEase's
    • RPM Installation
    • SOURCE Installation

Here only to introduce the process of updating the source code, because the source of the installation, customization of a lot of things, compared to control it

To install Python, you need to download the dependent components that will be used during compilation or during development

Yum Groupinstall "Development tools" Yum install Mysql-python zlib-devel bzip2-devel openssl-devel ncurses-devel Sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel-yyum install python-psycopg2-yyum install gcc py Thon-devel-yyum Install Python-memcached-y

Next can be developed to download python, for some reason, the Python official website will be Q (everyone knows), so it is recommended to replace the download source, such as: Sohu Http://mirrors.sohu.com/python

wget http://mirrors.sohu.com/python/2.7.7/python-2.7.7.tgz
TAR-XVF python-2.7.7.tgz
CD Python-2.7.7
./configure--prefix=/usr/local/python2.7
Make
sudo make install

In my first compilation process, encountered a lot of wonderful problems, such as:

Python build finished, but the necessary bits-to-build these modules were not found:  _bsddb                            
_curses _curses_panel _sqlite3 _ssl _tkinter bsddb185 bz2 dbm DL gdbm imageop readline sunaudiodev zlib to find the necessary bits, look in setup.py in detect_ Modules () for the module ' s name.

To give the message, it is clear that the corresponding module during compilation is not found, so also need to install the dependency package in advance, find some dependency on the network, the corresponding relationship, as follows:

Module Depend on Description
_bsddb Bsddb Interface to Berkeley DB Library. Interface to the Berkeley database
_curses Ncurses Terminal handling for Character-cell displays.
_curses_panel Ncurses A panel stack extension for curses.
_sqlite3 Sqlite DB-API 2.0 interface for SQLite databases. Sqllite,centos can be installed Sqlite-devel
_ssl openssl-devel.i686 Tls/ssl wrapper for socket objects.
_tkinter N/A A thin object-oriented layer on top of TCL/TK. If you do not use a desktop program, you can ignore Tkinter
bsddb185 Old BSDDB Module Old BSDDB module, can be ignored.
bz2 bzip2-devel.i686 Compression compatible with bzip2. Bzip2-devel
Dbm Bsddb Simple "Database" interface.
Dl N/A Call C functions in shared objects. Python2.6 started, has been deprecated.
gdbm gdbm-devel.i686 GNU ' s reinterpretation of dbm
Imageop N/A Manipulate RAW image data. has been deprecated.
ReadLine Readline-devel GNU ReadLine Interface
Sunaudiodev N/A Access to Sun audio hardware. This is for Sun platform, can be ignored under CentOS
Zlib Zlib Compression compatible with gzip

I installed the Readline-devel,sqlite-devel, bzip2-devel.i686,openssl-devel.i686,gdbm-devel.i686,libdbi-devel.i686,ncurses-libs,zlib-devel.i686, these dependent packages, recompile, all OK.

After installation, confirm that the installation is correct

/usr/local/python2. 7/bin/python2. 7 -V

However, the execution of Python-v is still Python 2.6, so make a soft connection

Ln-fs/usr/local/python2.7/bin/python2.7/usr/bin/python

Now execute python-v, show ptyhon2.7.

But, there is one last question, after the change, Yum will not run, so modify the first line of the/usr/bin/yum file

# !/usr/bin/python

Change into

# !/usr/bin/python2.6

Run Yum,everthing is ok! again

Second, install pip

Install Setuptools First, if python3.0 version, to install Distutils dependent package

wget Https://pypi.python.org/pypi/ez_setup

Unzip, enter the directory, execute:

Ptyhon ez_setup.py

The Easy_install was installed,

Next, install the PIP

wget Https://pypi.python.org/pypi/pip

Unzip, enter the directory, execute:

Python setup.py Install

After successful installation, execute pip , prompt command not found

The original PIP has not been put into the path.

Echo $PATH/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/titan/bin

So to add the PIP path to the system environment variable

# Vim/etc/profile

Add at the end of the document

Export path= "/usr/local/python-2.7.7/bin: $PATH"

Save exit. Execute # source/etc/profile

No error, it means success

Execute # pip-v again to display the command prompt.

Or because of the well-known reasons, PyPI website is also very slow links, it is recommended to use the domestic pypi source, such as watercress: http://pypi.douban.com

Add ~/.pip/pip.conf and write the following:

[Global] Index-url = http://pypi.douban.com/simple[install]trusted-host=pypi.douban.com

Everthing is ok!

Third,virtualenv

a machine may have multiple projects, in order to ensure that each project depends on the environment is pure, only provided to their own, then need to virtualenv!

virtualenv is used to create a stand-alone Python environment where multiple python is independent from each other and can :

    1. Install a new suite without permissions
    2. Different versions of the suite can be used for different applications
    3. Package upgrade does not affect other apps

1, installation Virtualenv

Install via Pip, or install via Yum

# pip Install virtualenv

How to use:

virtualenv [Virtual Environment name]

For example, create a tjf_env virtual environment

Virtualenv tjf_env

By default, the virtual environment relies on site packages in the system environment, which means that the installed third-party package in the system is also installed in the virtual environment, if you do not want to rely on the package, Then you can add the parameter--no-site-packages set up the virtual environment

virtualenv--no-site-packages [Virtual environment name]

Start the virtual environment

CD ENV
source./bin/activate

Note that at this point the command line one more (env), Env is the virtual environment name, then all the modules will only be installed in the directory.
Exit the virtual Environment:

Deactivate

Installing the Python Suite virtualenv in a virtual environment comes with the PIP installation tool, so the kit that needs to be installed can be run directly:

PIP install [package name]

If the virtual environment is not started and the PIP tool is installed, the package will be installed in the system environment, in order to avoid this, you can add the following in the ~/.BASHRC file:

Export Pip_require_virtualenv=true

or let the system automatically turn on the virtual environment when the PIP is executed:

Export Pip_respect_virtualenv=true

2, Installation Virtualenvwrapper

Virtaulenvwrapper is an expansion pack for virtualenv that makes it easier to manage virtual environments and can do:

    1. Consolidate all virtual environments in one directory
    2. Manage (new, delete, copy) virtual environments
    3. Switching Virtual Environments
    4. ...

Installation: sudo pip install virtualenvwrapper

At this time can not use Virtualenvwrapper, the default virtualenvwrapper installed under/usr/local/bin, in fact, you need to run the virtualenvwrapper.sh file before the line, do not worry, open this file to see, There are installation steps, we set the environment according to the operation.

1. Create a directory to store the virtual environment

mkdir $HOME/.virtualenvs

2. Add line in ~/.BASHRC: export workon_home= $HOME/.virtualenvs

3. Add line in ~/.BASHRC: source/usr/local/bin/virtualenvwrapper.sh

4. Run: source ~/.BASHRC

At this point the Virtualenvwrapper is ready to use.
Lists the virtual environment list Workon

New virtual Environment mkvirtualenv [virtual environment name]

Start/Switch Virtual environment Workon [virtual environment name]

Deleting a virtual Environment rmvirtualenv [virtual environment name]

Leaving the virtual environment deactivate

Virtualenv complete, everything is ok!

Iv. Web Framework

With the basics of PIP, what you want to install, it's almost as easy as a sniffer bag to take:

Pip Install djangopip install Tornadopip install flask

Python environment for the Python development environment

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.