在访问外网受限的内网环境使用pytest,如何生成离线依赖包并安装。
生成
前提 - 已安装python-pip
获取pytest的离线包和依赖包
(在可访问外网的环境下载依赖包)
xx@xx-pc MINGW64 /n/offline_package/pytest_off
$ pip download -d ./ pytest==6.0.1
$ pip download -d ./ #不指定版本,获取至当前目录下
...
$ ls -ll
total 692
-rw-r--r-- 1 xx 1049089 6790 八月 27 15:47 atomicwrites-1.4.0-py2.py3-none-any.whl
-rw-r--r-- 1 xx 1049089 49020 八月 27 15:47 attrs-20.1.0-py2.py3-none-any.whl
-rw-r--r-- 1 xx 1049089 15931 八月 27 15:47 colorama-0.4.3-py2.py3-none-any.whl
-rw-r--r-- 1 xx 1049089 31809 八月 27 15:46 importlib_metadata-1.7.0-py2.py3-none-any.whl
-rw-r--r-- 1 xx 1049089 4212 八月 27 15:47 iniconfig-1.0.1-py3-none-any.whl
-rw-r--r-- 1 xx 1049089 43054 八月 27 15:46 more_itertools-8.4.0-py3-none-any.whl
-rw-r--r-- 1 xx 1049089 37620 八月 27 15:47 packaging-20.4-py2.py3-none-any.whl
-rw-r--r-- 1 xx 1049089 18077 八月 27 15:47 pluggy-0.13.1-py2.py3-none-any.whl
-rw-r--r-- 1 xx 1049089 99037 八月 27 15:46 py-1.9.0-py2.py3-none-any.whl
-rw-r--r-- 1 xx 1049089 67842 八月 27 15:47 pyparsing-2.4.7-py2.py3-none-any.whl
-rw-r--r-- 1 xx 1049089 270611 八月 27 15:46 pytest-6.0.1-py3-none-any.whl
-rw-r--r-- 1 xx 1049089 10963 八月 27 15:47 six-1.15.0-py2.py3-none-any.whl
-rw-r--r-- 1 xx 1049089 19528 八月 27 15:46 toml-0.10.1-py2.py3-none-any.whl
-rw-r--r-- 1 xx 1049089 4872 八月 27 15:47 zipp-3.1.0-py3-none-any.whl
- 生成requirements
xx@xx-pc MINGW64 /n/offline_package/pytest_off
$ ls -l | awk '{print $9}' >> requirements.txt
xx@xx-pc MINGW64 /n/offline_package/pytest_off
$ cat requirements.txt
atomicwrites-1.4.0-py2.py3-none-any.whl
attrs-20.1.0-py2.py3-none-any.whl
colorama-0.4.3-py2.py3-none-any.whl
importlib_metadata-1.7.0-py2.py3-none-any.whl
iniconfig-1.0.1-py3-none-any.whl
more_itertools-8.4.0-py3-none-any.whl
packaging-20.4-py2.py3-none-any.whl
pluggy-0.13.1-py2.py3-none-any.whl
py-1.9.0-py2.py3-none-any.whl
pyparsing-2.4.7-py2.py3-none-any.whl
pytest-6.0.1-py3-none-any.whl
six-1.15.0-py2.py3-none-any.whl
toml-0.10.1-py2.py3-none-any.whl
zipp-3.1.0-py3-none-any.whl
验证
- 依赖requirements.txt安装
# pip install --no-index --find-links=./ -r requirements.txt
xx@xx-pc MINGW64 /n/offline_package/pytest_off
$ pip install --no-index --find-links=./ -r requirements.txt
Looking in links: ./
Processing n:\offline_package\pytest_off\atomicwrites-1.4.0-py2.py3-none-any.whl
Processing n:\offline_package\pytest_off\attrs-20.1.0-py2.py3-none-any.whl
Processing n:\offline_package\pytest_off\colorama-0.4.3-py2.py3-none-any.whl
Processing n:\offline_package\pytest_off\importlib_metadata-1.7.0-py2.py3-none-any.whl
Processing n:\offline_package\pytest_off\iniconfig-1.0.1-py3-none-any.whl
Processing n:\offline_package\pytest_off\more_itertools-8.4.0-py3-none-any.whl
Processing n:\offline_package\pytest_off\packaging-20.4-py2.py3-none-any.whl
Processing n:\offline_package\pytest_off\pluggy-0.13.1-py2.py3-none-any.whl
Processing n:\offline_package\pytest_off\py-1.9.0-py2.py3-none-any.whl
Processing n:\offline_package\pytest_off\pyparsing-2.4.7-py2.py3-none-any.whl
Processing n:\offline_package\pytest_off\pytest-6.0.1-py3-none-any.whl
Processing n:\offline_package\pytest_off\six-1.15.0-py2.py3-none-any.whl
Processing n:\offline_package\pytest_off\toml-0.10.1-py2.py3-none-any.whl
Processing n:\offline_package\pytest_off\zipp-3.1.0-py3-none-any.whl
Installing collected packages: atomicwrites, attrs, colorama, zipp, importlib-metadata, iniconfig, more-itertools, six, pyparsing, packaging, pluggy, py, toml, pytest
Found existing installation: atomicwrites 1.3.0
Uninstalling atomicwrites-1.3.0:
Successfully uninstalled atomicwrites-1.3.0
Found existing installation: attrs 19.1.0
Uninstalling attrs-19.1.0:
Successfully uninstalled attrs-19.1.0
Found existing installation: colorama 0.4.1
Uninstalling colorama-0.4.1:
Successfully uninstalled colorama-0.4.1
Found existing installation: zipp 0.5.2
Uninstalling zipp-0.5.2:
Successfully uninstalled zipp-0.5.2
Found existing installation: importlib-metadata 0.19
Uninstalling importlib-metadata-0.19:
Successfully uninstalled importlib-metadata-0.19
Found existing installation: more-itertools 7.2.0
Uninstalling more-itertools-7.2.0:
Successfully uninstalled more-itertools-7.2.0
Found existing installation: six 1.12.0
Uninstalling six-1.12.0:
Successfully uninstalled six-1.12.0
Found existing installation: pyparsing 2.4.1.1
Uninstalling pyparsing-2.4.1.1:
Successfully uninstalled pyparsing-2.4.1.1
Found existing installation: packaging 19.0
Uninstalling packaging-19.0:
Successfully uninstalled packaging-19.0
Found existing installation: pluggy 0.12.0
Uninstalling pluggy-0.12.0:
Successfully uninstalled pluggy-0.12.0
Found existing installation: py 1.8.0
Uninstalling py-1.8.0:
Successfully uninstalled py-1.8.0
Found existing installation: pytest 5.0.1
Uninstalling pytest-5.0.1:
Successfully uninstalled pytest-5.0.1
Successfully installed atomicwrites-1.4.0 attrs-20.1.0 colorama-0.4.3 importlib-metadata-1.7.0 iniconfig-1.0.1 more-itertools-8.4.0 packaging-20.4 pluggy-0.13.1 py-1.9.0 pyparsing-2.4.7 pytest-6.0.1 six-1.15.0 toml-0.10.1 zipp-3.1.0
-
启动python看能否正确import
故障分析
- 本地安装验证一直报错
xx@xx-pc MINGW64 /n/offline_package/pytest_off
$ pip install --no-index --find-links=./ -r requirements.txt
Looking in links: ./
Processing n:\offline_package\pytest_off\atomicwrites-1.4.0-py2.py3-none-any.whl
Processing n:\offline_package\pytest_off\attrs-20.1.0-py2.py3-none-any.whl
Processing n:\offline_package\pytest_off\colorama-0.4.3-py2.py3-none-any.whl
Processing n:\offline_package\pytest_off\importlib_metadata-1.7.0-py2.py3-none-any.whl
Processing n:\offline_package\pytest_off\iniconfig-1.0.1-py3-none-any.whl
Processing n:\offline_package\pytest_off\more_itertools-8.4.0-py3-none-any.whl
Processing n:\offline_package\pytest_off\packaging-20.4-py2.py3-none-any.whl
Processing n:\offline_package\pytest_off\pluggy-0.13.1-py2.py3-none-any.whl
Processing n:\offline_package\pytest_off\py-1.9.0-py2.py3-none-any.whl
Processing n:\offline_package\pytest_off\pyparsing-2.4.7-py2.py3-none-any.whl
Processing n:\offline_package\pytest_off\pytest-6.0.1-py3-none-any.whl
ERROR: Could not find a version that satisfies the requirement requirements.txt (from -r requirements.txt (line 13)) (from versions: none)
ERROR: No matching distribution found for requirements.txt (from -r requirements.txt (line 13))
仔细查看,发现requirements.txt生成的文件里多了一行:requirements.txt
删除后,执行没问题了。