Python Virtual Environment virtualenv

Source: Internet
Author: User
Tags install django pip install django virtual environment virtualenv

Reference Link: Python virtual environment--virtualenv python Virtual Environment installation and configuration How to use a configured virtualenv environment in Pycharm

Virtualenv

We're not going to talk about Python today. Next we say virtualenv, English better classmate, probably already guessed half, virtual, namely: fictitious. What the hell is env? Environment? So translating into Chinese is a "virtual environment".
What exactly is a virtual environment? As the name implies, it is a virtual environment. In layman's terms, virtual machines, Docker, can be used to understand the virtual environment, is to separate part of the content, we call this part of the independent thing called "container", in this container, we can only install the dependent packages we need, and the various containers are isolated from each other, do not affect each other. We're going to learn about Django, and we'll make a Django virtual environment out of this environment.
"Prerequisite Summary"
Django is also a very popular web framework. Because Django iterations are updated very quickly and frequently, there are some obsolete things that need to be discarded, and something new needs to be added, resulting in incompatibilities between different versions. such as Django1.3, Django1.4, Django1.8 There is a great difference between.
Or, as a Python version, for example, the Python version used in the work now works with the python2.x and python3.x two kinds.
"Story Background"
Suppose you want to do Python web development, using Django. There are two old items on hand, A and B need maintenance, and new Project C is under development. Here project a uses django1.3, Project B uses django1.4, and the new Project C uses Django1.8. So the question is, how do you develop and maintain ABC three projects at the same time?
The normal pattern might be this: now there is a bug on item A that needs to be repaired, so first execute the following command to remove the original version:

Pip Uninstall Django

Then execute the following command to install django1.3

Pip Install django==1.3

After a few minutes, the bug fix, OK, now the new Project C development, and then repeat the above story.
Well, this is still the ideal situation. The most undesirable scenario is that the Django-based third-party dependency is also related to the Django version, so in addition to the install and uninstall Django, but also uninstall and install its dependencies, ORZ, this special is embarrassing ...

What can virtualenv do?

Virtualenv can build a virtual and independent Python runtime environment, allowing individual projects to run in a separate environment from other projects. It can also be used to create multiple standalone python environments on a single machine, and Virtualenvwrapper provides some handy packages on the command line for the former.
Virtualenv is a very good virtual Python environment builder, and his greatest benefit is that it allows each Python project to use one environment alone without affecting the Python system environment or the environment of other projects.
Virtualenv can be used to create a standalone Python environment in which different Python versions or different packages are available, and you can install new packages in the environment without root privileges, with no effect on each other.

Anyway

installation,virtualenv is essentially a Python package, installed using PIP:

Pip Install Virtualenv

Create a virtual environment under working directory (default in current directory): Note the need to customize the name of the virtual environment!

~ $virtualenv testenvnew python executable in ~/testenv/bin/pythoninstalling setuptools, Pip, Wheel...done.

By default, the site-packages of the system is not included in the virtual environment, to use Add parameters :

Syntax: virtualenv--system-site-packages testenv

Create a virtual environment using the virtualenv default Python version

Syntax: virtualenv--no-site-packages ubuntu_env

You can create an env directory (the virtual environment name, which is the Python virtual environment) in the current directory, and you will notice that virtualenv will install Python,setuptools and Pip on you.

Customizing the Python version to create a virtual environment
1. Install a python that requires a version
2. Specify the python version in virtualenv
Virtualenv--no-site-packages--python=2.7 Env

Note:
1. Before creating the VIRTUALENV virtual environment, the corresponding version of Python must be installed on the system, and the current virtual environment will be invalid after uninstallation. Both Python2 and Python3 can be present in the system, with the system variable path (not the user variable) in the environment variable controlling the CMD or which version of Python is used in the system, which version of the path is preferred in the preceding version.
2.–no-site-packages indicates that the system global Python installation package is not included, which makes the environment cleaner
3.–python=python2.7 specifies that the version of Python is not already installed by the system Python2.7
4. Env is the established virtual environment name
5. The executable python does notexist error occurs without installing python2.7 or using the command virtualenv--no-site-packages--python=python2.7 env

Attention:

