import subprocess import os from pipfile import Pipfile def globally_installed_py_packages(): output = subprocess.check_output("pip list --format freeze".split(), env={}) output = output.decode("utf-8") output = output.strip() global_packages = output.split("\n") return global_packages global_packages = globally_installed_py_packages() name = "cow" p = Pipfile.load(filename=name) content = "" with open(name, "r") as f: content = f.read() for pip_package in p.data["default"]: version = p.data["default"][pip_package] if version == "*": for package in global_packages: package = package.lower() if package.startswith(pip_package.lower()): substr = f'{pip_package} = "*"' content = content.replace(substr, package) with open(name, "w") as f: f.write(content)