Microvenv: Create a Minimal Virtual Environment

2023-12-229:31195github.com

A minimal re-implementation of Python's `venv` module - GitHub - brettcannon/microvenv: A minimal re-implementation of Python's `venv` module

Create a minimal virtual environment (and utility code around environments).

The key purpose of this module is for when the venv module has been removed from the standard library by your Python distribution. Because venv is not available on PyPI and is developed in the stdlib, it is not possible to install it using pip or simply copy the code and expect it to work with older versions of Python. This module then attempts to be that portable alternative for creating virtual environments.

In general, though, using the venv module should be preferred and this module used as a fallback.

There is also utility code around virtual environments. See the docs for details.

CLI Usage

NOTE: The CLI is not available on Windows.

python -m microvenv [--without-scm-ignore-files] [env_dir=".venv"]

If an argument is provided to the script, it is used as the path to create the virtual environment in. Otherwise, the virtual environment is created in .venv.

For programmatic usage, there is the create() function, which is analogous to the venv.create() function.

def create(env_dir: os.PathLike[str] | str = ".venv", *, scm_ignore_files={"git"}) -> None

The microvenv/_create.py file is also small enough to have its contents passed in via the -c flag to python.

Differences compared to the venv module

The code operates similarly to py -m venv --symlinks --without-pip .venv, except that:

  • There are no activation scripts (you can execute python in the virtual environment directly)
  • Windows is not supported

Page 2

Create a minimal virtual environment (and utility code around environments).

The key purpose of this module is for when the venv module has been removed from the standard library by your Python distribution. Because venv is not available on PyPI and is developed in the stdlib, it is not possible to install it using pip or simply copy the code and expect it to work with older versions of Python. This module then attempts to be that portable alternative for creating virtual environments.

In general, though, using the venv module should be preferred and this module used as a fallback.

There is also utility code around virtual environments. See the docs for details.

CLI Usage

NOTE: The CLI is not available on Windows.

python -m microvenv [--without-scm-ignore-files] [env_dir=".venv"]

If an argument is provided to the script, it is used as the path to create the virtual environment in. Otherwise, the virtual environment is created in .venv.

For programmatic usage, there is the create() function, which is analogous to the venv.create() function.

def create(env_dir: os.PathLike[str] | str = ".venv", *, scm_ignore_files={"git"}) -> None

The microvenv/_create.py file is also small enough to have its contents passed in via the -c flag to python.

Differences compared to the venv module

The code operates similarly to py -m venv --symlinks --without-pip .venv, except that:

  • There are no activation scripts (you can execute python in the virtual environment directly)
  • Windows is not supported

Read the original article

Comments

  • By jmholla 2023-12-2215:251 reply

        > Differences compared to the venv module
        >
        > The code operates similarly to py -m venv --symlinks --without-pip .venv, except that:
        >
        > There are no activation scripts (you can execute python in the virtual environment directly)
    
    Minus the non-existence of activation scripts, is this not the case with normal virtual environments? I've always been able to invoke the python executable in the virtual environment directly and be in the proper environment. I believe the activation scripts just ensure your current shell uses the virtual environment's python rather than the system one or whatever you've configured your shell to use.

  • By MrYellowP 2023-12-2310:191 reply

    I never understood the need for virtual environments like this. In windows I use batchfiles running Subst to "activate" python installations to a specific "drive" and not only does it work greatly, it's also short, simple, transparent, flawless and independent of the work of others.

    • By fortyseven 2023-12-2316:17

      When the rest of us downgrade to Windows, maybe we'll keep this post in mind.

  • By drift_code 2023-12-2215:251 reply

    > The key purpose of this module is for when the venv module has been removed from the standard library by your Python distribution.

    I find this confusing. Does Python have plan to deprecate its “venv”? It doesn’t make sense

    • By nazarewk 2023-12-2215:42

      I am pretty sure either Ubuntu and/or Debian stripped it from default Python install.

HackerNews