The location of the installed libraries is in the env/lib/site-packages/directory of the virtual environment, not in the Python lib/site-packages directory of the system, so you know why the virtual environment is separate.
The virtual environment created by Note:virtualenv is completely unrelated to the host's Python environment, and your host-configured libraries cannot be used directly in virtualenv. You need to install the configuration again using Pip install in a virtual environment before you can use it.

About creating a virtual environment, you can do the same!

1. Create a virtual environment for a project: $ cd my_project_dir$ virtualenv venv #venv为虚拟环境目录名, directory name customization virtualenv venv will create a folder in the current directory. Contains the Python executable and a copy of the PIP library so that the other packages can be installed.     the name of the virtual environment (in this case, venv) can be arbitrary, and omitting the name will place the file in the current directory. In any directory where you run the command, this creates a copy of Python and puts it in a file called Venv. 2. You can choose to use a Python interpreter: $ virtualenv-p/usr/bin/python2.7 venv #-P parameter specifies the Python interpreter program path This will use the Python interpreter in/usr/bin/python2.7.

Virtual Environment Activation , you need to activate the created virtual environment if you want to use it!
Enter the virtual environment directory to execute the source. /bin/activate Activating a virtual environment:

# relative Path mode:
~/testenv$ source Bin/activate (testenv) ~/testenv$ Python-vpython 2.7.11+

# absolute Path mode $ source Venv/bin/activate # Absolute path

From now on, any package you install with PIP will be placed in the Venv folder, isolated from the globally installed Python. Install packages as usual, such as:

$ pip Install requests

Use Requirements.txt to install a version package (requirements.txt files are saved with the version information of each dependent package )

Pip Install-r requirements.txt

Enter the environment Env1, execute PIP freeze > Requirements.txt to save the package dependency information in the Requirements.txt file.
It is best to manually adjust the order, such as NumPy and scipy to be installed in front of matplotlib, in addition, if you want to install the latest version, then the later version number ==1.9.1 what to delete.
Then enter the destination virtual environment env2, execute PIP install-r requirements.txt,pip will automatically download and install all the packages from the Internet.
Virtual Environment Env2 If it is a copy of ENV1, it is best to first pip Uninstall-ry requirements.txt, then pip Install-r requirements.txt

If you have temporarily completed your work in a virtual environment, you can deactivate it:

Exit the virtual Environment:

# in the environment, relative exit
(testenv) ~/testenv$ deactivate~/testenv$

#走绝对路径
$. Venv/bin/deactivate

This will return to the system's default Python interpreter, which will also return to the default, including the installed libraries.

to delete a virtual environment, simply delete its folder. (Perform RM-RF venv).
Virtualenv here is a little inconvenient, because virtual start, stop scripts are in a specific folder, perhaps after some time, you may have a lot of virtual environment scattered throughout the system, you may forget their name or location.

Virtualenvwrapper

Since virtualenv does not facilitate centralized management of virtual environments, it is recommended to use Virtualenvwrapper directly. Virtualenvwrapper provides a series of commands that make it easy to work with virtual environments. It puts all your virtual environments in one place.
1. Install Virtualenvwrapper (make sure virtualenv is installed)

Pip Install Virtualenvwrapperpip install Virtualenvwrapper-win #Windows使用该命令

2. After the installation is complete, write the following in ~/.BASHRC

Export workon_home=~/envssource/usr/local/bin/virtualenvwrapper.sh

First line: Virtualenvwrapper store the Virtual environment directory
Second line: Virtrualenvwrapper will be installed in the Python bin directory, so the path is under the Python installation directory bin/virtualenvwrapper.sh

SOURCE ~/.BASHRC #读入配置文件, effective immediately

Virtualenvwrapper Basic Use

1. Create a virtual environment mkvirtualenv

Mkvirtualenv venv

This creates a new virtual environment named Venv under the directory specified by the Workon_home variable.
If you want to specify a python version, you can specify the Python interpreter through "--python"

Mkvirtualenv--python=/usr/local/python3.5.3/bin/python venv

2. Basic commands
View the current virtual environment directory

[Email protected] ~]# Workonpy2py3

Switch to a virtual environment

Exiting a virtual environment

Deleting a virtual environment

Rmvirtualenv venv
Automatically establish virtual environments using Pycharm

Virtual environments can also be built using the Pycharm feature

