python虚拟环境配置(Python virtual environment configuration)

Python为什么要使用虚拟环境-Python虚拟环境的安装和配置-virtualenv

第一段代码 2018-07-17 12:25:26 101949 收藏 629分类专栏: Python开发/爬虫 文章标签: Python 虚拟环境版权

Python开发/爬虫专栏收录该内容37 篇文章5 订阅订阅专栏一 虚拟环境 virtual environment

它是一个虚拟化,从电脑独立开辟出来的环境。通俗的来讲,虚拟环境就是借助虚拟机docker来把一部分内容独立出来,我们把这部分独立出来的东西称作“容器”,在这个容器中,我们可以只安装我们需要的依赖包,各个容器之间互相隔离,互不影响。譬如,本次学习需要用到Django,我们可以做一个Django的虚拟环境,里面只需要安装Django相关包就可以了,需要Scrapy库,就在开辟一个独立空间来学习Scrapy库相关就行了。

二 前言:为什么要用虚拟环境

在实际项目开发中,我们通常会根据自己的需求去下载各种相应的框架库,如Scrapy、Beautiful Soup等,但是可能每个项目使用的框架库并不一样,或使用框架的版本不一样,这样需要我们根据需求不断的更新或卸载相应的库。直接怼我们的Python环境操作会让我们的开发环境和项目造成很多不必要的麻烦,管理也相当混乱。如一下场景:

场景1:项目A需要某个框架1.0版本,项目B需要这个库的2.0版本。如果没有安装虚拟环境,那么当你使用这两个项目时,你就需要 来回 的卸载安装了,这样很容易就给你的项目带来莫名的错误;

场景2:公司之前的项目需要python2.7环境下运行,而你接手的项目需要在python3环境中运行,想想就应该知道,如果不使用虚拟环境,这这两个项目可能无法同时使用,使用python3则公司之前的项目可能无法运行,反正则新项目运行有麻烦。而如果虚拟环境可以分别为这两个项目配置不同的运行环境,这样两个项目就可以同时运行。

Tips:其实虚拟环境好处也确实比较多,会给我们项目的开发带来许多的好处,但是初学者,建议还是不要这么折腾,我们的首要目的是更快的掌握更多的知识,研究virtualenv会花费一些额外的经历,而且意志不强的同学很容易遭受打击,但是这个优点我们还是要记下来的方便以后要用的时候能很快的想起。

三 虚拟环境的安装和使用

本部分将通过命令给出搭建虚拟环境的过程和常见的使用方法:

1.前提必须安装好python环境,并检查电脑系统环境path是否有python路径,

2.安装虚拟环境

打开windows命令终端(cmd)安装虚拟环境 virtualenv(如果你执行了上面查看python版本的语句,那么要先使用exit()方法先退出)

pip3 install virtualenv

它会默认安装到你python库文件lib中,可以看安装完成的地址,里面给出了安装之后的库地址(上面是说我已经安装了,一般第一次安装任何库,看见sucessful就说明成了)

3.创建虚拟环境

通过上面的步骤安装成功之后,我们就可以创建虚拟环境了:virtualenv 虚拟环境名

这个命令创建虚拟环境,会在当前所在目录进行创建,如C:\Users\Smalu(电脑管理者路径)

virtualenv envname

4.进入虚拟环境

先要进入cd到虚拟环境的位置(目录)的Scripts中,然后在激活(activate.bat)虚拟环境,则进入新建的虚拟环境中了。

cd scrapy3env\Scriptsactivate.bat

进入之后就可以通过pip3 命令安装需要各种框架依赖包了

5.退出虚拟环境 deactivate.bat

deactivate.bat

四 虚拟环境的配置

通过上面的步骤其实我们就已经完成虚拟环境virtualenv的安装和使用了,但是认真的你肯定发现了上面需要记住每一个虚拟环境的目录,才能进入虚拟环境并操作,很麻烦,下面我们通过另一个配置来简化我们的使用

1.安装 virtualenvwrapper-win

pip3 install virtualenvwrapper-win

2.然后使用workon查看是否安装成功(workon是简化操作的神级命令之一,下面你就可以体会到)

(出现这个,这说明安装成功)、

3.通过新方法新建我们的虚拟环境:mkvirtualenv 虚拟环境名

mkvirtualenv py3scrapy

4.配置。给你虚拟环境安装目录设置一个专门(你想放)的目录

通过上面的步骤,创建虚拟环境,默认放在C:\Users\电脑用户名\Envs目录中

这样可能有时候不满足我们的需求,比如我们想把项目放在其他盘(或其他位置),这样就需要我们自己配置一下环境

4.1 新建要存放大文件夹:如 D:\codeworkpace\python\envs

4.2 配置电脑系统环境变量,->新建系统变量 -> 变量名:WORKON_HOME ->指定路径:D:\codeworkpace\python\envs

通过设置WORKON_HOME路径,就给我们的python虚拟环境指定了一个存放位置:

再次运行workon,目录中没有虚拟环境了,因为默认目录已经改变

那么我们可以将之前的虚拟环境的项目拷贝到新建目录下

再次运行workon,就可以看到该目录下所有的虚拟空间了

5 进入(使用)某个虚拟空间:workon 虚拟空间名

workon #列出所以目录下的空间名workon py3entest #使用名为py3entest的虚拟空间 #新建虚拟空间的方法依然是:mkvirtualenvs 空间名

6 退出虚拟空间 deactivate

deactivate

总结:配置之后的常用命令

列出虚拟环境列表:workon 新建虚拟环境:mkvirtualenv [虚拟环境名称] ->应该就是make的简写方便理解

https://blog.csdn.net/godot06/article/details/81079064 windows安装python虚拟环境

启动/切换虚拟环境:workon [虚拟环境名称] 离开虚拟环境:deactivate

