How to install packages offline?

后端 未结 10 880
一个人的身影
一个人的身影 2020-11-22 00:38

What\'s the best way to download a python package and it\'s dependencies from pypi for offline installation on another machine? Is there any easy way to do this with pip or

10条回答
  •  不知归路
    2020-11-22 01:13

    I had a similar problem. And i had to make it install the same way, we do from pypi.

    I did the following things:

    1. Make a directory to store all the packages in the machine that have internet access.

      mkdir -p /path/to/packages/
      
    2. Download all the packages to the path

    Edit: You can also try:

    python3 -m pip wheel --no-cache-dir -r requirements.txt -w /path/to/packages
    
    pip download -r requirements.txt -d /path/to/packages
    
    Eg:- ls /root/wheelhouse/  # **/root/wheelhouse** is my **/path/to/packages/**
    total 4524
    -rw-r--r--. 1 root root   16667 May 23  2017 incremental-17.5.0-py2.py3-none-any.whl
    -rw-r--r--. 1 root root   34713 Sep  1 10:21 attrs-18.2.0-py2.py3-none-any.whl
    -rw-r--r--. 1 root root 3088398 Oct 15 14:41 Twisted-18.9.0.tar.bz2
    -rw-r--r--. 1 root root  133356 Jan 28 15:58 chardet-3.0.4-py2.py3-none-any.whl
    -rw-r--r--. 1 root root  154154 Jan 28 15:58 certifi-2018.11.29-py2.py3-none-any.whl
    -rw-r--r--. 1 root root   57987 Jan 28 15:58 requests-2.21.0-py2.py3-none-any.whl
    -rw-r--r--. 1 root root   58594 Jan 28 15:58 idna-2.8-py2.py3-none-any.whl
    -rw-r--r--. 1 root root  118086 Jan 28 15:59 urllib3-1.24.1-py2.py3-none-any.whl
    -rw-r--r--. 1 root root   47229 Jan 28 15:59 tqdm-4.30.0-py2.py3-none-any.whl
    -rw-r--r--. 1 root root    7922 Jan 28 16:13 constantly-15.1.0-py2.py3-none-any.whl
    -rw-r--r--. 1 root root  164706 Jan 28 16:14 zope.interface-4.6.0-cp27-cp27mu-manylinux1_x86_64.whl
    -rw-r--r--. 1 root root  573841 Jan 28 16:14 setuptools-40.7.0-py2.py3-none-any.whl
    -rw-r--r--. 1 root root   37638 Jan 28 16:15 Automat-0.7.0-py2.py3-none-any.whl
    -rw-r--r--. 1 root root   37905 Jan 28 16:15 hyperlink-18.0.0-py2.py3-none-any.whl
    -rw-r--r--. 1 root root   52311 Jan 28 16:15 PyHamcrest-1.9.0-py2.py3-none-any.whl
     -rw-r--r--. 1 root root   10586 Jan 28 16:15 six-1.12.0-py2.py3-none-any.whl
    
    1. Tar the packages directory and copy it to the Machine that doesn't have internet access. Then do,

      cd /path/to/packages/
      tar -cvzf packages.tar.gz .  # not the . (dot) at the end
      

    Copy the packages.tar.gz into the destination machine that doesn't have internet access.

    1. In the machine that doesn't have internet access, do the following (Assuming you copied the tarred packages to /path/to/package/ in the current machine)

      cd /path/to/packages/
      tar -xvzf packages.tar.gz
      mkdir -p $HOME/.config/pip/
      vi $HOME/.config/pip/pip.conf
      

    and paste the following content inside and save it.

    [global]
    timeout = 10
    find-links = file:///path/to/package/
    no-cache-dir = true
    no-index = true
    
    1. Finally, i suggest you use, some form of virtualenv for installing the packages.

      virtualenv -p python2 venv # use python3, if you are on python3
      source ./venv/bin/activate
      pip install 
      

    You should be able to download all the modules that are in the directory /path/to/package/.

    Note: I only did this, because i couldn't add options or change the way we install the modules. Otherwise i'd have done

    pip install --no-index --find-links /path/to/download/dir/ -r requirements.txt
    

提交回复
热议问题