HttpRunner ubuntu16.04.3 python 多版本改造与 HttpRunner 部署

老马 · 2017年12月22日 · 最后由 假精哟 回复于 2018年08月29日 · 2057 次阅读

问题:

HttpRunner supports Python 2.7, 3.4, 3.5, and 3.6. And we strongly recommend you to use Python 3.6.

官方强烈建议使用 Python 3.6 。

当我用 ubuntu16.04.3 默认支持的 python3.5.2 的 pip3 安装 httprunner 后

cmd@TR:~$ httprunner -V
httprunner:未找到命令

尝试添加 python3.5 的 PATH 也是无果

详见:https://github.com/HttpRunner/HttpRunner/issues/68

当然你可以有以下两种选择:

1 编译安装 python3.6 到本地某一目录 然后添加 PATH

2 利用 update-alternatives --list python 来切换系统默认 python 版本

但这两种方案 都很麻烦,且对系统自带的 py2 py3 会有影响,主要是因为 ubuntu 在升级程序 apt upgrade 的时候,有的程序依赖 py2 或 py3 如果你更改默认 py 版本,会导致升级程序出错。

我的 ubuntu16.04.3 的相关系统 python 版本如下:

cmd@TR:~$ python -V

Python 2.7.12

cmd@TR:~$ python2 -V

Python 2.7.12

cmd@TR:~$ python3 -V

Python 3.5.2



cmd@TR:~$ whereis python

python: /usr/bin/python /usr/bin/python3.5m /usr/bin/python3.5m-config /usr/bin/python2.7-config /usr/bin/python2.7 /usr/bin/python3.5-config /usr/bin/python3.5 /usr/lib/python2.7 /usr/lib/python3.5 /usr/lib/python3.6 /etc/python /etc/python2.7 /etc/python3.5 /usr/local/lib/python2.7 /usr/local/lib/python3.5 /usr/include/python3.5m /usr/include/python2.7 /usr/include/python3.5 /usr/share/python /usr/share/man/man1/python.1.gz

cmd@TR:~$ whereis python2

python2: /usr/bin/python2.7-config /usr/bin/python2 /usr/bin/python2.7 /usr/lib/python2.7 /etc/python2.7 /usr/local/lib/python2.7 /usr/include/python2.7 /usr/share/man/man1/python2.1.gz

cmd@TR:~$ whereis python3

python3: /usr/bin/python3 /usr/bin/python3.5m /usr/bin/python3.5m-config /usr/bin/python3.5-config /usr/bin/python3.5 /usr/lib/python3 /usr/lib/python3.5 /usr/lib/python3.6 /etc/python3 /etc/python3.5 /usr/local/lib/python3.5 /usr/include/python3.5m /usr/include/python3.5 /usr/share/python3 /usr/share/man/man1/python3.1.gz





cmd@TR:~$ python -m site

sys.path = [

    '/home/cmd',

    '/usr/lib/python2.7',

    '/usr/lib/python2.7/plat-x86_64-linux-gnu',

    '/usr/lib/python2.7/lib-tk',

    '/usr/lib/python2.7/lib-old',

    '/usr/lib/python2.7/lib-dynload',

    '/usr/local/lib/python2.7/dist-packages',

    '/usr/lib/python2.7/dist-packages',

    '/usr/lib/python2.7/dist-packages/gtk-2.0',

    '/usr/lib/python2.7/dist-packages/wx-3.0-gtk2',

]

USER_BASE: '/home/cmd/.local' (exists)

