契约测试框架pact-python 安装不了或者安装报错解决
pact-python 在github上的readme推荐使用pip install pact-python 命令来安装。
但是!但是!但是!
pact在安装过程中,会下载ruby环境,由于国内无法下载,会导致下载不到。
因此,找了很多资料,发现可以按如下步骤解决
1.首先去github下载pact-python源码,并解压出来
2.进入解压的目录,运行python setup.py build 和 python setup.py install ,运行第二个命令会发现安装卡住,此时可以中断安装
3.去github下载ruby环境pact-1.88.14-win32.zip,命名改为win32.zip ,https://github.com/pact-foundation/pact-ruby-standalone/releases
4.把改名后的zip放到python site-pactage-pact下的bin目录,并且修改 setup.py 把下载和创建目录注释掉
"""pact-python PyPI Package.""" import os import platform import sys import tarfile from zipfile import ZipFile from setuptools import setup from setuptools.command.develop import develop from setuptools.command.install import install IS_64 = sys.maxsize > 2 ** 32 PACT_STANDALONE_VERSION = '1.54.4' here = os.path.abspath(os.path.dirname(__file__)) about = {} with open(os.path.join(here, "pact", "__version__.py")) as f: exec(f.read(), about) class PactPythonDevelopCommand(develop): """ Custom develop mode installer for pact-python. When the package is installed using `python setup.py develop` or `pip install -e` it will download and unpack the appropriate Pact mock service and provider verifier. """ def run(self): develop.run(self) bin_path = os.path.join(os.path.dirname(__file__), 'pact', 'bin') if not os.path.exists(bin_path): os.mkdir(bin_path) install_ruby_app(bin_path) class PactPythonInstallCommand(install): """ Custom installer for pact-python. Installs the Python package and unpacks the platform appropriate version of the Ruby mock service and provider verifier. """ def run(self): install.run(self) bin_path = os.path.join(self.install_lib, 'pact', 'bin') # os.mkdir(bin_path) 注释掉 install_ruby_app(bin_path) def install_ruby_app(bin_path): """ Download a Ruby application and install it for use. :param bin_path: The path where binaries should be installed. """ target_platform = platform.platform().lower() uri = ('https://github.com/pact-foundation/pact-ruby-standalone/releases' '/download/v{version}/pact-{version}-{suffix}') if 'darwin' in target_platform: suffix = 'osx.tar.gz' elif 'linux' in target_platform and IS_64: suffix = 'linux-x86_64.tar.gz' elif 'linux' in target_platform: suffix = 'linux-x86.tar.gz' elif 'windows' in target_platform: suffix = 'win32.zip' else: msg = ('Unfortunately, {} is not a supported platform. Only Linux,' ' Windows, and OSX are currently supported.').format( platform.platform()) raise Exception(msg) if sys.version_info.major == 2: from urllib import urlopen else: from urllib.request import urlopen path = os.path.join(bin_path, suffix)
# 注释掉 # resp = urlopen(uri.format(version=PACT_STANDALONE_VERSION, suffix=suffix)) # with open(path, 'wb') as f: # if resp.code == 200: # f.write(resp.read()) # else: # raise RuntimeError( # 'Received HTTP {} when downloading {}'.format( # resp.code, resp.url)) if 'windows' in platform.platform().lower(): with ZipFile(path) as f: f.extractall(bin_path) else: with tarfile.open(path) as f: f.extractall(bin_path)
5.再次执行 python setup.py install 则安装完成