Python Virtual Environment virtualenv usage tutorial, pythonvirtualenv

Source: Internet
Author: User

Python Virtual Environment virtualenv usage tutorial, pythonvirtualenv

Virtualenv is a tool used to create an isolated Python environment. Virtualenv creates a folder containing all the necessary executable files to use the packages required by the Python project.

Install

pip install virtualenv

Basic usage

1. Create a virtual environment for a project:

$ Cd my_project_dir $ virtualenv venv # venv indicates the virtual environment directory name, which is customized.

Virtualenv venv will create a folder in the current directory, including the Python executable file and a copy of the pip library, so that other packages can be installed. The name of the virtual environment (venv in this example) can be arbitrary. If the name is omitted, all files will be placed in the current directory.

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

You can choose to use a Python Interpreter:

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

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

2. to start using the virtual environment, it must be activated:

$ source venv/bin/activate  

From now on, any package you install using pip will be placed in the venv folder, isolated from the global Python installation.

Installation packages, such:

$ pip install requests

3. If you have finished your work in the virtual environment temporarily, you can disable it:

$ . venv/bin/deactivate

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

To delete a virtual environment, you only need to delete its folder. (Execute rm-rf venv ).

Virtualenv is inconvenient because the virtual startup and stop scripts are in specific folders. After a while, you may have many virtual environments scattered across the system, you may forget their names or locations.

Virtualenvwrapper

Because virtualenv is not easy to centrally manage the virtual environment, virtualenvwrapper is recommended. Virtualenvwrapper provides a series of commands to facilitate working with virtual environments. It places all your virtual environments in one place.

Install virtualenvwrapper (ensure that virtualenv is installed)

Pip install virtualenvwrapperpip install virtualenvwrapper-win # Use this command in Windows

After the installation is complete ~ /. Bashrc write the following content

export WORKON_HOME=~/Envssource /usr/local/bin/virtualenvwrapper.sh 

Row 1: virtualenvwrapper stores the virtual environment directory

Line 2: virtrualenvwrapper will be installed in the bin directory of python, so this path is bin/virtualenvwrapper. sh under the python installation directory

Source ~ /. Bashrc # Read the configuration file and take effect immediately

Basic use of virtualenvwrapper

1. Create a virtual environment mkvirtualenv

mkvirtualenv venv  

In this way, the virtual environment named venv is created under the directory specified by the WORKON_HOME variable.

To specify the python version, you can use "-- python" to specify the python interpreter.

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

2. Basic commands

View the current virtual environment directory

[root@localhost ~]# workonpy2py3

Switch to Virtual Environment

[root@localhost ~]# workon py3(py3) [root@localhost ~]# 

Exit the Virtual Environment

(py3) [root@localhost ~]# deactivate[root@localhost ~]# 

Delete Virtual Environment

rmvirtualenv venv

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.