9

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? Wanted to check this before posting an issue on the Pipenv repo.

Don
  • 6,632
  • 3
  • 26
  • 34

2 Answers2

7

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)

ascourtas
  • 227
  • 2
  • 10
3

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

James O'Brien
  • 1,696
  • 1
  • 13
  • 10