VIRTUALENV Create an isolated Python environment __python

Source: Internet
Author: User
Tags virtual environment virtualenv
virtualenvVirtualenv is a tool. Used to create an isolated python environment.
First, installation
# pip Install virtualenv

Ii. Creating a Python virtual environment
Command: virtualenv [virtual Environment name].
# virtualenv ENV1
After execution, a folder with the same name as the virtual environment is generated locally.

Use the--python parameter to specify the Python version of the virtual environment:
# virtualenv--python=/usr/local/python-2.7.8/bin/python2.7 ENV1

If you want to rely on a Third-party package for your system environment, you can use parameter--system-site-packages.
Alternatively, you can use the Virtualenvwrapper toggle Global sitepackages command to control whether the current environment uses global site-packages.
# virtualenv--system-site-packages ENV1

Third, start the virtual environment
Enter the virtual environment directory and start the virtual environment as follows:
[Root@com ~]# CD env1/
[root@com env1]# Source Bin/activate
(ENV1) [Root@com env1]# Python-v
Python 2.7.8

All modules in the future PIP installation will be installed in the virtual environment directory.

Four, exit the virtual environment
#deactivate

VirtualenvwrapperVirtualenvwrapper is a virtualenv extension tool that makes it easy to create, delete, replicate, and switch to different virtual environments.
1. Installation and Configuration
# pip Install Virtualenvwrapper
Create a folder that holds all virtual environments:
# mkdir ~/workspaces
Set the environment variable and add the following two lines to the ~/.BASHRC.
Export Workon_home=~/workspaces
source/usr/bin/virtualenvwrapper.sh #/usr/local/bin/virtualwrapper.sh)
To make the setting take effect:
# source ~/.BASHRC


2. Create a virtual Environment: mkvirtualenv [Virtual Environment name]
[Root@com ~]# mkvirtualenv ENV1
(ENV1) [Root@com ~]#

Mkvirtualenv can use virtualenv parameters, such as--python, to specify the Python version. When you create a virtual environment, you automatically switch to this virtual environment. The virtual Environment directory is in the Workon_home.
(ENV1) [root@com ~]# mkvirtualenv envpy3--python=python3
(ENVPY3) [Root@com ~]#

3. List Virtual environments: Lsvirtualenv-b
(ENV2) [Root@.com ~]# Lsvirtualenv-b
Env1
Env2
4. Switch virtual Environment: Workon [Virtual Environment name]
(ENV2) [Root@com ~]# Workon ENV1
(ENV1) [Root@com ~]# Echo $VIRTUAL _env
/root/workspaces/env1

5. See what packages are installed in your environment: lssitepackages

6. Directory entering the current environment: cdvirtualenv [subdirectory name]
# cdvirtualenv
# pwd
/root/workspaces/env1sudo Easy_install Virtualenvwrapper
# Cdvirtualenv Bin
# pwd
/root/workspaces/env1/bin

Site-packages directory to enter the current environment: cdsitepackages [subdirectory name]
# cdsitepackages
# pwd
/root/workspaces/env1/lib/python2.6/site-packages
# cdsitepackages Pip
# pwd
/root/workspaces/env1/lib/python2.6/site-packages/pip
7. Control whether the environment uses global site-packages:toggleglobalsitepackages

8. Replication Virtual Environment: cpvirtualenv [Source] [dest]
# cpvirtualenv Env1 env3

9. Exit the virtual environment: deactivate
10. Delete Virtual Environment: rmvirtualenv [Virtual Environment name]

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.