Ubuntu16.04 Configuring a Python virtual environment

Source: Internet
Author: User
Tags virtual environment virtualenv

Reasons to use Virtualenv:

1. python2.7 and python3.5 are present in the ubuntu16.04 system by default. If you choose to install python3.6 the Python version is much more, and to avoid the effects of the Python version, you can use Virtualenv and virtualenvwrapper to manage the virtual environment

2. In the process of using Python development, many projects will inevitably encounter different projects depending on the different versions of the library problem;

3. The development process does not want to allow the physical environment to fill a variety of libraries, triggering future dependence disaster.

4. Use different virtual environments for different projects to keep the development environment and the host environment clean

Virtualenv a great tool to help us manage different python environments. Virtualenv can create multiple virtual environments in the system that are different and do not interfere with each other. Also, it's worth mentioning that using PIP installation dependencies in a VIRTUALENV virtual environment can bypass the permission settings for some systems, because after all, you don't need to write data to the system directory.

Installing Virtualenv

Install according to the PIP for the Python version you are using. For example, you need to use python3.6 (python3.6 corresponds to pip3.6) at this point: sudo pip3.6 install virtualenv can be completed

Virtualenv use

Set up a virtual environment for a new engineering test, located in/path/to/project/test/

Virtualenv/path/to/project/test

Using LS, you can see that the project has more bin, Lib folder

Where the bin directory contains some of the commands available in this virtual environment, as well as the script activate that opens the virtual environment; Include contains header files in the virtual environment, including Python header files; Lib is a library of dependencies ~ ~

Of course, we are not yet in the virtual environment. Activating a virtual environment requires only one command.

Source/path/to/project/test/bin/activate

At this point we can already be in the virtual environment.

Determine whether there is a project name in the virtual environment, depending on the front of the command line

Just want to install a library (such as XLRD) in this virtual environment: Go into the virtual environment and use PIP install XLRD. Be careful not to use sudo

Exit the virtual Environment input deactivate

If you want to delete a virtual environment, simply delete the bin, include, and LIB directories in this directory

Virtualenvwrapper

One of the biggest drawbacks of virtualenv is that every time you start a virtual environment, go to the directory under the directory where the virtual environment is located bin source activate , which requires us to remember the directory where each virtual environment resides.

Virtualenvwrapper, all of the virtual environment catalogs are centralized, such as to be placed ~/virtualenvs/ , and used in different directories for different virtual environments to manage. It also eliminates the source operation of each time the virtual environment is turned on, making the virtual environment more usable.

Installation

You can also use the Pip method to install Virtualenvwrapper. (if Virtualenv is installed)

sudo pip install Virtualenvwrapper

Installation error may occur in the installation, the main problem is in a called six on the package. So when installing, you can use the following methods.

sudo pip install virtualenvwrapper--ignore-install Six

Use

The virtualenvwrapper needs to be configured. It needs to specify an environment variable, called Workon_home, and needs to run its initialization tool virtualenvwrapper.sh. Workon_home is the directory that it will use to store various virtual environment directories, which we can set to/.virtualenvs.

# Virtualenv-wrappper Path
Export Workon_home= $HOME/.virtualenvs
Export virtualenvwrapper_python=/usr/local/bin/python3.6
Export Virtualenvwrapper_virtualenv=/usr/local/bin/virtualenv
source/usr/local/bin/virtualenvwrapper.sh

Make changes according to your own circumstances, such as python3.6

Since we need to perform these two operations each time, we can write them to the terminal's configuration file. For example, if you use bash, add to ~/.BRASHRC, or add to ~/.ZSHRC if you use Zsh. This will automatically run every time the terminal is started, the terminal after which Virtualenvwrapper can be used.

With Virtualenvwrapper, we can easily create a virtual environment with the following commands.

Mkvirtualenv Test

Then we have a virtual environment called test. It is stored in the $workon_home/test directory.

The virtual environment is automatically activated after you create a new virtual environment. If we normally want to enter a virtual environment, we can use the following command.

Workon Test

This is why the directory hosting the virtual environment in the environment variable is called Workon_home. By the way, Workon back can support the TAB key auto-completion.

Leave the virtual environment.

Deactivate

Delete the virtual environment.

Rmvirtualenv Test

The original text can be consulted: 1190000004079979

 

Ubuntu16.04 Configuring a Python virtual environment

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.