No more python virtual environments?

Virtual Environments

A virtual environment is a term used to describe any folder or workspace that is isolated from outside it. Virtual environments are an important part of any python workflow. This is because it lets you install different versions of the same package, install packages that are not globally required, and track packages in the current project requirement.

Basically creating a virtual environment installs the python interpreter within the project workspace/directory and makes it the default python path(when activated). There are various tools for creating virtual environments and the popular ones are,

  • virtualenv: Lightweight virtual environment creator
  • poetry: Fully-fledged project manager

PEP 582 - Python local packages directory

This PEP proposes to add to Python a mechanism to automatically recognize a pypackages directory and prefer importing packages installed in this location over user or global site-packages. This will avoid the steps to create, activate, or deactivate "virtual environments". Python will use the pypackages from the base directory of the script when present.

What this does is, create a folder named __pypackages__ and install all the project/workspace requirements in this folder. No interpreter will be copied over essentially using the global one. PEP 582 was originally introduced in May of 2018 and it is still in draft (not accepted yet).

To test this right now, you can install a python wrapper called pythonloc

pip install --user pythonloc

In your project directory, run

piploc install fastapi
# piploc install [anything]
# piploc is installed automatically with pythonloc

And when you check the directory,

Folder PATH listing for volume BEAST
Volume serial number is FCA8-676F
            │   ├───dependencies
            │   │   └───__pycache__
            │   ├───middleware
            │   │   └───__pycache__
            │   ├───openapi
            │   │   └───__pycache__
            │   ├───security
            │   │   └───__pycache__
            │   └───__pycache__
            │   └───__pycache__
            │   ├───middleware
            │   │   └───__pycache__
            │   └───__pycache__

Every package is installed within a folder named __pypackages__. And the interpreter will scan for this directory, if it exists, the directory will be added to PYTHONPATH, else it'll run from site-packages. Now if you run a file using python, either it'll say package import failed or it'll import from site-packages if it exists. Because the python interpreter does not scan for the __pypackages__ yet. Therefore you have to run pythonloc

You can perform the standard package installation commands like,

  • piploc install .
  • piploc install -r requirements.txt

pip freeze => pipfreezeloc


No Comments Yet