Python virtualenv Virtual Environment configuration

Source: Internet
Author: User
Tags install django pip install django virtual environment virtualenv

http://blog.csdn.net/pipisorry/article/details/47008981

What is it

Virtualenv is used to create multiple standalone Python runtime environments on a single machine, Virtualenvwrapper provides some handy package on the command line.

Virtualenv is a very good virtual Python environment builder, and his greatest benefit is that it allows each Python project to use one environment alone without affecting the Python system environment or the environment of other projects.

Virtualenv can be used to create a standalone Python environment in which different Python versions or different packages are available, and you can install new packages in the environment without root privileges, with no effect on each other.


Why do you use

-Isolation of third-party package dependencies between projects, such as a project dependent on Django1.2.5,b project dependent django1.3.
-To facilitate the deployment of applications, the development environment of the virtual environment packaged in a production environment, do not need to be on the server again.


Installing Virtualenv

Note: Already installed Python3 This step may not, Python3 comes with pyvenv, with this on line, the former virtualenv myVenv--python=python3 always error

1. installation package Installation

To http://pypi.python.org/pypi/virtualenv#downloads download the latest version, after decompression, run the Python setup.py install.
After installation, make sure that the virtualenv.py file is found in your system's path, otherwise it will not be able to run through the command line. Of course, it is also possible to run in the installation directory, which is not recommended.

2. PIP installation

Pip Install Virtualenv

Two new subdirectories will appear under the Python3.4.2\lib\site-packages directory Virtualenv_support, Virtualenv-13.1.0.dist-info

Installing Virtualenvwrapper

[Some common virtualenvwrapper commands may need to be mastered to facilitate the management of Python versions, linked in:
Http://www.doughellmann.com/docs/virtualenvwrapper/command_ref.html]

After installation, create a virtual environment, and then install the Virtualenvwrapper
virtualenv ENV #ENV for the environment's name, can be arbitrarily set, in fact, is a folder, in the home under the User name folder can be found.
source Env/bin/activate #这样进进入了virtualenv的虚拟开发环境.
After entering the virtual environment, the command line will start to show (ENV), the representative has entered the environment, then you can install Virtualenvwrapper and Django

Enter command line pip install Virtualenvwrapper
It is not possible to use sudo here, because in virtualenv, it is one of the most convenient designs without administrative privileges.

After the Virtualenvwrapper is installed, it will list the virtualenv as a dependent package, so it will be installed automatically.

Open a new shell, enter mkvirtualenv test . If you open another shell, you are not in this virtualenv, you can workon test start by. If your work is done, you can use it deactivate to deactivate it.

http://blog.csdn.net/pipisorry/article/details/47008981


Enter the virtual environment and use

Create a virtual environment with the system default Python version

<span style= "color: #33CC00;" ><strong>virtualenv env</strong></span>


You can create an env directory (the virtual environment name, which is the Python virtual environment) in the current directory, and you will notice that virtualenv will install Python,setuptools and Pip on you.

Create a virtual environment with another Python version

1. Install a python that requires a version
2. Specify the python version in virtualenv
virtualenv--no-site-packages--python=2.7 env Note:
1.–no-site-packages indicates that the system global Python installation package is not included, which makes the environment cleaner

2.–python=python2.7 specifies that the version of Python is not already installed by the system Python2.7

3. Env is the established virtual environment name

4. The executable python does notexist error occurs without installing python2.7 or using the command virtualenv--no-site-packages--python=python2.7 env

Entering a virtual environment
Windows:

Env\script\activate.bat

Linux (Mac OS):

SOURCE Env/bin/activate

At this point, you will find that you have entered the virtual machine in front of the command line with an extra (env) out. Now you can use pip install xxx to install the library you want.

The note:activate. Bat script Sets the console environment variable so that the Python command that the console calls later executes the Python in the virtual environment

Exiting a virtual environment

1. Use commands directly in this environment to deactivate exit

2. Deactivate. Bat is a restore console environment variable setting


pycharm The configuration item runs in VirtualenvSetting up a project in Pycharm run in virtualenv

First virtualenv venv Create a virtual environment and install the relevant operating environment.

Open Pycharm


You can select the desired operating environment in the drop-down box

If you cannot find the virtual environment that you created, you can add a new Python interpreter to the right-hand settings button, and add local to select the Python interpreter in the virtual environment venv that you created, such as D:\venv\Scripts\ Python.exe) It's ready.



Where is the installed library?
Env/lib/site-packages/directory, not in the system's Python lib/site-packages directory, so you know why the virtual environment is separate.

Note:the virtual environment created by Virtualenv is completely unrelated to the host's Python environment, and your host-configured libraries cannot be used directly in virtualenv. You need to install the configuration again using Pip install in a virtual environment before you can use it.


Take a chestnut: go to the environmental test and install Django

1. Pip installs Django in a virtual environment

Use source env/bin/activate the command to enter the development environment, then view the Python version, then use the pip install django install Django

import django

No error has occurred and the Django has been successfully installed

2. Installation package Installation

As long as it's still in the VIRTUALENV environment, the steps to install Django are exactly the same as the steps to actually install Django. You can refer to the steps of the official website. It's actually downloading, and then entering the command line.
Https://docs.djangop roject.com/en/1.4/to pics/install/#instal ling-a-distribution- Specific-package
1. Download the latest release from our Download page.
2. Untar the downloaded file (e.g. tar xzvf django-x.y.tar.gz, where x.y is the version number of the latest release). If you ' re using Windows, you can download the command-line tool Bsdtar to does this, or you can use a gui-based tool such a S 7-zip.
3. Change to the directory created in step 2 (e.g. CD django-x.y).
4. If you ' re using Linux, Mac OS X or some other flavor of Unix, enter the command sudo python setup.py install at the Shell prompt. If you ' re using Windows, start a command shell with administrator privileges and run the Commandpython setup.py install. This would install Django in your Python installation ' s site-packages directory.
After you install Django, use the Deactivate command to exit virtualenv.

from:http://blog.csdn.net/pipisorry/article/details/47008981

Ref: Using Vitrualenvwrapper to isolate a Python project's library dependencies

Using virtualenv to build a Python3 Django environment


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python virtualenv Virtual Environment configuration

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.