Don
Don

Reputation: 6882

How to check whether a given Python Environment is a Pipenv

Currently Pipenv can be used in a directory to check whether we have a corresponding pipenv environment or not (e.g. pipenv --py).

Is there a similar API to determine whether a given interpreter is a pipenv?

Upvotes: 10

Views: 9463

Answers (3)

serv-inc
serv-inc

Reputation: 38247

If you are trying to check inside a python interpreter, the following could help:

any('.virtualenvs' in x for x in sys.path))

due to pipenv storing its virtual environments in a .virtualenvs directory.

Upvotes: 0

James O'Brien
James O'Brien

Reputation: 1706

You might need more accuracy than the given answer in a Makefile or as part of a build process, because the user could be using virtualenv or pyenv.

When you run pipenv shell, I noticed an environmental variable gets set: PIPENV_ACTIVE=1

After exiting the shell with exit, PIPENV_SHELL will be unset.

So in a Makefile (this may be gnu-make specific syntax), you can add a target:

guard-%:
    @ if [ "${${*}}" = "" ]; then \
        echo "Run pipenv before command" \
        exit 1; \
    fi

evaluate: guard-PIPENV_ACTIVE # evaluate model
    python evaluate_model.py

$ make evaluate
Makefile:18: *** Run pipenv shell before command.  Stop.

Note: make requires tabs not spaces so copying you'll have to replace.

guard-% from https://stackoverflow.com/a/7367903/1340069

Upvotes: 3

ascourtas
ascourtas

Reputation: 267

From within a Pipenv shell, you can run 'pip -V' which will show you the path to the pip version you're using -- which will include the virtual environment path, and the Python interpreter.

For example:

pipenv shell

Produces:

Spawning environment shell (/bin/bash). Use 'exit' to leave.
~/$ . /home/<username>/.local/share/virtualenvs/projects-6W-pCI0A/bin/activate

Then, from within the Pipenv shell, running

pip -V

Gives:

pip 10.0.1 from /home/<username>/.local/share/virtualenvs/projects-6W-pCI0A/local/lib/python2.7/site-packages/pip (python 2.7)

Of course, your username would replace <username>, and your current working directory would replace mine (projects)

Upvotes: 7

Related Questions