前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python测试开发01-虚拟环境搭建

python测试开发01-虚拟环境搭建

作者头像
wencheng
发布2020-11-03 15:45:44
6653
发布2020-11-03 15:45:44
举报

一、虚拟环境搭建.

一、虚拟环境介绍

Python虚拟环境的主要目的是为了给不同的工程创建相互独立的运行环境。在虚拟环境下,每一个工程都有自己独立的依赖包,而与其它的工程无关。不同的虚拟环境中同一个包可以有不同的版本,并且,虚拟环境的数量没有限制,我们可以轻松的地用virtualenv或者pipenv等工具来创建多个虚拟环境。

.

二、virtualenv的安装和应用

(windows)安装步骤:

1.创建一个存放虚拟环境的目录例如(.virtualenv)

2.pip 安装 virtualenv

3.pip 安装虚拟环境的管理包virtualenvwrapper-win

4.配置环境变量(变量名:WORKON_HOME值:上面的创建目录路径)

5.进入cmd下输入workon出现如下图说明安装成功了。

.

virtualenv的使用命令

虚拟命令的一些命令:

wokon列出所有的虚拟环境

workon 【name】 进入指定的虚拟环境

deactivate 退出当前的虚拟环境

mkvirtualenv 【name】创建虚拟环境

rmvirtualenv 【name】删除虚拟环境

包安装:虚拟环境下安装对应的依赖包,进入虚拟环境直接使用pip install 进行安装

.

pipenv的安装和使用

pipenv集成了pip,virtualenv两者的功能,且完善了两者的一些缺陷。

过去使用virtualenv管理requirements.txt文件可能会有问题,pipenv使用pipfile和pipfile.lock,前者用来记录项目依赖包列表,后者记录了固定版本的详细依赖列表,查看依赖关系是十分方便。支持python2、python3,mac、win、linux在各个平台的命令一样的。

安装pip install pipenv

创建虚拟环境

第一步创建文件夹 mkdir py3env

第二步进入文件夹 cd py3env

第三步初始化虚拟环境 pipenv install

Pipfile.lock not found, creating…

Locking [dev-packages] dependencies…

Locking [packages] dependencies…

Updated Pipfile.lock (ca72e7)!

Installing dependencies from Pipfile.lock (ca72e7)…

================================ 0/0 - 00:00:00

To activate this project's virtualenv, run pipenv shell.

Alternatively, run a command inside the virtualenv with pipenv run.

D:\workspace\python01>

Using c:\python3.6\python.exe (3.6.7) to create virtualenv…

created virtual environment CPython3.6.7.candidate.2-64 in 1383ms

creator CPython3Windows(dest=C:\Users\hewencheng\.virtualenvs\python01-4K2rx9Jw, clear=False, global=False)

seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\hewencheng\AppData\Local\pypa\virtualenv)

added seed packages: pip==20.2.4, setuptools==50.3.2, wheel==0.35.1

activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

Virtualenv location: C:\Users\hewencheng\.virtualenvs\python01-4K2rx9Jw

Creating a Pipfile for this project…

Pipfile.lock not found, creating…

Locking [dev-packages] dependencies…

Locking [packages] dependencies…

Updated Pipfile.lock (ca72e7)!

Installing dependencies from Pipfile.lock (ca72e7)…

================================ 0/0 - 00:00:00

To activate this project's virtualenv, run pipenv shell.

Alternatively, run a command inside the virtualenv with pipenv run.

D:\workspace\python01>

Using c:\python3.6\python.exe (3.6.7) to create virtualenv…

created virtual environment CPython3.6.7.candidate.2-64 in 1383ms

creator CPython3Windows(dest=C:\Users\hewencheng\.virtualenvs\python01-4K2rx9Jw, clear=False, global=False)

seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\hewencheng\AppData\Local\pypa\virtualenv)

added seed packages: pip==20.2.4, setuptools==50.3.2, wheel==0.35.1

activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

Virtualenv location: C:\Users\hewencheng\.virtualenvs\python01-4K2rx9Jw