After that, put the Requirements.txt file in the virtual directory venv, Pycharm automatically identify, click "Install Requirements" to install the corresponding package.

or use:

(venv) $ pip freeze >requirements.txt    # Build (venv) $ pip install-r requirements.txt    # Install 
       

Next we say virtualenv, English better classmate, probably already guessed half, virtual, namely: fictitious. What the hell is env? Environment? Yes, it is! It's it! That's it! So translating into Chinese is a "virtual environment".

Slow, master, first step on the foot, what is the virtual environment? as the name implies, it is a virtual environment. In layman's terms, virtual machines, Docker, can be used to understand the virtual environment, is to separate part of the content, we call this part of the independent thing called "container", in this container, we can only install the dependent packages we need, and the various containers are isolated from each other, do not affect each other. For example, this study needs to use the flask, so we can do a flask virtual environment, which only need to install flask related packages can be. For example, next time we're going to learn Django, we'll have a Django virtual environment.

Wait, master, I don't seem to understand. Let's have another chestnut.

"Prerequisite Summary"
Django is also a popular web framework that is much more complex than flask. Django iterations are updated very quickly and frequently, so there are some obsolete things that need to be discarded, and something new needs to be added, resulting in incompatibilities between different versions. such as Django1.3, Django1.4, Django1.8 There is a great difference between.

"Story Background"
Suppose you want to do Python web development, using Django. There are two old items on hand, A and B need maintenance, and new Project C is under development. Here project a uses django1.3, Project B uses django1.4, and the new Project C uses Django1.8. So the question is, how do you develop and maintain ABC three projects at the same time?

The normal pattern might be this: now there is a bug on item A that needs to be repaired, so first execute the following command to remove the original version:

Pip Uninstall Django

Then execute the following command to install django1.3

Pip Install django==1.3

After a few minutes, the bug fix, OK, now the new Project C development, and then repeat the above story.

Well, this is still the ideal situation. What the? Is there a situation that is not ideal? Yes, Django-based third-party dependencies are also related to the Django version, so in addition to the install and uninstall Django, but also uninstall and install its dependencies, ORZ, this special is embarrassing ...

OK, can start the virtual environment of the journey, with the tight, students.


Navcat
Links: Http://www.imooc.com/article/18537?block_id=tuijian_wz
Source: MU-Class Network
This article original published in Mu class network, reproduced please indicate the source, thank you for your cooperation!

Next we say virtualenv, English better classmate, probably already guessed half, virtual, namely: fictitious. What the hell is env? Environment? Yes, it is! It's it! That's it! So translating into Chinese is a "virtual environment".

Slow, master, first step on the foot, what is the virtual environment? as the name implies, it is a virtual environment. In layman's terms, virtual machines, Docker, can be used to understand the virtual environment, is to separate part of the content, we call this part of the independent thing called "container", in this container, we can only install the dependent packages we need, and the various containers are isolated from each other, do not affect each other. For example, this study needs to use the flask, so we can do a flask virtual environment, which only need to install flask related packages can be. For example, next time we're going to learn Django, we'll have a Django virtual environment.


Navcat
Links: Http://www.imooc.com/article/18537?block_id=tuijian_wz
Source: MU-Class Network
This article original published in Mu class network, reproduced please indicate the source, thank you for your cooperation!

Next we say virtualenv, English better classmate, probably already guessed half, virtual, namely: fictitious. What the hell is env? Environment? Yes, it is! It's it! That's it! So translating into Chinese is a "virtual environment".

Slow, master, first step on the foot, what is the virtual environment? as the name implies, it is a virtual environment. In layman's terms, virtual machines, Docker, can be used to understand the virtual environment, is to separate part of the content, we call this part of the independent thing called "container", in this container, we can only install the dependent packages we need, and the various containers are isolated from each other, do not affect each other. For example, this study needs to use the flask, so we can do a flask virtual environment, which only need to install flask related packages can be. For example, next time we're going to learn Django, we'll have a Django virtual environment.


Navcat
Links: Http://www.imooc.com/article/18537?block_id=tuijian_wz
Source: MU-Class Network
This article original published in Mu class network, reproduced please indicate the source, thank you for your cooperation!

Python Virtual Environment virtualenv

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.