Skip to main content

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

Project description

isolated-environment

Linting MacOS_Tests Ubuntu_Tests Win_Tests

pip install 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 incompatibilities within and between AI apps.

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')
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)

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.

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 project.
    • 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.

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.2.tar.gz (16.5 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.2-py2.py3-none-any.whl (6.8 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: isolated-environment-1.0.2.tar.gz
  • Upload date:
  • Size: 16.5 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.2.tar.gz
Algorithm Hash digest
SHA256 69de73f46642555b3e8b936a68b25cff8f2bfc61e0fe28ad46512a92567b93c3
MD5 56a95414c0eb72b7540317c2b9239dc0
BLAKE2b-256 74e68b2c575e349fe4b7a5dc5527c7047f15e2c1bad2ed754e2a8992ad2cfb18

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for isolated_environment-1.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 56ef0ca2fa4af68a2f7c367679620ccb4e870337157d1578de1358555574ca32
MD5 f304ef2c6339c59f8f3baa1c99713cef
BLAKE2b-256 97ae858d1bc98f0dd0a88ae40e9e3fdf2900a490ec69ac2f8b39c3843839af44

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