离线安装 Python 库
1. 有网环境准备依赖资源
apt install pip # 安装更新 pip
pip install --upgrade pip
mkdir relay # 创建依赖库下载保存目录
cd relay
pip download -d ./ pytest==3.3.0 # 下载对应版本依赖库到当前目录
ls -l | awk '{print $9}' | grep -v requirements > requirements.txt # 当前目录下的库添加到 requirements.txt
cat requirements.txt
#检查需添加的库是否正确
# 如果是windows,请执行
Get-ChildItem -File | ForEach-Object { $_.Name } | Where-Object { $_ -notmatch "requirements" } > requirements.txt
2. 打包 relay 上传到无网机器
登录无网机器,解压relay后进行如下步骤
cd relay
pip install --no-index --find-links=./ -r requirements.txt
安装完成后
测试是否安装成功
pip list | grep pytest
python # 进入python cmd 环境
import pytest # 确定引入新安装的库是否正确
3. pip安装了pytest但在bash中运行pytest返回未找到
[root@izbp1jfqk9lif6vh3vclhkz site-packages]# pytest
-bash: pytest: command not found
试试python -m pytest
通过pip安装pytest不会使其成为系统命令,而是会将其安装到python。-m命令将pytest作为其自己的命令运行,然后任何后续脚本都将作为参数。
[root@izbp1jfqk9lif6vh3vclhkz test]# python -m pytest test_01.py
======================================================================== test session starts ========================================================================
platform linux -- Python 3.7.4, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: /root/test
plugins: allure-pytest-2.8.19
collected 2 items
test_01.py .. [100%]
========================================================================= 2 passed in 0.05s =========================================================================
[root@izbp1jfqk9lif6vh3vclhkz test]#