Python virtual environment installed on Ubuntu

Source: Internet
Author: User
Tags virtual environment virtualenv install pandas theano install matplotlib

The installation guide is operated under Ubuntu. Different Linux versions, the installation instructions are different. Therefore, some of the instructions for this guide do not apply to non-Ubuntu systems such as CentOS.

Why do I need to use a virtual environment?

A virtual environment is a tool that creates a virtual Python environment for these projects by placing a dependency on the different project requirements separately in a separate place. It solves the dilemma of "Project X relies on version 1.x and Project Y requires Project 4.x", and keeps your global site-packages directory clean and manageable.

Install Virtualenv:

First, some modules, such as setuptools, need to be installed:

sudo apt-get install python-setuptools python-dev build-essential

Then install PIP, the next virtualenv and other environments can be installed by PIP:

sudo apt-get install Python-pip

Detailed PIP installation can be consulted in the documentation: http://pip-cn.readthedocs.io/en/latest/installing.html

Next, we can install virtualenv via PIP:

$ sudo pip install virtualenv

At this point, we can create a virtual environment for the project:

$ virtualenv < virtual environment name >

The command creates a virtual Environment folder in the current file that contains a copy of the Python executable and Pip library. You can switch to the virtual environment directory we created and activate it via the source bin/activate directive. At this point, the left side of your terminal will have the name you created before using the VIRTUALENV command, indicating that you have entered the virtual environment. At this point, you can install any Python library you need with PIP. For example, installing Theano can be installed with the following instructions:

$ pip Install Theano

View the list of installed libraries by using the PIP List directive.

Exit instructions for this virtual environment:

$ deactivate

Delete the simplest, directly rm-rf the current folder.

Install Virtualenvwrapper:

Installation of virtual environment through VIRTUALENV, use is more troublesome, management is also very annoying. It is recommended to install Virtualenvwrapper, which makes the virtual environment easier to work with.

Installation:

Pip Install Virtualenvwrapper

The default virtualenvwrapper is installed under/usr/local/bin

We can then create a folder to hold the virtual environment, such as:

$ mkdir $HOME/.virtualenvs

Next, we need to configure the ~/.BASHRC to add the virtualenv:

Export Workon_home= $HOME/.virtualenvs

source/usr/local/bin/virtualenvwrapper.sh

Let BASHRC take effect:

SOURCE ~/.BASHRC

In this way, the Virtualenvwrapper is ready for installation. We can install the virtual environment by using the following command:

Mkvirtualenv < virtual environment name >

At this time, this folder will be created in the directory workon_home. We can select a virtual environment with the Workon + <tab button >. Other swimming, such as the LSVIRTUALENV command can be consulted: http://pythonguidecn.readthedocs.io/zh/latest/dev/virtualenvs.html

Next, create one or more virtual environments Env1,env2:

$ mkvirtualenv env1

Once successful, the current path will be preceded by a(env1)

$ mkvirtualenv env2

Here are some basic operations commands

    1. List Virtual environments:
$ lsvirtualenv -benv1env2
    1. To switch between virtual environments:
$ workon env1
    1. See which packages are installed in your environment:
$ lssitepackages
    1. Enter the current environment:
$ cdvirtualenv
    1. Site-packages into the current environment:
$ cdsitepackages$ cdsitepackages pip
    1. To replicate a virtual environment:
as env3...
    1. Exit the virtual Environment:
$ deactivate
    1. To delete a virtual environment:
$ rmvirtualenv env2Removing env2...
Installation Numpy,scipy,matplotlib, etc.

Following the installation of Python's various packages, it is more smooth, such as Ann on the ENV1:

$ workon env1

Installing NumPy
pip install numpy

Installing scipy
$ pip install scipy

Installing Matplotlib
$ pip install matplotlib

Installing Ipython
$ pip install ipython[all]

Installing Pandas
$ pip install pandas

Installing Statsmodel
$ pip install statsmodel

Installing Scikit-learn
$ pip install scikit-learn

In order to complete the installation successfully, follow-up can be analyzed in the virtual environment.

Each time you enter the virtual environment, the following code can be executed,
However, because my environment variables are not written into the BASHRC, we have to execute two more lines:

cd ~/workspaces$ export WORKON_HOME=~/workspaces$ source /usr/local/bin/virtualenvwrapper.sh$ workon env1

Exiting the virtual environment is used

$ deactivate

Transferred from: https://www.linuxidc.com/Linux/2017-04/142752.htm

Python virtual environment installed on Ubuntu

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.