To get the version information, you can actually use pip freeze selectively after install. Here is a function that should do what you are asking for:
pip_install_save() {
package_name=$1
requirements_file=$2
if [[ -z $requirements_file ]]
then
requirements_file="./requirements.txt"
fi
pip install $package_name && pip freeze | grep -i $package_name >> $requirements_file
}
Note the -i to the grep command. Pip isn’t case sensitive with package names, so you will probably want that.