VIRTUALENV Installation and use

Source: Internet
Author: User
Tags virtual environment virtualenv

  • Different people like to build their own development environment in different ways, but in almost all programming communities, there is always one (or more) development environment that makes it easier for people to accept. There are no errors in using different development environments, but some environment settings make it easier to test easily and do repetitive/templated tasks that make daily routines simple and easy to maintain.

    What is Virtualenv?

    The most common method in a python development environment is to use the VIRTUALENV package. Virtualenv is a package that is used to create a standalone Python environment. Now, there's the question: Why do we need a standalone Python environment? To answer this question, allow me to cite Virtualenv's own documentation:

    Virtualenv is a tool to create isolated Python environments.

    The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you has an application that needs version 1 of Libfoo, but another application requires version 2. How can I use both these applications? If You install everything into/usr/lib/python2.7/site-packages (or whatever your platform's), it ' s E Asy to end of a situation where you unintentionally upgrade an application that shouldn ' t is upgraded.

    Or more generally, what if you want to install a application and leave it be? If An application works, the libraries or the versions of those libraries can break the application.

    Also, what if can ' t install packages into the global site-packages directory? For instance, on a shared host.

    In all these cases, virtualenv can help you. It creates an environment the have its own installation directories, which doesn ' t share libraries with other virtualenv en Vironments (and optionally doesn ' t access the globally installed libraries either).

    The basic problem that we need to deal with is the dependency, version, and indirect permissions issues of the package. Imagine that you have two apps, one app requires Libfoo version 1, and another app requires version 2. How can I use these applications at the same time? If you install everything to the/usr/lib/python2.7/site-packages (or any platform's standard location) in this case, you may accidentally upgrade an application that should not be upgraded.

    Simply put, you can create a different/separate Python environment for each project, and you will install all the required packages into their own separate environments for each project.

    Installation and use of virtualenv

    Installing the virtualenv is simple:

    Once the virtualenv is installed, you can create a separate Python environment for your project by running the following command:

    mkdir nowamagic_venvvirtualenv--distribute nowamagic_venv

    OK, success. What happened up there? You created a folder nowamagic_venv to store your new standalone Python environment. This folder is located under/root.

    Let's take a look at the output:

    New python executable in nowamagic_venv/bin/python2.7also creating executable in Nowamagic_venv/bin/pythoninstalling Setuptools......done. Installing Pip...........done.

    The--distribute option enables virtualenv to use the new release-based package management system rather than the Setuptools-acquired package. All you need to know now is that the--distribute option automatically installs PIP in the new virtual environment, so you don't need to install it manually. When you become a more experienced Python developer, you will understand the details.

      • Activate: This virtualenv activation file
      • PIP: This virtualenv's independent pip
      • A copy of the Python:python interpreter
      • lib/python2.7: All new packages will be present in this
    Test it.

    Activate this virtualenv by using the following command:

    [Email protected] ~]# CD Nowamagic_venv[[email protected] nowamagic_venv]# source Bin/activate (nowamagic_venv) [email Protected] nowamagic_venv]#

    Run the following command to better understand the difference between the two, if you have entered the virtualenv please leave first.

    Deactivate  #离开

    First let's see if it calls the PYTHON/PIP command and it calls that one.

    [email protected] ~]# which Python/usr/bin/python
    [email protected] ~]# which PIP/USR/LOCAL/BIN/PIP

    One more time! Open the Virtualenv this time and see what's different. The following is shown on my machine:

    [email protected] ~]# which Python/root/nowamagic_venv/bin/python
    [email protected] ~]# which PIP/ROOT/NOWAMAGIC_VENV/BIN/PIP

    Virtualenv copies a copy of the Python executable file, creates useful scripts and installs the required packages for the project, and you can install/upgrade/remove them throughout the project's lifecycle. It also modifies some search paths, such as Pythonpath, to ensure that:

      • When packages are installed, they are installed in the currently active virtualenv, not in the system-wide Python path.
      • When the import code occurs, VIRTUALENV will take precedence over the packages installed in the environment, rather than the packages installed in the System Python directory.

    It is also important that, by default, all system-wide packages are visible to virtualenv. This means that if you install Simplejson in your system python directory, it will automatically be available to all Virtualenvs for use. This behavior can be changed, and the virtualenv that adds the--no-site-packages option when creating virtualenv will not read the system package, as follows:

    Virtualenv nowamagic_venv--no-site-packages
      • Here, Virtualenvs is finished, it's simple, isn't it? Follow the steps, everything is logical. And also very powerful, Python's development environment is very simple to configure. Combined with the virtualenvwrapper of the back, you can automatically manage multiple environments, which is magical.
    E>

VIRTUALENV Installation and use

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.