python2.7 virtualenv创建虚拟环境 以及 安装pytest

1.安装virtualenv及创建虚拟环境

由于工作需要使用python2.7版本,因此在在安装virtualenv时需要注意版本问题

1.1 安装virtualenv

首先,使用conda search查找对应python2.7兼容版本

$ conda search virtualenv

Loading channels: done
# Name                       Version           Build  Channel             
virtualenv                    15.2.0          py27_0  pkgs/main           
virtualenv                    15.2.0          py35_0  pkgs/main           
virtualenv                    15.2.0          py36_0  pkgs/main           
virtualenv                    16.0.0          py27_0  pkgs/main           
virtualenv                    16.0.0          py35_0  pkgs/main           
virtualenv                    16.0.0          py36_0  pkgs/main           
virtualenv                    16.0.0          py37_0  pkgs/main           
virtualenv                    16.0.0          py38_0  pkgs/main           
virtualenv                    16.7.5            py_0  pkgs/main           
virtualenv                   20.0.20          py36_1  pkgs/main           
virtualenv                   20.0.20          py37_1  pkgs/main  

我们可以看到virtualenv==15.2.0是支持python2.7的,因此我们安装这个版本

conda install virtualenv==15.2.0

1.2 创建虚拟环境并激活

(1)创建虚拟环境venv

$ virtualenv [虚拟环境名称] 
$ virtualenv venv

New python executable in ~/Desktop/baidu/bce-pcqa/vpc-console-test/vpc-smoke-test/venv/bin/python
copying ~/opt/anaconda3/envs/py2.7/bin/python => ~/Desktop/baidu/bce-pcqa/vpc-console-test/vpc-smoke-test/venv/bin/python
copying ~/opt/anaconda3/envs/py2.7/bin/../lib/libpython2.7.dylib => ~/Desktop/baidu/bce-pcqa/vpc-console-test/vpc-smoke-test/venv/lib/libpython2.7.dylib
Installing setuptools, pip, wheel... 


done.

(2)激活环境

$ cd venv
$ source ./bin/activate

激活之后,可以使用命令查看是否已经生效

$ which python
或者
$ which pip

(3)退出环境

$ deactivate

(4)删除环境

没有使用virtualenvwrapper前,可以直接删除venv文件夹来删除环境

2. 安装pytest

pytest官方文档:https://docs.pytest.org/en/stable/getting-started.html#our-first-test-run

在这里插入图片描述
上图说明,官网要求python为3.x版本

向下寻找,发现pytest 4.6.x版本支持python2.7

在这里插入图片描述
查找资源,发现pytest==4.6.2可满足

$ conda search pytest

Loading channels: done
# Name                       Version           Build  Channel             
pytest                         4.6.2          py27_0  pkgs/main           
pytest                         4.6.2          py36_0  pkgs/main           
pytest                         4.6.2          py37_0  pkgs/main           
pytest                        4.6.11          py36_0  pkgs/main           
pytest                        4.6.11          py37_0  pkgs/main           
pytest                        4.6.11          py38_0  pkgs/main      

安装pytest

使用虚拟环境venv中的pip安装pytest,方便测试包使用

# 激活虚拟环境venv
source venv/bin/activate

# 安装
pip install -U pytest==4.6.2

DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Collecting pytest==4.6.2
  WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)",)': /packages/cd/eb/04a30246424f5664a8fb72d982a57f60399642a63d24c7fdfaf70d673c27/pytest-4.6.2-py2.py3-none-any.whl
  Downloading pytest-4.6.2-py2.py3-none-any.whl (229 kB)
     |████████████████████████████████| 229 kB 14 kB/s 
Collecting funcsigs>=1.0; python_version < "3.0"
  Using cached funcsigs-1.0.2-py2.py3-none-any.whl (17 kB)
Collecting py>=1.5.0
  Downloading py-1.10.0-py2.py3-none-any.whl (97 kB)
     |████████████████████████████████| 97 kB 15 kB/s 
Collecting pluggy<1.0,>=0.12
  Downloading pluggy-0.13.1-py2.py3-none-any.whl (18 kB)
Collecting pathlib2>=2.2.0; python_version < "3.6"
  Downloading pathlib2-2.3.6-py2.py3-none-any.whl (17 kB)
Collecting six>=1.10.0
  Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting more-itertools<6.0.0,>=4.0.0; python_version <= "2.7"
  Downloading more_itertools-5.0.0-py2-none-any.whl (52 kB)
     |████████████████████████████████| 52 kB 18 kB/s 
Collecting packaging
  Downloading packaging-20.9-py2.py3-none-any.whl (40 kB)
     |████████████████████████████████| 40 kB 16 kB/s 
Collecting atomicwrites>=1.0
  Downloading atomicwrites-1.4.0-py2.py3-none-any.whl (6.8 kB)
Collecting attrs>=17.4.0
  Downloading attrs-21.2.0-py2.py3-none-any.whl (53 kB)
     |████████████████████████████████| 53 kB 20 kB/s 
Collecting wcwidth
  Downloading wcwidth-0.2.5-py2.py3-none-any.whl (30 kB)
Collecting importlib-metadata>=0.12
  Downloading importlib_metadata-2.1.1-py2.py3-none-any.whl (10 kB)
Collecting scandir; python_version < "3.5"
  Downloading scandir-1.10.0.tar.gz (33 kB)
Collecting pyparsing>=2.0.2
  Downloading pyparsing-2.4.7-py2.py3-none-any.whl (67 kB)
     |████████████████████████████████| 67 kB 12 kB/s 
Collecting backports.functools-lru-cache>=1.2.1; python_version < "3.2"
  Downloading backports.functools_lru_cache-1.6.4-py2.py3-none-any.whl (5.9 kB)
Collecting configparser>=3.5; python_version < "3"
  Downloading configparser-4.0.2-py2.py3-none-any.whl (22 kB)
Collecting zipp>=0.5
  Downloading zipp-1.2.0-py2.py3-none-any.whl (4.8 kB)
Collecting contextlib2; python_version < "3"
  Downloading contextlib2-0.6.0.post1-py2.py3-none-any.whl (9.8 kB)
Building wheels for collected packages: scandir
  Building wheel for scandir (setup.py) ... done
  Created wheel for scandir: filename=scandir-1.10.0-cp27-cp27m-macosx_10_7_x86_64.whl size=18727 sha256=c857f181befc4ca764e480ee9b0304afe0e94320b679e17f9493f27badf96e1c
  Stored in directory: ~/Library/Caches/pip/wheels/58/2c/26/52406f7d1f19bcc47a6fbd1037a5f293492f5cf1d58c539edb
Successfully built scandir
Installing collected packages: funcsigs, py, configparser, contextlib2, zipp, six, scandir, pathlib2, importlib-metadata, pluggy, more-itertools, pyparsing, packaging, atomicwrites, attrs, backports.functools-lru-cache, wcwidth, pytest
Successfully installed atomicwrites-1.4.0 attrs-21.2.0 backports.functools-lru-cache-1.6.4 configparser-4.0.2 contextlib2-0.6.0.post1 funcsigs-1.0.2 importlib-metadata-2.1.1 more-itertools-5.0.0 packaging-20.9 pathlib2-2.3.6 pluggy-0.13.1 py-1.10.0 pyparsing-2.4.7 pytest-4.6.2 scandir-1.10.0 six-1.16.0 wcwidth-0.2.5 zipp-1.2.0
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值