Virtualenv Basic Tutorials for Beginners

Source: Internet
Author: User
Tags virtualenv

This article directory:

[TOC]
Introduction to Virtual Environments

Virtualenv is used to create multiple standalone Python virtual environments on a single machine, and multiple Python environments are independent from each other and can:

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

The virtual environment is a private copy on the Python interpreter, and you can install packages in an isolated environment without affecting the global Python interpreter in your system.

A virtual environment is useful because it prevents the system from having problems with package management clutter and version conflicts. Creating a virtual environment for each application ensures that applications can access only the packages they use, so that the global interpreter acts as a source and remains clean and tidy to more virtual environments. Another benefit is that the virtual environment does not require administrator privileges.
Installing Virtualenv

Most Linux distributions offer a virtualenv package. For example, Ubuntu users can install by using the following command:

$ sudo apt-get install python-virtualenv

If you are using Mac OSX, you can use Easy_install to install Virtualenv:

$ sudo easy_install virtualenv

If you are using Microsoft Windows or any operating system that does not provide an official VIRTUALENV package, then you will have a slightly more complex installation process.

Using your Web browser, navigate to https://bitbucket.org/pypa/setuptools/, Setuptools Setup's homepage, and find a link in the "Downloads" section to download a call ez_setup.py The installer script. Save this file to your computer's Temp folder, and then run the following command in that directory:

$ python ez_setup.py $ easy_install virtualenv

Note: The previous command must be issued by an account with administrator privileges. In Microsoft Windows, run the command Prompt window as an administrator option. In Unix-based systems, the two installation commands must be preceded by sudo or executed as the root user. Once the installation is complete, the VIRTUALENV program can be executed through a regular account.
Create a virtual environment

Once installed, we can use the virtualenv command to create a Python virtual environment. This command has a required parameter: the name of the virtual environment. A folder with the specified name and all the files in it that are related to the virtual environment are created in the current directory. The virtual environment convention is typically named Venv:

$ virtualenv venv
New python executable in venv/bin/python2.7
Also creating executable in Venv/bin/python
Installing Setuptools............done.
Installing Pip...............done.

Now you have a venv folder and a completely new virtual environment that contains a private Python interpreter. When using a virtual environment, you must "activate" it. If you are using the Bash command-line tool (Linux and Mac OSX users), you can use this command to activate the virtual environment:

$ source Venv/bin/activate

If you are using Microsoft Windows, the activation command is:

> Venv\scripts\activate

When the virtual environment is activated, the location of the Python interpreter is added to the PATH, but the change is not permanent; it affects only the current command session. To remind you that you have activated the virtual environment, the activation command will include the name of the environment in the command prompt:

(venv) $

When you're done working in a virtual environment and want to go back to the global Python interpreter, you can enter deactivate at the command prompt.
To install a Python package using PIP

Most Python packages are installed through the PIP program, and Virtualenv are added automatically when creating a virtual environment. When a virtual environment is activated, the location of the PIP program is added to the PATH.

Note: If you use Pyvenv to create a virtual environment in Python 3.3, you must manually install PIP. The installation instructions can be found on the PIP website. Under Python 3.4, pyvenv will automatically install PIP.

For example, to install flask into a virtual environment, use the following command:

(venv) $ pip Install flask

With this command, both the flask and its dependency sets are installed in the virtual environment. You can verify that flask is installed correctly by starting the Python interpreter and trying to import it:

(venv) $ python
>>> Import Flask
>>>

If you need to install more packages, this will be more cumbersome, we also have a one-click installation method. First create a new text file, such as: Requirements.txt, and then save the package name you need to install into the file (depending on your needs), as follows:

babel==1.3
flask==0.10.1
flask-login==0.2.7
flask-sqlalchemy==1.0
flask-wtf==0.9.3
jinja2==2.7.1
sqlalchemy==0.8.2
wtforms==1.0.5
werkzeug==0.9.4
psycopg2==2.5.1
...

Finally you only need to enter the following command, all the required packages can be installed all:

(venv) $ pip install-r requirements.txt

If the error does not occur, congratulations: the installation was successful.

To see which packages are installed in the current environment, you can use the following command:

(venv) $ pip freeze

Comments!

Virtualenv Basic Tutorials for Beginners

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.