USER_SITE: '/home/cmd/.local/lib/python2.7/site-packages' (doesn't exist)

ENABLE_USER_SITE: True



cmd@TR:~$ python2 -m site

sys.path = [

    '/home/cmd',

    '/usr/lib/python2.7',

    '/usr/lib/python2.7/plat-x86_64-linux-gnu',

    '/usr/lib/python2.7/lib-tk',

    '/usr/lib/python2.7/lib-old',

    '/usr/lib/python2.7/lib-dynload',

    '/usr/local/lib/python2.7/dist-packages',

    '/usr/lib/python2.7/dist-packages',

    '/usr/lib/python2.7/dist-packages/gtk-2.0',

    '/usr/lib/python2.7/dist-packages/wx-3.0-gtk2',

]

USER_BASE: '/home/cmd/.local' (exists)

USER_SITE: '/home/cmd/.local/lib/python2.7/site-packages' (doesn't exist)

ENABLE_USER_SITE: True



cmd@TR:~$ python3 -m site

sys.path = [

    '/home/cmd',

    '/usr/lib/python35.zip',

    '/usr/lib/python3.5',

    '/usr/lib/python3.5/plat-x86_64-linux-gnu',

    '/usr/lib/python3.5/lib-dynload',

    '/home/cmd/.local/lib/python3.5/site-packages',

    '/usr/local/lib/python3.5/dist-packages',

    '/usr/lib/python3/dist-packages',

]

USER_BASE: '/home/cmd/.local' (exists)

USER_SITE: '/home/cmd/.local/lib/python3.5/site-packages' (exists)

ENABLE_USER_SITE: True




cmd@TR:~$ whereis pip2

pip2: /usr/bin/pip2 /usr/share/man/man1/pip2.1.gz

cmd@TR:~$ whereis pip3

pip3: /usr/bin/pip3 /usr/share/man/man1/pip3.1.gz

pyenv + pyenv-virtualenv Python 多版本方案:

https://www.cnblogs.com/npumenglei/p/3719412.html
https://github.com/pyenv/pyenv-installer
https://github.com/pyenv/pyenv-virtualenv

然后看到 pyenv 方案 没有执行编译安装 py3.6.3 sudo make install 暂时该已经编译好的先放那,以后看看是否需要

pyenv + pyenv-virtualenv 还是要一起用 只用 pyenv 好像还是会有冲突问题:
但这时候, 每个版本的环境仍是唯一的, 如果我们想在环境中安装一些库的话, 还是会导致这个版本的环境被修改。 这个时候, 如果我们用 virtual env 去建立虚拟环境, 就可以完全保证系统路径的干净。无论你在虚拟环境中安装了什么程序, 都不会影响已安装版本的系统环境

以下是安装记录:

cmd@TR:~$ sudo apt-get install curl git-core

cmd@TR:~$ curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
  略去部分
WARNING: seems you still have not added 'pyenv' to the load path.
# Load pyenv automatically by adding
# the following to ~/.bash_profile:
export PATH="/home/cmd/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

根据提示 需要追加 PATH 依我个人习惯我追加到了 ~/.profile
sudo gedit ~/.profile

末尾追加
#Pyenv path
export PATH="/home/cmd/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"


source ~/.profile

cmd@TR:~$ pyenv --version
pyenv 1.2.0


pyenv install --list


cmd@TR:~$ pyenv install 3.6.3
Downloading Python-3.6.3.tar.xz...
-> https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz
Installing Python-3.6.3...
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib?
Installed Python-3.6.3 to /home/cmd/.pyenv/versions/3.6.3

cmd@TR:~$ pyenv versions
* system (set by /home/cmd/.pyenv/version)
  3.6.3

cmd@TR:~$ pyenv global 3.6.3
cmd@TR:~$ pyenv versions
  system
* 3.6.3 (set by /home/cmd/.pyenv/version)

利用 virtualenv 创建虚拟 python 环境
现在我们已经安装了多个 python 版本,但是这还不够, 某些有洁癖的程序员是一定要系统环境足够干净的,这时候我们可以使用 virtualenv 来创建虚拟 python 环境
virtualenv 本是一个独立的工具,官网在这里:https://pypi.python.org/pypi/virtualenv
幸运的是,如果你是安装我们前面的方式安装 pyenv 的,那它已经帮我们以 plugin 的形式安装好了 virtualenv, 我们只要使用就好了
首先我们创建一个 3.6.3 的虚拟环境


cmd@TR:~$ pyenv virtualenv 3.6.3 pyenv363
Requirement already satisfied: setuptools in /home/cmd/.pyenv/versions/3.6.3/envs/pyenv363/lib/python3.6/site-packages
Requirement already satisfied: pip in /home/cmd/.pyenv/versions/3.6.3/envs/pyenv363/lib/python3.6/site-packages

cmd@TR:~$ pyenv versions
  system
* 3.6.3 (set by /home/cmd/.pyenv/version)
  3.6.3/envs/pyenv363
  pyenv363

切换和使用新的 python 虚拟环境
切换到新的虚拟环境的命令为

cmd@TR:~$ pyenv activate pyenv363
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.

(pyenv363) cmd@TR:~$ python
Python 3.6.3 (default, Dec 22 2017, 09:54:35)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.

如果要切换回系统环境, 运行这个命令即可
pyenv deactivate

那如果要删除这个虚拟环境呢? 只要直接删除它所在的目录就好:
rm -rf ~/.pyenv/versions/pyenv363/
或者卸载:
pyenv uninstall pyenv363

最后虚拟环境安装 httprunner:

(pyenv363) cmd@TR:~$ pip install git+https://github.com/HttpRunner/HttpRunner.git#egg=HttpRunner
Collecting HttpRunner from git+https://github.com/HttpRunner/HttpRunner.git#egg=HttpRunner
  Cloning https://github.com/HttpRunner/HttpRunner.git to /tmp/pip-build-frtiyh5m/HttpRunner
Collecting requests[security] (from HttpRunner)
  Using cached requests-2.18.4-py2.py3-none-any.whl
Collecting flask (from HttpRunner)
  Using cached Flask-0.12.2-py2.py3-none-any.whl
Collecting PyYAML (from HttpRunner)
  Using cached PyYAML-3.12.tar.gz
Collecting PyUnitReport (from HttpRunner)
  Using cached pyunitreport-0.1.4.tar.gz
Collecting idna<2.7,>=2.5 (from requests[security]->HttpRunner)
  Using cached idna-2.6-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests[security]->HttpRunner)
  Using cached certifi-2017.11.5-py2.py3-none-any.whl
Collecting urllib3<1.23,>=1.21.1 (from requests[security]->HttpRunner)
  Using cached urllib3-1.22-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2 (from requests[security]->HttpRunner)
  Using cached chardet-3.0.4-py2.py3-none-any.whl
Collecting cryptography>=1.3.4; extra == "security" (from requests[security]->HttpRunner)
  Downloading cryptography-2.1.4-cp36-cp36m-manylinux1_x86_64.whl (2.2MB)
    100% |████████████████████████████████| 2.2MB 133kB/s
Collecting pyOpenSSL>=0.14; extra == "security" (from requests[security]->HttpRunner)
  Using cached pyOpenSSL-17.5.0-py2.py3-none-any.whl
Collecting click>=2.0 (from flask->HttpRunner)
  Using cached click-6.7-py2.py3-none-any.whl
Collecting itsdangerous>=0.21 (from flask->HttpRunner)
  Using cached itsdangerous-0.24.tar.gz
Collecting Jinja2>=2.4 (from flask->HttpRunner)
  Using cached Jinja2-2.10-py2.py3-none-any.whl
Collecting Werkzeug>=0.7 (from flask->HttpRunner)
  Using cached Werkzeug-0.13-py2.py3-none-any.whl
Collecting six>=1.4.1 (from cryptography>=1.3.4; extra == "security"->requests[security]->HttpRunner)
  Using cached six-1.11.0-py2.py3-none-any.whl
Collecting asn1crypto>=0.21.0 (from cryptography>=1.3.4; extra == "security"->requests[security]->HttpRunner)
  Using cached asn1crypto-0.24.0-py2.py3-none-any.whl
Collecting cffi>=1.7; platform_python_implementation != "PyPy" (from cryptography>=1.3.4; extra == "security"->requests[security]->HttpRunner)
  Downloading cffi-1.11.2-cp36-cp36m-manylinux1_x86_64.whl (419kB)
    100% |████████████████████████████████| 430kB 231kB/s
Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->flask->HttpRunner)
  Using cached MarkupSafe-1.0.tar.gz
