Installation and use of Python virtual environment under Ubuntu system

Source: Internet
Author: User
Tags virtual environment virtualenv

When developing Python projects, it is necessary to create multiple Python virtual environments in a virtual environment, because different projects require different resource bundles and related configurations.

Install the virtual environment

Open Linux Terminal (shortcut key ctrl+alt+t), enter command

sudo apt install python-virtualenvsudo easy_install virtualenvwrapper

Note: The above two commands are executed one after the other, after completion of the virtual environment installation.

Or you can use Pip to install, as long as Pip is installed, and Python comes with a pip:

sudo pip install virtualenvsudo pip install Virtualenvwrapper
    • Description: The result of the installation is identical, use any one.
    • Virtualenv is a virtual environment, Virtualenvwrapper virtualenv commands are encapsulated to make them more friendly

Note: The order of installation cannot be reversed, virtualenvwrapper must depend on virtualenv.

Create a virtual environment
    • Enter the following command at the terminal:
      MKVIRTUALENV virtual environment name such as: Mkvirtualenv mywork//creates a virtual environment named MyWork
    • The created virtual environment is saved in the/home/.virtualenvs directory and can be viewed in this directory;
    • The virtual environment is automatically entered when the creation is successful, and the command line prompts to change;

    • To specify a python version to create a virtual environment

When the above command executes, the system will create the default Python version of the development environment, and generally everyone's computer will install the Python2 and Python3 versions at the same time, sometimes requiring different versions to create a virtual environment.

Mkvirtualenv-p python path Virtual Environment name example: Mkvirtualenv-p/usr/bin/python3 mywork
    • -P refers to the shorthand for path
    • The path of the py installation, which is installed by default in the/usr/bin/directory, and if it is not found at one time, use the command:
Whereis Python3

Details: You do not need to go to the/home/.virtualenvs directory, you can execute commands under any directory, and the virtual environment is created in the same location.

Note: Creating a virtual environment is required to be done under networking, otherwise the creation fails.

Note: Using Mkvirtualenv to create a virtual environment, you may be prompted not to find the command, typically the script file is not joined to the system path, resolved as follows:

    • Method One:
      # find Virtualenvwrapper script file Whereis virtualenvwrapper# Enter the directory CD/ usr/local/python3/bin# Execute script file activation Virtualenvwrappersource virtualenvwrapper.sh# can use commands correctly 
    • method Two:
      # method requires manual activation after each reboot, inconvenient, add command to environment variable sudo vi ~/.bashrc# add a line at the end source/usr/local/python3/bin/virtualenvwrapper.sh# After saving, executing the source ~/.bashrc# can now use the command, and each time you restart the host auto-load command 
      Virtual environment View and use
    • Delete virtual environment
      rmvirtualenv [Virtual Environment Name] Example: Rmvirtualenv mywork 

      Note: If the current location is in a virtual environment, you need to exit the virtual environment before you can perform the delete

      Note: You can perform a delete operation in any directory, and if you do not know the name, you can Rmvirtualen + two times tab to prompt all virtual environments

    • Exiting a virtual environment
      Deactivate
    • Entering a virtual environment
      Workon [Virtual Environment Name] Example: Workkon mywork

      Note: Pressing workon+ two times the TAB key prompts all virtual environments.

    • View Python packages installed in a virtual environment
      Pip Listpip Freeze

      Two commands can be queried, but the form of display is not the same

Install Python packages in a virtual environment
    • Enter the virtual environment, in the terminal input:
      The name of the PIP install package

Note: Be sure not to use sudo pip ..., this is to install the Python package in a virtual environment, if you use sudo permissions, the Python package will be installed in the host non-virtual environment, in the virtual environment can not find this package.

Installation and use of Python virtual environment under Ubuntu system

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.