在一些项目中,我们会不可避免地使用到python组件,而生产环境大多数服务器是没有办法直接联网下载python的包的。当然,有的小伙伴会很聪明地把这个包的whl下载下来到服务器上去执行,但是会得到这样的结果:
C:\Users\a-mengsr>pip install C:\Users\a-mengsr\Desktop\mssql_scripter-1.0.0a23-py2.py3-none-win32.whl Processing c:\users\a-mengsr\desktop\mssql_scripter-1.0.0a23-py2.py3-none-win32.whl WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x03658C88>, 'Connection to pypi.org timed out. (connecttimeout=15)')': /simple/future/ WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x03658DA8>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/future/
可以看到,即便是指定离线的whl包,pip安装过程中同样会去尝试联网,因为python的包是存在依赖的,当在本地环境找不到依赖时,就会尝试进行下载。
遇到这种情况,可以通过如下方式解决:
1、首先在一台可以上网的电脑上,完成组件安装的整个过程,并记录下输出的日志:
C:\Windows\system32>pip install mssql-scripter --proxy=proxyhk.aac.com:8011 Collecting mssql-scripter Downloading mssql_scripter-1.0.0a23-py2.py3-none-win32.whl (36.3 MB) |████████████████████████████████| 36.3 MB 123 kB/s Collecting future>=0.16.0 Downloading future-0.18.2.tar.gz (829 kB) |████████████████████████████████| 829 kB 3.3 MB/s Collecting wheel>=0.29.0 Downloading wheel-0.35.1-py2.py3-none-any.whl (33 kB) Collecting enum34>=1.1.6 Downloading enum34-1.1.10-py3-none-any.whl (11 kB) Using legacy setup.py install for future, since package 'wheel' is not installed. Installing collected packages: future, wheel, enum34, mssql-scripter Running setup.py install for future ... done Successfully installed enum34-1.1.10 future-0.18.2 mssql-scripter-1.0.0a23 wheel-0.35.1
在这个过程中,会输出在安装过程中所使用到的依赖包。根据上述日志可以看到,在安装mssql-scripter这个组件的过程中,一共使用到了feature、wheel、enum34这三个额外的依赖。那么,首先前往https://pypi.org,搜索并按照平台下载对应的依赖包。
依赖包有可能是whl格式的文件,也有可能是tar.gz的源码。如果是whl格式的文件,直接使用如下命令进行安装:
C:\Users\a-mengsr>pip install C:\Users\a-mengsr\Desktop\wheel-0.35.1-py2.py3-none-any.whl Processing c:\users\a-mengsr\desktop\wheel-0.35.1-py2.py3-none-any.whl Installing collected packages: wheel Successfully installed wheel-0.35.1
C:\Users\a-mengsr>pip install C:\Users\a-mengsr\Desktop\enum34-1.1.10-py3-none-any.whl Processing c:\users\a-mengsr\desktop\enum34-1.1.10-py3-none-any.whl Installing collected packages: enum34 Successfully installed enum34-1.1.10
而遇到源码类型的包,则直接解压缩,而后进入其目录,在命令行下执行指令,至于执行的指令,在上面的日志中已经给出来了:
setup.py install
因为是源码安装,这个过程中会执行编译,输出很多,此处省略。
而将上述所有依赖全部手动安装完毕之后,就可以重新安装上述的主whl了:
C:\Users\a-mengsr>pip install C:\Users\a-mengsr\Desktop\mssql_scripter-1.0.0a23-py2.py3-none-win32.whl Processing c:\users\a-mengsr\desktop\mssql_scripter-1.0.0a23-py2.py3-none-win32.whl Requirement already satisfied: future>=0.16.0 in d:\yinl\python3\lib\site-packages\future-0.18.2-py3.8.egg (from mssql-scripter==1.0.0a23) (0.18.2) Requirement already satisfied: enum34>=1.1.6 in d:\yinl\python3\lib\site-packages (from mssql-scripter==1.0.0a23) (1.1.10) Requirement already satisfied: wheel>=0.29.0 in d:\yinl\python3\lib\site-packages (from mssql-scripter==1.0.0a23) (0.35.1) Installing collected packages: mssql-scripter Successfully installed mssql-scripter-1.0.0a23
就可以完成安装了。