Creating a Pipfile for this project…

Pipfile.lock not found, creating…

Locking [dev-packages] dependencies…

Locking [packages] dependencies…

Updated Pipfile.lock (ca72e7)!

Installing dependencies from Pipfile.lock (ca72e7)…

================================ 0/0 - 00:00:00

To activate this project's virtualenv, run pipenv shell.

Alternatively, run a command inside the virtualenv with pipenv run.

D:\workspace\python01>

创建好虚拟环境后生成:一个Pipfile和pipfile.lock文件

第四步用pipenv install requests包

Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a2/38/928ddce2273eaa564f6f50de919327bf3a00f091b5baba8dfa9460f3a8a8/idna-2.10-py2.py3-none-any.whl (58 k

B)

Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1

Downloading https://pypi.tuna.tsinghua.edu.cn/packages/56/aa/4ef5aa67a9a62505db124a5cb5262332d1d4153462eb8fd89c9fa41e5d92/urllib3-1.25.11-py2.py3-none-any.whl

(127 kB)

Collecting certifi>=2017.4.17

Downloading https://pypi.tuna.tsinghua.edu.cn/packages/5e/c4/6c4fe722df5343c33226f0b4e0bb042e4dc13483228b4718baf286f86d87/certifi-2020.6.20-py2.py3-none-any.w

hl (156 kB)

Installing collected packages: chardet, idna, urllib3, certifi, requests

Successfully installed certifi-2020.6.20 chardet-3.0.4 idna-2.10 requests-2.24.0 urllib3-1.25.11

Adding requests to Pipfile's [packages]…

Pipfile.lock (d89249) out of date, updating to (ca72e7)…

Locking [dev-packages] dependencies…

Locking [packages] dependencies…

Updated Pipfile.lock (d89249)!

Installing dependencies from Pipfile.lock (d89249)…

================================ 5/5 - 00:00:02

To activate this project's virtualenv, run pipenv shell.

Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a2/38/928ddce2273eaa564f6f50de919327bf3a00f091b5baba8dfa9460f3a8a8/idna-2.10-py2.py3-none-any.whl (58 k

B)

Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1

Downloading https://pypi.tuna.tsinghua.edu.cn/packages/56/aa/4ef5aa67a9a62505db124a5cb5262332d1d4153462eb8fd89c9fa41e5d92/urllib3-1.25.11-py2.py3-none-any.whl

(127 kB)

Collecting certifi>=2017.4.17

Downloading https://pypi.tuna.tsinghua.edu.cn/packages/5e/c4/6c4fe722df5343c33226f0b4e0bb042e4dc13483228b4718baf286f86d87/certifi-2020.6.20-py2.py3-none-any.w

hl (156 kB)

Installing collected packages: chardet, idna, urllib3, certifi, requests

Successfully installed certifi-2020.6.20 chardet-3.0.4 idna-2.10 requests-2.24.0 urllib3-1.25.11

Adding requests to Pipfile's [packages]…

Pipfile.lock (d89249) out of date, updating to (ca72e7)…

Locking [dev-packages] dependencies…

Locking [packages] dependencies…

Updated Pipfile.lock (d89249)!

Installing dependencies from Pipfile.lock (d89249)…

================================ 5/5 - 00:00:02

To activate this project's virtualenv, run pipenv shell.

Alternatively, run a command inside the virtualenv with pipenv run.

安装成功

使用pipenv环境迁移

拷贝pipfile和pipfile.lock文件到迁移的路径下使用命令pipenv install迁移即可。使用pipenv graph查看包

D:\workspace\python01\test1>pipenv graph

flash==1.0.3

requests==2.24.0

- certifi [required: >=2017.4.17, installed: 2020.6.20]

- chardet [required: >=3.0.2,<4, installed: 3.0.4]

- idna [required: >=2.5,<3, installed: 2.10]

- urllib3 [required: >=1.21.1,<1.26,!=1.25.1,!=1.25.0, installed: 1.25.11]

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家的支持。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-10-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 自动化测试 To share 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档