Under Ubuntu installation and use of Python virtual environment

First, install the virtual environment
1. The first use of the environment need to install python pip (Note: By default Linux pip is not installed, you need to manually install)
installation pip command: sudo apt install python-pip: python2.7 installed
sudo apt install python3-pip: python3.5 installation
Note: If pip version is too low, you need to upgrade, pip install --upgrade pip, the installation is successful, it is best to restart

2. Install the virtual environment virtualenv
installation command: pip install virtualenv
problems that may arise:
question 1: Could not get lock / var / lib / dpkg / lock
this problem could be have another program is running, resulting in capital being locked unavailable . The cause of the locked resources may not be completed normally when the last run or install updates, and then this condition occurs, the solution is simple:
type in the following two in the terminal:
sudo RM / var / Cache / APT / Archives / Lock
sudo RM / var / lib / dpkg / Lock
question 2: Consider using the `--user` option or check the permissions
solution: pip install --user virtualenv 


Second, the use of the virtual environment
1, creating a virtual environment 
  1. create a directory to store virtual environment mkdir, and enter the directory
  command: mkdir vir
     cd vir
  2. create a virtual environment 
  virtualenv -p python language version of the name of the environment of the virtual path 
  example: virtualenv -p /usr/bin/python3.5 test1
  ( the default python version python2.7, the default will automatically create a corresponding directory name with the same name as the virtual environment directory)

2. activate the virtual environment
  1. Activate the virtual environment, when the virtual environment is activated before the command can be seen (the name of the virtual environment)
  Source virtual environment directory / bin / of an activate
  2. Exit virtual environment
  deactivate
  if you want to delete a virtual environment, simply exit the virtual environment , delete the corresponding virtual environment directory. It does not affect other environments.

3. unified virtual environment management software virtualenvwrapper 
  1. Install the management software
  sudo easy_install virtualenvwrapper
  default virtualenvwrapper installed in / usr / local / bin Now,
  in fact, need to run virtualenvwrapper.sh file for the job; so you need to configure it:

  2. Configure environment variables (configuration software running path and the storage location of the virtual environment)

  Create a virtual environment management directory: mkdir $ the HOME / .local / virtualenvs
  $ the HOME / Home / current user 
  vim ~ / .bashrc add the following content:
    

export VIRTUALENV_USE_DISTRIBUTE=1
    export WORKON_HOME=$HOME/.local/virtualenvs   
    if [ -e $HOME/.local/bin/virtualenvwrapper.sh ];then 
    source $HOME/.local/bin/virtualenvwrapper.sh 
    else if [ -e /usr/local/bin/virtualenvwrapper.sh ];then 
    source /usr/local/bin/virtualenvwrapper.sh 
    fi 
    fi 
    export PIP_VIRTUALENV_BASE=$WORKON_HOME 
    export PIP_RESPECT_VIRTUALENV=true

 

    Refresh the environment variables: source ~ / .bashrc

    Activating virtual environment management software
    Source /usr/local/bin/virtualenvwrapper.sh
  3. Use virtual environment
    1. Create and activate the virtual environment: mkvirtualenv virtual environment name (the default is created python2.7)
    2. create the specified language version of the virtual environment 
      mkvirtualenv name -p python language version of the path of the virtual environment
      name mkvirtualenv -p /usr/bin/python3.5 virtual environment
    3, exit the virtual environment: deactivate
    4., virtual environment before proceeding to use: workon virtual environment name (the virtual environment must be present)
    5, delete the virtual environment: rmvirtualenv virtual environment name
    6, lists all environment: workon or lsvirtualenv -b

Published 131 original articles · won praise 22 · views 120 000 +

Guess you like

Origin blog.csdn.net/qq_38890412/article/details/104536645