Python Virtual Environment--virtualenv

Source: Internet
Author: User
Tags virtual environment virtualenv

Virtualenv is a tool for creating an isolated python environment. VIRTUALENV Create a folder that contains all the necessary executables to use the packages required by the Python project.

Installation
Pip Install Virtualenv
Basic use
    1. Create a virtual environment for a project:
$ cd my_project_dir$ virtualenv venv #venv为虚拟环境目录名, directory name customization 

  virtualenv venvA folder will be created in the current directory, containing the Python executable file and pip a copy of the library so that the other packages can be installed. The name of the virtual environment (in this case venv ) can be arbitrary, and omitting the name will place the file in the current directory.

In any directory where you run the command, this creates a copy of Python and puts it in a file called venv .

You can choose to use a Python interpreter:

$ virtualenv-p/usr/bin/python2.7 venv The #-p parameter specifies the Python interpreter program path    

This will use /usr/bin/python2.7 the Python interpreter in.

    1. To get started with a virtual environment, it needs to be activated:
$ source Venv/bin/activate

从现在起,任何你使用pip安装的包将会放在 venvfolder, which is isolated from the globally installed Python.

Install packages as usual, such as:

$ pip Install requests
    1. If you have temporarily completed your work in a virtual environment, you can deactivate it:
$ . Venv/bin/deactivate

This will return to the system's default Python interpreter, which will also return to the default, including the installed libraries.

To delete a virtual environment, simply delete its folder. (Execution rm -rf venv ).

Virtualenv here is a little inconvenient, because virtual start, stop scripts are in a specific folder, perhaps after some time, you may have a lot of virtual environment scattered throughout the system, you may forget their name or location.

Virtualenvwrapper

Since virtualenv does not facilitate centralized management of virtual environments, it is recommended to use Virtualenvwrapper directly. Virtualenvwrapper provides a series of commands that make it easy to work with virtual environments. It puts all your virtual environments in one place.

Install Virtualenvwrapper (make sure virtualenv is installed)

Pip Install Virtualenvwrapper
Pip Install Virtualenvwrapper-win #Windows使用该命令

After the installation is complete, write the following in ~/.BASHRC

Export Workon_home=~/envs
source/usr/local/bin/virtualenvwrapper.sh

First line: Virtualenvwrapper store the Virtual environment directory

 Second line: Virtrualenvwrapper will be installed in the Python bin directory, so the path is under the Python installation directory bin/virtualenvwrapper.sh

SOURCE ~/.BASHRC #读入配置文件, effective immediately

 

Virtualenvwrapper Basic Use

1. Create a virtual environment mkvirtualenv

Mkvirtualenv venv

This creates a new virtual environment named Venv under the directory specified by the Workon_home variable.

If you want to specify a python version, you can specify the Python interpreter through "--python"

Mkvirtualenv--python=/usr/local/python3.5.3/bin/python venv

2. Basic commands

  View the current virtual environment directory

[Email protected] ~]# Workonpy2py3

Switch to a virtual environment

Exiting a virtual environment

Deleting a virtual environment

Rmvirtualenv venv

This article refers to the link: http://pythonguidecn.readthedocs.io/zh/latest/dev/virtualenvs.html

Python Virtual Environment--virtualenv

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.