Python Virtual Environment virtualenv installation and use details, pythonvirtualenv

Source: Internet
Author: User
Tags virtual environment virtualenv

Python Virtual Environment virtualenv installation and use details, pythonvirtualenv

Preface

Dynamic languages Ruby and Python all have their own virtual environments. virtual environments are independent execution environments during program execution. Different virtual environments can be created on the same server for different systems, the Running Environments between projects are independent and independent from each other. For example, Project B can run in the Python2-based environment, while project B can run in the Python3-based environment. Python manages the virtual environment through the virtualenv tool.

Install virtualenv

$ pip install virtualenv

Usage

$ cd my_project_folder$ virtualenv venv

After the command is executed, it will create a folder in the current directory, which contains some Python execution files and pip copies for installing other packages.

.├── bin│ ├── activate│ ├── activate.csh│ ├── activate.fish│ ├── activate_this.py│ ├── easy_install│ ├── easy_install-3.5│ ├── pip│ ├── pip3│ ├── pip3.5│ ├── python -> python3│ ├── python3│ ├── python3.5 -> python3│ └── wheel├── include│ └── python3.5m -> /Library/Frameworks/Python.framework/Versions/3.5/include/python3.5m└── lib └── python3.5

In addition, you can select the specified Python interpreter when creating an env. For example, the following is a virtual environment created based on Python3.

$ virtualenv -p /usr/local/bin/python3 venv

By default, the virtual environment depends on the site packages in the system environment, that is, the third-party packages installed in the system will also be installed in the virtual environment. If you do not want to rely on these packages, you can add the -- no-site-packages parameter to create a virtual environment.

Virtualenv -- no-site-packages [Virtual Environment name]

Activate Virtual Environment

cd envsource ./bin/activate

After successful activation, the name of the virtual environment is displayed in the command line, similar((env)Your-Computer:your_project UserName$)

Exit the Virtual Environment

$ deactivate

To delete a virtual environment, runrm -rf venv/Command.

Install Python packages in a Virtual Environment

Virtualenv comes with pip Installation tool, so you can directly run the packages to be installed:

Pip install [suite name]

If the virtual environment is not started and the pip tool is installed in the system environment, packages will be installed in the system environment. To avoid this, you can ~ /. Add the following to the bashrc file:

export PIP_REQUIRE_VIRTUALENV=true

If pip is run when the virtual environment is not enabled, an error is prompted:

Could not find an activated virtualenv (required).

Virtualenvwrapper

Virtaulenvwrapper is the extension package of virtualenv, which is used to facilitate the management of virtual environments. It can do the following:

1. Integrate all virtual environments into one directory

2. Manage (add, delete, and copy) Virtual Environment

3. Switch the Virtual Environment

4 ....

Install Virtualenvwrapper

Virtualenv must be installed before Virtualenvwrapper is installed.

$ pip install virtualenvwrapper

Virtualenvwrapper cannot be used yet. By default, virtualenvwrapper is installed under/usr/local/bin. In fact, you need to run virtualenvwrapper. sh file. Don't worry. Open this file and check it. There are installation steps in it. We can set the environment according to the operation.

1. Create a directory to store the Virtual Environment

mkdir $HOME/Envs

Edit ~ /. Zshrc or ~ /. Bashrc (depending on the shell type you use)

Export WORKON_HOME = $ HOME/Envs source/usr/local/bin/virtualenvwrapper. sh 3. Run:

$ source ~/.zshrc

Now virtualenvwrapper can be used. Basic usage of virtualenvwrapper:

1. List Virtual Environments

Workon or lsvirtualenv

Create a virtual environment

Mkvirtualenv [Virtual Environment name]

2. Start/switch the Virtual Environment

Workon [Virtual Environment name]

3. delete a virtual environment

Rmvirtualenv [Virtual Environment name]

4. Leave the virtual environment and use the same commands as virutalenv.

deactivate

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message to us. Thank you for your support.

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.