在一些項目中,我們會不可避免地使用到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
就可以完成安裝了。