C:\Users\Administrator>workon django01(django01) C:\Users\Administrator>pip install django==1.11.11Collecting django==1.11.11Downloading Django-1.11.11-py2.py3-none-any.whl (6.9 MB)|████████████████▌ | 3.6 MB 18 kB/s eta 0:03:04

————————

Why should Python use virtual environment – installation and configuration of Python virtual environment – virtualenv

The first code 2018-07-17 12:25:26 101949 collection 629 classification column: Python development / crawler article label: Python virtual environment copyright

The python development / crawler column contains 37 articles. 5 subscribe to the virtual environment

It is a virtualized and independent environment from computers. Generally speaking, the virtual environment is to use the virtual machine docker to separate part of the content. We call this part of the independent thing “container”. In this container, we can only install the dependent packages we need, and each container is isolated from each other without affecting each other. For example, Django needs to be used in this study. We can make a virtual environment of Django. We only need to install Django related packages. If we need scripy library, we can open up an independent space to learn about scripy library.

Why use virtual environment

In actual project development, we usually download various corresponding framework libraries according to our own needs, such as scripy, beautiful soup, etc., but the framework libraries used in each project may be different, or the versions of the frameworks used may be different, so we need to constantly update or uninstall the corresponding libraries according to our needs. Directly connecting with our Python environment will cause a lot of unnecessary trouble and confusion in our development environment and project management. The following scenario is shown:

Scenario 1: project a needs a framework version 1.0, and project B needs a library version 2.0. If the virtual environment is not installed, you need to uninstall and install back and forth when you use these two projects, which can easily bring inexplicable errors to your project;

Scenario 2: previous projects of the company require Python 2 7 environment, and the project you took over needs to be run in the python 3 environment. Think about it. If you don’t use the virtual environment, these two projects may not be used at the same time. If you use Python 3, the company’s previous projects may not be able to run. Anyway, the operation of new projects is troublesome. If the virtual environment can configure different running environments for the two projects, the two projects can run at the same time.

Tips: in fact, virtual environment does have many benefits, which will bring many benefits to the development of our project. However, for beginners, it is recommended not to do so. Our primary purpose is to master more knowledge faster. Studying virtualenv will cost some additional experience, and students with weak will are easy to be hit, But we still have to write down this advantage. It’s convenient to remember it quickly when we want to use it in the future.

III. installation and use of virtual environment

This part will give the process of building a virtual environment and common usage methods through commands:

1. The prerequisite is to install the python environment and check whether there is a python path in the computer system environment path,

2. Install virtual environment

Open the windows command terminal (CMD) and install the virtual environment virtualenv (if you execute the above statement to view the python version, you should use the exit () method to exit first)

pip3 install virtualenv

It will be installed into your Python library file lib by default. You can see the address after installation, and the library address after installation is given in it (the above says that I have installed any library for the first time, and it will be explained when I see sucessful)

3. Create a virtual environment

After the installation is successful through the above steps, we can create the virtual environment: virtualenv virtual environment name

This command creates a virtual environment in the current directory, such as C: \ users \ smalu (computer administrator path)

virtualenv envname

4. Enter the virtual environment

First enter the scripts of CD to the location (directory) of the virtual environment, and then activate (activate. Bat) the virtual environment to enter the new virtual environment.

cd scrapy3env\Scriptsactivate.bat

After entering, you can install various framework dependent packages through PIP3 command

5. Exit the virtual environment deactivate bat

deactivate.bat

IV. configuration of virtual environment

In fact, through the above steps, we have completed the installation and use of the virtual environment virtualenv, but seriously, you must find that we need to remember the directory of each virtual environment before we can enter the virtual environment and operate. It’s very troublesome. Let’s simplify our use through another configuration

1.安装 virtualenvwrapper-win

pip3 install virtualenvwrapper-win

2. Then use workon to check whether the installation is successful (workon is one of the divine commands to simplify the operation, as you can see below)

(this indicates that the installation is successful.)

3. Create a new virtual environment through a new method: mkvirtualenv virtual environment name

mkvirtualenv py3scrapy

4. Configuration. Set a special (you want to put) directory for your virtual environment installation directory

Through the above steps, create a virtual environment, which is placed in the C: \ users \ computer user name \ envs directory by default

This may not meet our needs sometimes. For example, we want to put the project on other disks (or other locations), so we need to configure the environment ourselves

4.1 create an enlarged folder to save: for example, D: \ codeworkspace \ Python \ envs

4.2 configure computer system environment variables, – & gt; New system variable – & gt; Variable name: workon_ HOME -> Specified path: D: \ codeworkspace \ Python \ envs

By setting workon_ The home path specifies a storage location for our Python virtual environment:

Run workon again, and there is no virtual environment in the directory, because the default directory has changed

Then we can copy the previous virtual environment projects to the new directory

Run workon again, and you can see all the virtual spaces in the directory

5 enter (use) a virtual space: workon virtual space name

Workon # lists the space name under the directory workon py3entest # use the virtual space named py3entest # the method of creating a new virtual space is still mkvirtualenvs space name

6 exit virtual space deactivate

deactivate

Summary: common commands after configuration

List virtual environments: workon create a new virtual environment: mkvirtualenv [virtual environment name] – [gt; It should be the abbreviation of make, which is easy to understand

https://blog.csdn.net/godot06/article/details/81079064 windows安装python虚拟环境

Start / switch virtual environment: workon [virtual environment name] leave virtual environment: deactivate

C:\Users\Administrator>workon django01(django01) C:\Users\Administrator>pip install django==1.11.11Collecting django==1.11.11Downloading Django-1.11.11-py2.py3-none-any.whl (6.9 MB)|████████████████▌ | 3.6 MB 18 kB/s eta 0:03:04