3

I want to find the Python virtual environments ("venvs" for short) in my file system, and for this I need to know what set of files constitutes a venv.

Some tools - when they want to make sure a directory is a venv - check for the pyvenv.cfg file, but older virtual environments and environments created by Pipenv do not have this file. - Furthermore pyvenv.cfg does not seem to be necessary for activating the venv either.

So what is the minimal set of files to define a working virtual environment? And what is an easy way to check for this in Python?

Clarification:

With "working virtual environment" I mean:

  • A way to use a specific version of Python executable defined in that environment (and possibly different to the default system Python)
  • A way to use specific packages (usually installed via pip, bit that's not a requirement).
  • The Python executable in the venv should be aware of being run inside a venv (see sys.base_prefix and sys.prefix in the Python documentation.)
halloleo
  • 9,216
  • 13
  • 64
  • 122
  • 2
    It depends by what you mean. A "virtual environment" simply means a way to use an independent set of python packages. You can implement it by hand if you want to, without following the standard tools. So the answer to your generic question is: you can't. If you have some specific tools & versions in mind, you should clarify which ones you are interested in. IMHO checking that a directory has `bin/activate` and `lib/pythonX.Y` is a good sign. – Giacomo Alzetta Nov 13 '18 at 12:41
  • @GiacomoAlzetta I mean: A way to have packages inside this directory, so that they do not interfere with the default installation and vice versa. Additionally Python should, when running, be aware of this situation. (See [`sys.base_prefix`](https://docs.python.org/3/library/sys.html#sys.base_prefix)) – halloleo Nov 13 '18 at 13:04
  • 2
    A virtual environment is mostly a bunch of environment variables, which are typically set via some `activate` script, but don't have to be. It's also a directory of packages, which can be anywhere and doesn't need to be anywhere near the `activate` script. Soooo… it can literally be anything. – deceze Nov 14 '18 at 00:45
  • one recommendation, if you’re building these things, is to use a specific name convention for doing so. i’ve ditched ‘/env/‘ in favor of ‘/venv/‘ already and might switch again. no, it doesn’t directly answer the question, but it’s useful not to compound the problem unnecessarily. ‘/node_modules/‘ often has nested ‘/env/‘s. – JL Peyret Nov 14 '18 at 03:39

0 Answers0