Use of Python's Virtualenv (Virtual Environment) (Windows)

 Reference is transferred from: http://www.seoerzone.com/article/virtualenv-for-python/

 

Virtualenv (virtualenviroment) aims to create a Python virtual independent environment, independent of other Python project environments.

Windows articles

Installation of Virtualenv

 

The installation of virtualenv requires the use of pip ( without pip , you can refer to the pip installation tutorial )

 

Command: pip installvirtualenv

 

After the run is complete, run virtualenv --version


Indicates that the installation was successful.

 

 

Virtual env use

 

You can use the virtualenv -h command to view the help documentation.

 

Here is a brief introduction:

First create a new working folder in a folder on your PC , and then cd to that directory

NOTE : There is no WORKON_HOME in the computer environment variable. If there is, please switch the directory to the directory specified by the environment variable.

Then use the command: virtualenv myvirtual

 


At this point , the my virtual virtual environment is successfully created, and there should be a myvirtual folder in the current directory.

The execution command is as follows:

1cd myvirtual\Scripts

2activate


After successful execution, you will see a ( myvirtual ) at the beginning of the command line

 

Note : The command deactivate is to exit the virtual environment

The default python environment of my machine at this time is python2.7, then the virtual environment created at this time is the virtualized environment created by python2.7. If you need to select a python interpreter to create a virtualized environment, the command is:

 

virtualenv -p /usr/bin/python3.6myenv

 

Virtualenv manager - Virtualenvwrapper

 

The manager can effectively and quickly manage virtual

 

virtualenvwrapper的安装

1、命令:pip installvirtualenvwrapper-win


安装成功。

2、配置虚拟环境安装目录的环境变量。

在环境变量里新建WOEKON_HOME如下图:


这里变量值根据自己PC自己随意选择,以后新建的虚拟环境文件夹是配置在该目录下。

 

virtualenvwrapper的使用

输入virtualenvwrapper回车就会有帮助文档出来


这里举例几个命令

1mkvirtualenv myvir2

创建一个名字叫myvir2的虚拟环境

2rmvirtualenv myvir2

删除名字叫myvir2的虚拟环境

3workon

单独使用这个命令会列出所有的虚拟环境和lsvirtualenv是相同的,当跟上虚拟环境名称后就会切换到该虚拟环境。

4mkproject 工程目录名

这个命令需要我们添加PROJECT_HOME环境变量,然后才可以使用。

改命令会在在前面创建的PROJECT_HOME的指定文件夹里创建一个工程目录,同时会创建一个与工程目录同名的虚拟空间。


Ps:感觉这个命令就是为了解决不同Python工程但需要相同环境但又不想重新创建环境问题所以设计的。知道的大神可以补充一下。

还不清楚怎么用?

如果上面配置好了之后还不知道怎么使用的话?请按照以下三步:

Step1、新建虚拟环境

Step2、安装Python所需要的包,编辑自己的代码调试运行。

Step3、写好了之后退出虚拟环境。

 

虚拟环境的复制

一、首先收集原环境的安装包信息。

在虚拟环境中使用命令:

pip freeze >e:\fileofinstalled.txt

 

NOTES:

  1. 文件生成以后,有时候需要调整安装包的顺序,例如一安装包需要依赖另一个安装包,这时候就要调整两个安装包的位置。
  2. 如果想要安装某个包的最新版,只需要把 ==”后面的版本信息删除即可。

 

二、拷贝信息到新的环境

首先新建一个虚拟环境,然后切换到新的虚拟环境,然后执行以下命令:

pip install -r  e:\fileofinstalled.txt

 

这样就完成了环境的拷贝,我感觉与其这样操作还不如使用上面的mkproject命令。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325730486&siteId=291194637