Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dineshdharme/49d2e753e21a7daeb32057060c366adf to your computer and use it in GitHub Desktop.
Save dineshdharme/49d2e753e21a7daeb32057060c366adf to your computer and use it in GitHub Desktop.
Offline installation of python dependencies
#!/usr/bin/env bash
# This script follows the steps described in this link:
# https://stackoverflow.com/a/51646354/8808983
LIBRARIES_DIR="python3.6-wheelhouse"
REQ_FILE="requirements.txt"
PLATFORM="manylinux1_x86_64"
if [ -d ${LIBRARIES_DIR} ]; then
echo "Removing all directories and other stuff from previous run of the script"
rm -rf ${LIBRARIES_DIR}*
mkdir ${LIBRARIES_DIR}
else
mkdir ${LIBRARIES_DIR}
fi
## Remove the package pkg-resources as it causes issues and not required
## Removing the pip & setuptools entries as they will be added manually
grep -v -E "pkg-resources==0.0.0|pip==21.1.2|setuptools==39.0.1" ${REQ_FILE} > tmpfile && mv tmpfile ${REQ_FILE}
echo 'pip==21.1.2' >> ${REQ_FILE}
echo 'setuptools==39.0.1' >> ${REQ_FILE}
## IMPORTANT : IT IS ADVISED TO USE Latest version of pip
echo "Please use latest version of pip in order to avoid spurious errors. So upgrade your pip after creating a virtual environment"
pip3 download --platform=${PLATFORM} --no-deps --no-cache-dir -r ${REQ_FILE} -d ${LIBRARIES_DIR}
files_to_add=("requirements.txt" "install-python-libraries-offline.sh")
for file in "${files_to_add[@]}"; do
echo "Adding file ${file}"
cp "$file" ${LIBRARIES_DIR}
done
tar -czvf ${LIBRARIES_DIR}.tar.gz ${LIBRARIES_DIR}
#!/usr/bin/env bash
# This script follows the steps described in this link:
# https://stackoverflow.com/a/51646354/8808983
# This file should run during the installation process from inside the libraries directory, after it was untared:
# pythonX-wheelhouse.tar -> untar -> pythonX-wheelhouse
# |
# |--requirements.txt
# |--install-python-libraries-offline.sh
LIBRARIES_TAR="python3.6-wheelhouse.tar.gz"
LIBRARIES_DIR_UNTAR="python3.6-wheelhouse-untarred"
REQ_FILE="requirements.txt"
VENV_DIR="my_venv"
if [ -d "$VENV_DIR" ] ; then
echo "Removing old environment directory"
rm -rf "$VENV_DIR"
fi
if [ -d "$LIBRARIES_DIR_UNTAR" ] ; then
echo "Removing old untarred directory"
rm -rf "$LIBRARIES_DIR_UNTAR"
fi
echo "Creating virtual environment and activating it"
python3 -m venv ${VENV_DIR}
source "${VENV_DIR}/bin/activate"
echo "Creating the untar directory and untarring the wheels into it"
mkdir ${LIBRARIES_DIR_UNTAR} && tar -xzvf ${LIBRARIES_TAR} -C ${LIBRARIES_DIR_UNTAR}
echo "changing directory into the python-wheelhouse3.6 under the untar directory"
cd ${LIBRARIES_DIR_UNTAR}/*/
echo "Listing all the files in the subdirectory"
ls -al .
echo "Upgrading pip & setuptools in the installation"
# python3 -m pip install --upgrade pip
pip3 install --ignore-installed pip==21.1.2 --no-index --find-links .
pip3 install --ignore-installed setuptools==39.0.1 --no-index --find-links .
#python3 -m pip install --upgrade pip==21.1.2 --no-index --find-links .
echo "Installing the requirements from requirements file"
pip3 install --ignore-installed -r ${REQ_FILE} --no-index --find-links .
@dineshdharme
Copy link
Author

Building on top of the answer given here :
https://stackoverflow.com/questions/11091623/how-to-install-packages-offline

What's the best way to download a python package and it's dependencies from pypi for offline installation on another machine?

  1. Write a "requirements.txt" file that specifies the libraries you want to pack.
  2. Create a tar file that contains all your libraries (see the Packer script).
  3. Put the created tar file in the target machine and untar it.
  4. run the Installer script (which is also packed into the tar file).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment