Skip to main content

Like Pipx, but allows creation of a virtual environment then populating it.

Project description

isolated-environment

This is a package isolation library designed specifically for AI developers to solve the problems of AI dependency conflicts introduced by the various pytorch/tensorflow/etc incompatibilities within and between AI apps.

Linting MacOS_Tests Ubuntu_Tests Win_Tests

pip install isolated-environment

It moves the install of your chosen dependencies from install time to runtime. The benefit of this is that you can query the system and make choices on what needs to be installed. For example in pip you can't conditionally install packages based on whether nvidia-smi has been installed (indicating cuda acceleration), but with isolated-environment this is straightfoward.

It also works for any other complex dependency chain. I made this library because conda has significant problems and messes up the system on Windows with its own version of git-bash, standard pip doesn't support implicit --extra-index-url so pretty much all AI apps have non-standard install processes. This really sucks. This library fixes all of this so that complex AI apps can simply be installed with plain old pip.

Instead of having your complex, version conflicting dependencies in your requirements.txt file, you'll move it to the runtime.

This also allows your dependency chain to be installed lazily. For example, maybe your front end app has multiple backends (like transcribe-anything) and are dependent on whether cuda is installed on the system or not. With this library you can query the runtime and decide what you want to install.

For example, if the computer supports cuda you may want to install pytorch with cuda support, a multi-gigabyte download. However if you are running the app on a CPU only machine you may opt for the tiny cpu only pytorch.

In plain words, this package allows you to install your AI apps globally without having to worry about pytorch dependency conflicts.

Example:

from pathlib import Path
import subprocess

CUDA_VERSION = "cu121"
EXTRA_INDEX_URL = f"https://download.pytorch.org/whl/{CUDA_VERSION}"

HERE = Path(os.path.abspath(os.path.dirname(__file__)))
from isolated_environment import IsolatedEnvironment

iso_env = IsolatedEnvironment(HERE / 'whisper_env')
with iso_env.lock():
    iso_env.install_environment()
    iso_env.pip_install('torch==2.1.2', EXTRA_INDEX_URL)
    iso_env.pip_install('openai-whisper')
venv = iso_env.environment()
subprocess.run(['whisper', '--help'], env=venv, shell=True, check=True)

Why not just use venv directly?

You can! But this package is a better abstraction and solves the platform specific footguns that venv makes you go through to work correctly on all platforms.

Background

After making my first major AI project transcribe-anything I quickly learned that pytorch has a lot of different versions of its library and globally installing the package is an absolute nightmare, especially on Windows. The major problem is that out of the box in Windows, pytorch does not support cuda acceleration, you have to use pip with an --extra-index-url parameter. If this isn't done right the first time, you will get a cpu-only version of pytorch which is tricky to remove from the site-packages directory, requiring the developer to pip uninstall all packages using pytorch and then purge the pip cache.

This is a real world example of how I was able to purge the cpu-pytorch from Windows, which took me a lot of trial and error to figure out.

Without this library, you would have to do something like this to purge cpu-pytorch from the global site-packages

    uninstall = [
        "torch",
        "torchtext",
        "torchdata",
        "torchaudio",
        "torchvision",
        "torch-directml"
    ]
    for package in uninstall:
        subprocess.run(["pip", "uninstall", "-y", package], check=True)
    subprocess.run(["pip", "cache", "purge"], check=True)

...yuck

This means that if I install one tool and force the correct dependencies in, another tool relying on those dependencies will BREAK.

Isn't this just yet another package manager?

If this is a package manager, then so is bash and cmd.exe. Let's get real here. Also, if this library was part of the standard, we might not have needed conda or pipx or any of the other alt package managers to fill in the gaps of pip.

isolated-environment vs pipx

pipx seems like a great solution but has major downsides. One downside is that pipx is pretty global, it's wants to install a tool in a global directory and link it to your local bin, which requires a restart or manually adding the path. Also, if you are depending on two different versions of a tool, then there are going to be conflicts. Additionally, the tool in the pipx directory becomes independent of the package that installed it and requires its own uninstall step, which must be performed manually. And one last final issue with pipx is that creating a virtual environment requires at least one package, before injecting other packages into it. Working around this issue would require someone to create a dummy package just to get the initial virtual environment constructed, before injecting packages into it. This is a big issue with whisper for example, which requires that cuda-pytorch be installed first, to skip the cpu-pytorch it installs by default.

So given all of these limitations of pipx, I created this isolated-environment library which solves all of these problems, specifically:

  1. The virtual environment name and path can be specified by our code, and is initially empty, as God intended it.
  2. The virtual environment can live within your site-packages directory, so if you uninstall your package then the isolated environment will be removed as well.

This solves the problem for transcribe-anything and now all AI dependencies can be installed during runtime in a private environment only accessible to its package that will be uninstalled if the tool is uninstalled. This means no conflicts with other libs due to pytorch cpu vs gpu installs.

The result was pure bliss. You can now install transcribe-anything in your global python/pip directory without having to be concerned about global conflicts with pytorch. As far as I know, no other AI tool does this.

I hope that isolated-environment will help you write great AI software without all of the conflicts that currently plague the python ecosystem that every other AI python tool seems to suffer from.

The downsides

The downside is that it gets a bit trickier to access the tool installed in an isolated-environment. For example, installing transcribe-anything no longer globally installs whisper, which means to test out whisper I have to cd to the correct private environment and activate it before invoking the tool.

Another downside, but this also exists within pipx is that you can't directly call into Python code within the isolated-environment. The only interface that can be used at this point are command-based apis (anything that subprocess.run can invoke). But this is typical of all code that is isolated in its own environment.

Development

Install

  • First time setup
    • clone the repo
    • run ./install
  • To develop software, run . ./activate.sh

Windows

This environment requires you to use git-bash.

Linting

Run ./lint.sh to find linting errors using ruff, flake8 and mypy.

License

This software is free to use for personal and commercial products. However, if you make changes to isolated-environment code you must agree to the following "good-samaritan" stipulations:

  • All changes to isolated-environment MUST be put into a github fork, linked to this github project (https://github.com/zackees/isolated-environment).
    • That means clicking on the fork button on this repo, and then putting your changes into that fork.

This agreement means that isolated-environment can receive additional features from those that benefit from this package, so that others can benefit as well.

This supplemental licensing supersedes any language in the generic license attached. If you merely use isolated-environment as is, without modification, none of this supplemental license applies to you.

Releases

  • 1.0.4 - Added lock()
  • 1.0.0 - Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

isolated-environment-1.0.4.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

isolated_environment-1.0.4-py2.py3-none-any.whl (7.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file isolated-environment-1.0.4.tar.gz.

File metadata

  • Download URL: isolated-environment-1.0.4.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for isolated-environment-1.0.4.tar.gz
Algorithm Hash digest
SHA256 7fa5abd203725f3c01c9526507aaa21c25f59e3fba3556ab2ed1cc51fc58c132
MD5 35703ebc1221f0b4a6ce17ec1fd1a372
BLAKE2b-256 9188845c08c74487c6f373956c6a88a25c636e3b117e5b5924c22357918a3a3f

See more details on using hashes here.

File details

Details for the file isolated_environment-1.0.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for isolated_environment-1.0.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 493ca8aa27e7e022ccd59a3c8da503465b7a0ff9167742068dd6de11ad42e318
MD5 a55f4143d0c37a5cb3829ad69c24525b
BLAKE2b-256 1cfb6a6889d084695f89380ae0308e099281a7e82e927bbd5031805c1271e1bc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page