Skip to content

Instantly share code, notes, and snippets.

@jakkaj
Created April 18, 2023 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakkaj/ff8203dfff6676b6ae34a194d08f9960 to your computer and use it in GitHub Desktop.
Save jakkaj/ff8203dfff6676b6ae34a194d08f9960 to your computer and use it in GitHub Desktop.
Pins the versions of pip entries in the requirements.txt file. Unlike freeze, it only does the requirements file.
#!/bin/bash
# Check if requirements.txt exists
if [ ! -f "requirements.txt" ]; then
echo "Error: requirements.txt not found."
exit 1
fi
# Remove existing requirements2.txt if it exists
if [ -f "requirements2.txt" ]; then
rm -f requirements2.txt
fi
# Ensure the input file ends with a newline
sed -i -e '$a\' requirements.txt
# Iterate through each line in requirements.txt
while IFS= read -r package; do
# Check if the package is installed
version=$(pip show "$package" | grep -i "^version" | awk '{print $2}')
if [ -n "$version" ]; then
# Append the package name and version to requirements2.txt
echo "${package}==${version}" >> requirements2.txt
else
echo "Warning: Package $package not found. Skipping."
fi
done < requirements.txt
echo "Requirements updated in requirements2.txt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment