Tutorial丨Reference Python third-party libraries in MeterSphere pre- and post-scripts

Many friends in the open source community have asked about the specific method of referencing the Python third-party library in the pre- and post-scripts of the MeterSphere open source continuous testing platform. This article will quickly and concisely share the specific steps to achieve this operation.

plan

Virtualenv is a tool for creating isolated Python environments. Virtualenv creates a folder containing all the necessary executable files to use the packages required by the Python project. By creating a Virtualenv environment on the host where the MeterSphere continuous testing platform is located, installing the third-party library, and mounting it into the JMeter container, the third-party library can be introduced when executing the Python script on the MeterSphere platform.

Implementation steps

1. Prerequisites

The host where MeterSphere is located has Python and Virtualenv tools installed.

Note: In an environment where both Python2 and Python3 are installed, use virtualenv --copies .  The virtual environment created will be Python3. Currently, MeterSphere only supports Python2, so try to use python2 -m virtualenv  environment name to create it.

Install Virtualenv:

pip install virtualenv

2. Specific steps

① Enter the node where MeterSphere is located and install the required third-party packages.

Install third-party packages :

#创建virtualenv#data目录为docker默认挂载路径,可将第三方包安装该目录下,若Metersphere实际安装路径为其他路径,则下面操作放在MS实际安装路径下cd /opt/metersphere/data/mkdir pythoncd python/virtualenv --copies .source bin/activate
#安装第三方包,比如xpinyinpip install xpinyin==0.5.7  -i https://pypi.tuna.tsinghua.edu.cn/simple#查看是否安装成功,是否有生成相应目录ll lib/python2.7/site-packages/

② Write a Python pre- and post-script in the foreground and verify it.

Script example:

import sys#注意:此路径容器内部的映射路径,为固定值sys.path.append("/opt/metersphere/data/python/lib/python2.7/site-packages") 
from xpinyin import Pinyin
log.info("==========debug==========")

③ Verification test

​​​​​​​

{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324040810&siteId=291194637