Collecting pycparser (from cffi>=1.7; platform_python_implementation != "PyPy"->cryptography>=1.3.4; extra == "security"->requests[security]->HttpRunner)
Installing collected packages: idna, certifi, urllib3, chardet, six, asn1crypto, pycparser, cffi, cryptography, pyOpenSSL, requests, click, itsdangerous, MarkupSafe, Jinja2, Werkzeug, flask, PyYAML, PyUnitReport, HttpRunner
  Running setup.py install for itsdangerous ... done
  Running setup.py install for MarkupSafe ... done
  Running setup.py install for PyYAML ... done
  Running setup.py install for PyUnitReport ... done
  Running setup.py install for HttpRunner ... done
Successfully installed HttpRunner-0.8.5 Jinja2-2.10 MarkupSafe-1.0 PyUnitReport-0.1.4 PyYAML-3.12 Werkzeug-0.13 asn1crypto-0.24.0 certifi-2017.11.5 cffi-1.11.2 chardet-3.0.4 click-6.7 cryptography-2.1.4 flask-0.12.2 idna-2.6 itsdangerous-0.24 pyOpenSSL-17.5.0 pycparser-2.18 requests-2.18.4 six-1.11.0 urllib3-1.22



(pyenv363) cmd@TR:~$ httprunner -V

HttpRunner version: 0.8.5

PyUnitReport version: 0.1.4
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 2 条回复 时间 点赞
老马 启动 HttpRunner DemoAPI server 中提及了此贴 12月22日 17:30

~/.profile
后末尾追加
#Pyenv path
export PATH="/home/cmd/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

不知道为啥 每次启动 terminal
pyenv activate pyenv363 会提示找不到 pyenv
还必须再 source 一次。我把它调整到~/.bashrc 启动终端就不需要再 source 了。

谢谢楼主,正好有 python 多版本的需求👏

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册