Last active
July 25, 2020 05:01
-
-
Save chrisdlangton/905a14e7a42118e0d6b5a45c8ce1d3a0 to your computer and use it in GitHub Desktop.
Python pip requirements.txt lock file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
CWD=$(pwd) | |
TMP_DIR=$1 | |
if [[ $EUID -eq 0 ]]; then | |
echo -e "${RED}x${NC} This script must not be run as root" | |
exit 1 | |
fi | |
if [ -z $(which python3) ]; then | |
echo "python3 not found" | |
exit 1 | |
fi | |
if [ -z $(which pip) ]; then | |
echo "python3 pip not found" | |
exit 1 | |
fi | |
if [[ ! -f requirements.txt ]]; then | |
echo "requirements.txt not found" | |
exit 1 | |
fi | |
if [[ -z "${TMP_DIR}" ]]; then | |
TMP_DIR=/tmp/piplock.$(date +'%s%N') | |
fi | |
if [[ ! -z "$(which deactivate)" ]]; then | |
deactivate | |
fi | |
mkdir -p ${TMP_DIR} | |
cd ${TMP_DIR} | |
python3 -m pip install -U pip | |
python3 -m pip install -U virtualenv | |
python3 -m venv .venv | |
source .venv/bin/activate | |
pip install -q -U --no-cache-dir --isolated --no-warn-conflict -r ${CWD}/requirements.txt | |
check=$(pip check --no-cache-dir --isolated) | |
check_exit=$? | |
if [[ $check_exit -ne 0 ]]; then | |
echo ${check} | |
exit 1 | |
fi | |
LOCK="$(pip freeze)" | |
deactivate | |
cd ${CWD} | |
rm -rf ${TMP_DIR} | |
echo ${LOCK} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Overview
If you are familiar with Node.js package lock files you might have asked if Python Pip can provide the same functionality and learned that you must painstakingly write out the packages and their whole dependency tree of packages yourself, and lock the version using
==
of these.This script automates that painstaking process.
Example - locking
requests
dependencyIt is as easy, get the script
assume simple requirements;
Running
piplock
produces;Enjoy!