4

I'm looking for a way to test whether a directory is a virtualenv or not, e.g. by checking the existence or contents of key files inside the directory.

The purpose of this is that I want to create a script which manages virtualenvs for me and I want to prevent it from removing a directory which is not actually a virtualenv (which could e.g. happen if I used tab completion to choose the virtualenv and was trigger-happy).

Is there some standard way to detect whether a directory has been initialized as a virtualenv by virtualenv? I'm ok with a solution which only works with the newest versions of virtualenv.

My current solution is the check for the existence of the two paths bin/python and bin/activate.

Feuermurmel
  • 9,490
  • 10
  • 60
  • 90
  • If you keep your virtualenvs in `~/.virtualenvs/` then you don't really have to worry about that. – Wayne Werner Jan 22 '16 at 14:26
  • 1
    Why don't you just install `virtualenvwrapper`? You can try `rmvirtualenv foo`, which will remove `foo` if it's a virtualenv and won't if it's not. @WayneWerner 's way is the standard way, though. – erip Jan 22 '16 at 14:27
  • @WayneWerner I have a lot of project going on at the same time, thus I want to keep my virtualenvs in the same directory as the project. – Feuermurmel Jan 22 '16 at 15:22
  • @erip With the script I'm creating, I try to move away from the notion of activating a virtualenv in an already running shell and I instead want to launch a new shell (to get a clean environment and to make deactivation reliable by exiting the shell). I've looked at `virtualenvwrapper` and it takes the opposite approach. – Feuermurmel Jan 22 '16 at 15:24

0 Answers0