Verifying all Pipenv packages are installed and are up-to-date without uninstalling and re-installing the entire virtual environment "pipenv install"

Viewed 448

As the title would suggest I haven’t figured out how to avoid the default behavior of using pipenv install. By default, pipenv install uninstalls the virtual environment and then reinstantiates it along with all dependencies specified inside the pipfile. While this default behavior is ok it becomes a painfully slow process as the dependency list grows.

I am looking to change this by first detecting if a virtual environment already exists and if so only update the packages that need to be updated. This would also mean that if a package in the Pipfile is set to a specific package that package would not be updated.

However, if a virtual environment does not exist then the standard pipenv install would be sufficient.

Here is the pseudo like representation of my current attempt:

try:
    import pipenv
except ModuleNotFoundError:
    # Install pipenv here

try:
    subprocess.check_output(["pipenv", "run", "pip3", "-V"])
    # Virtual Env exists
    packages = subprocess.check_output(["pipenv", "run", "pip3", "list", "--outdated"]).decode('utf-8')

    # After parsing out the packages properly
    # run python3 -m pipenv run pip3 install -U {packages}
except subprocess.SubprocessError:
    # No Virtual Env found, create a new one
    # using 'python3 -m pipenv --three install --dev --skip-lock --clear'

While this code executes without problems I have no idea if this takes into account Pipfile versions. I also get warnings saying that some of the upgraded dependencies are not compatible with other already installed dependencies. But pip3 still seems to install these packages, breaking future imports...

Is there a better way to go about solving this issue?

Thanks everyone!

0 Answers
Related