1、场景介绍
在一些服务器上,我们搭建完Python
环境之后,因为服务器的网络限制原因,不能直接通过pip
命令下载安装Python
的依赖包。
因此,我们需要在可以正常上网的服务器上下载好所需的依赖包文件,然后拷贝到目标服务器,通过pip
在目标服务器上进行安装。
2、准备requirements.txt文件
requirements.txt
文件罗列的是依赖包列表,表示你要下载的依赖包以及对应的版本。
该文件可以通过两种方式创建,分别是pip命令创建和手工创建。
jupyter==1.0.0notebook==6.1.0
(1)pip命令创建requirements.txt,可以把当前服务器的Python依赖包生成列表到requirements.txt文件中。
【pip list】
【pip freeze >requirements.txt】
(2)手工创建
requirements.txt
,直接新建一个requirements.txt
文件,然后按照格式填写所需依赖包以及对应版本号。
3、下载依赖包
注意:该步骤需要在可以正常上网的服务器上下载。下载的依赖包文件如图2所示。
pip download -r requirements.txt -d packages/ -i https://pypi.tuna.tsinghua.edu.cn/simple
其中:(packages/
是要生成的文件夹,存放下载的依赖包文件;https://pypi.tuna.tsinghua.edu.cn/simple
是镜像源地址)。
4、安装依赖包
准备好依赖包之后,就可以在无法使用pip命令下载安装依赖包的目标服务器上进行安装了。
使用命令之前,需要把packsges
文件夹、requirements.txt
文件拷贝到目标服务器,下面以requests
依赖包为例。
pip install --no-index --find-links=./packages -r ./requirements.txt
(其中:./packsges
是依赖包的所在路径;./requirements.txt
是依赖包的列表路径)。
D:\zjj\pip>pip listPackage Version--------------------------------- ---------anyio 3.7.1argon2-cffi 21.3.0argon2-cffi-bindings21.2.0arrow 1.2.3asttokens 2.2.1async-lru 2.0.4attrs 23.1.0Babel 2.12.1backcall0.2.0beautifulsoup44.12.2bleach6.0.0certifi 2023.7.22cffi1.15.1charset-normalizer3.2.0colorama0.4.6comm0.1.3debugpy 1.6.7decorator 5.1.1defusedxml0.7.1distlib 0.3.7exceptiongroup1.1.2executing 1.2.0fastjsonschema2.18.0filelock3.12.2fqdn1.5.1idna3.4importlib-metadata6.8.0importlib-resources 6.0.0ipykernel 6.25.0ipython 8.12.2ipython-genutils0.2.0ipywidgets8.0.7isoduration 20.11.0jedi0.19.0Jinja23.1.2json5 0.9.14jsonpointer 2.4jsonschema4.18.4jsonschema-specifications 2023.7.1jupyter_client8.3.0jupyter-console 6.6.3jupyter-contrib-core0.4.2jupyter_core5.3.1jupyter-events0.6.3jupyter-highlight-selected-word 0.2.0jupyter-lsp 2.2.0jupyter-nbextensions-configurator 0.6.3jupyter_server2.7.0jupyter_server_terminals0.4.4jupyterlab4.0.3jupyterlab-pygments 0.2.2jupyterlab_server 2.24.0jupyterlab-widgets3.0.8lxml4.9.3MarkupSafe2.1.3matplotlib-inline 0.1.6mistune 3.0.1nbclient0.8.0nbconvert 7.7.3nbformat5.9.1nest-asyncio1.5.7notebook7.0.0notebook_shim 0.2.3overrides 7.3.1packaging 23.1pandocfilters 1.5.0parso 0.8.3pickleshare 0.7.5pip 23.2.1pipenv2023.7.23pkgutil_resolve_name1.3.10platformdirs3.10.0prometheus-client 0.17.1prompt-toolkit3.0.39psutil5.9.5pure-eval 0.2.2pycparser 2.21Pygments2.15.1python-dateutil 2.8.2python-json-logger2.0.7pytz2023.3pywin32 306pywinpty2.0.11PyYAML6.0.1pyzmq 25.1.0qtconsole 5.4.3QtPy2.3.1referencing 0.30.0requests2.31.0rfc3339-validator 0.1.4rfc3986-validator 0.1.1rpds-py 0.9.2Send2Trash1.8.2setuptools68.0.0six 1.16.0sniffio 1.3.0soupsieve 2.4.1stack-data0.6.2terminado 0.17.1tinycss21.2.1tomli 2.0.1tornado 6.3.2traitlets 5.9.0typing_extensions 4.7.1uri-template1.3.0urllib3 2.0.4virtualenv20.24.2virtualenv-clone0.5.7wcwidth 0.2.6webcolors 1.13webencodings0.5.1websocket-client1.6.1widgetsnbextension4.0.8zipp3.16.2D:\zjj\pip>pip install --no-index --find-links=./packages -r ./requirements.txtLooking in links: ./packagesProcessing d:\zjj\pip\packages\requests-2.28.2-py3-none-any.whl (from -r ./requirements.txt (line 1))Requirement already satisfied: charset-normalizer<4,>=2 in d:\environment\python\python3.8.10\lib\site-packages (from requests==2.28.2->-r ./requirements.txt (line 1)) (3.2.0)Requirement already satisfied: idna<4,>=2.5 in d:\environment\python\python3.8.10\lib\site-packages (from requests==2.28.2->-r ./requirements.txt (line 1)) (3.4)Processing d:\zjj\pip\packages\urllib3-1.26.16-py2.py3-none-any.whl (from requests==2.28.2->-r ./requirements.txt (line 1))Requirement already satisfied: certifi>=2017.4.17 in d:\environment\python\python3.8.10\lib\site-packages (from requests==2.28.2->-r ./requirements.txt (line 1)) (2023.7.22)Installing collected packages: urllib3, requestsAttempting uninstall: urllib3Found existing installation: urllib3 2.0.4Uninstalling urllib3-2.0.4:Successfully uninstalled urllib3-2.0.4Attempting uninstall: requestsFound existing installation: requests 2.31.0Uninstalling requests-2.31.0:Successfully uninstalled requests-2.31.0Successfully installed requests-2.28.2 urllib3-1.26.16D:\zjj\pip>pip listPackage Version--------------------------------- ---------anyio 3.7.1argon2-cffi 21.3.0argon2-cffi-bindings21.2.0arrow 1.2.3asttokens 2.2.1async-lru 2.0.4attrs 23.1.0Babel 2.12.1backcall0.2.0beautifulsoup44.12.2bleach6.0.0certifi 2023.7.22cffi1.15.1charset-normalizer3.2.0colorama0.4.6comm0.1.3debugpy 1.6.7decorator 5.1.1defusedxml0.7.1distlib 0.3.7exceptiongroup1.1.2executing 1.2.0fastjsonschema2.18.0filelock3.12.2fqdn1.5.1idna3.4importlib-metadata6.8.0importlib-resources 6.0.0ipykernel 6.25.0ipython 8.12.2ipython-genutils0.2.0ipywidgets8.0.7isoduration 20.11.0jedi0.19.0Jinja23.1.2json5 0.9.14jsonpointer 2.4jsonschema4.18.4jsonschema-specifications 2023.7.1jupyter_client8.3.0jupyter-console 6.6.3jupyter-contrib-core0.4.2jupyter_core5.3.1jupyter-events0.6.3jupyter-highlight-selected-word 0.2.0jupyter-lsp 2.2.0jupyter-nbextensions-configurator 0.6.3jupyter_server2.7.0jupyter_server_terminals0.4.4jupyterlab4.0.3jupyterlab-pygments 0.2.2jupyterlab_server 2.24.0jupyterlab-widgets3.0.8lxml4.9.3MarkupSafe2.1.3matplotlib-inline 0.1.6mistune 3.0.1nbclient0.8.0nbconvert 7.7.3nbformat5.9.1nest-asyncio1.5.7notebook7.0.0notebook_shim 0.2.3overrides 7.3.1packaging 23.1pandocfilters 1.5.0parso 0.8.3pickleshare 0.7.5pip 23.2.1pipenv2023.7.23pkgutil_resolve_name1.3.10platformdirs3.10.0prometheus-client 0.17.1prompt-toolkit3.0.39psutil5.9.5pure-eval 0.2.2pycparser 2.21Pygments2.15.1python-dateutil 2.8.2python-json-logger2.0.7pytz2023.3pywin32 306pywinpty2.0.11PyYAML6.0.1pyzmq 25.1.0qtconsole 5.4.3QtPy2.3.1referencing 0.30.0requests2.28.2rfc3339-validator 0.1.4rfc3986-validator 0.1.1rpds-py 0.9.2Send2Trash1.8.2setuptools68.0.0six 1.16.0sniffio 1.3.0soupsieve 2.4.1stack-data0.6.2terminado 0.17.1tinycss21.2.1tomli 2.0.1tornado 6.3.2traitlets 5.9.0typing_extensions 4.7.1uri-template1.3.0urllib3 1.26.16virtualenv20.24.2virtualenv-clone0.5.7wcwidth 0.2.6webcolors 1.13webencodings0.5.1websocket-client1.6.1widgetsnbextension4.0.8zipp3.16.2D:\zjj\pip>