Package: isolated-environment
isolated-environmentThis is a package isolation library designed specifically for AI developers to solve the problems
of AI dependency conflicts introduced by the variouspytorch/tensorflow/etc incompatibilities within and between AI apps.pipinstallisolated-environmentIt moves the install of your chosen dependencies frominstall timetoruntime. The benefit of this is that you can query the system
and make choices on what needs to be installed. For example inpipyou can't conditionally install packages based on whethernvidia-smihas
been installed (indicatingcudaacceleration), but withisolated-environmentthis is straightfoward.It also works for any other complex dependency chain. I made this library becausecondahas significant problems and messes up the system
on Windows with its own version of git-bash, standardpipdoesn't support
implicit--extra-index-urlso 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 oldpip.Instead of having your complex, version conflicting dependencies in yourrequirements.txtfile, 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 (liketranscribe-anything)
and are dependent on whethercudais 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 installpytorchwith 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 onlypytorch.In plain words, this package allows you to install your AI apps globally without having to worry aboutpytorchdependency conflicts.Example:frompathlibimportPathimportsubprocessfromisolated_environmentimportisolated_environment_runTENSOR_VERSION="2.1.2"CUDA_VERSION="cu121"EXTRA_INDEX_URL=f"https://download.pytorch.org/whl/{CUDA_VERSION}"HERE=Path(os.path.abspath(os.path.dirname(__file__)))venv_path=Path(HERE)/"whisper-venv"requirements=["whisper-whisper",f"torch=={TENSOR_VERSION}+{CUDA_VERSION}--extra-index-url{EXTRA_INDEX_URL}"]cmd_list=["whisper","--help"]# Note that shell=False, universal_newlines=True, capture=Truecp:subprocess.CompletedProcess=isolated_environment_run(env_path=venv_path,requirements=requirements,cmd_list=cmd_list)print(cp.stdout)Why not just usevenvdirectly?You can! But this package is a better abstraction and solves the platform specific footguns thatvenvmakes you go through to work correctly on all platforms.BackgroundAfter making my first major AI projecttranscribe-anythingI quickly learned thatpytorchhas 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 supportcudaacceleration, you have to usepipwith an--extra-index-urlparameter. If this isn't
done right the first time, you will get a cpu-only version of pytorch which is tricky to remove from thesite-packagesdirectory, requiring
the developer topip uninstallall packages usingpytorchand then purge thepipcache.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 globalsite-packagesuninstall=["torch","torchtext","torchdata","torchaudio","torchvision","torch-directml"]forpackageinuninstall:subprocess.run(["pip","uninstall","-y",package],check=True)subprocess.run(["pip","cache","purge"],check=True)...yuckThis means that if I install one tool and force the correct dependencies in, another tool relying on those dependencies willBREAK.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 neededcondaorpipxor any of the other alt package managers to fill in the gaps ofpip.isolated-environmentvspipxpipxseems like a great solution but has major downsides. One downside is thatpipxis 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 thepipxdirectory becomes independent
of the package that installed it and requires its own uninstall step, which must be performed manually. And one last final issue withpipxis 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 withwhisperfor example, which requires that cuda-pytorch be installed first, to skip the cpu-pytorch it installs by default.So given all of these limitations ofpipx, I created thisisolated-environmentlibrary which solves all of these problems, specifically:The virtual environment name and path can be specified by our code, and is initially empty, as God intended it.The virtual environment can live within yoursite-packagesdirectory, so if you uninstall your package then the isolated environment will be removed as well.This solves the problem fortranscribe-anythingand 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 topytorchcpu vs gpu installs.The result was pure bliss. You can now installtranscribe-anythingin your globalpython/pipdirectory without having to be concerned
about global conflicts withpytorch. As far as I know, no other AI tool does this.I hope thatisolated-environmentwill 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 downsidesThe downside is that it gets a bit trickier to access the tool installed in anisolated-environment. For example, installingtranscribe-anythingno longer globally installswhisper, which means to test outwhisperI have tocdto the correct private environment and activate it before invoking the tool.Another downside, but this also exists withinpipxis that you can't directly call into Python code within theisolated-environment. The only interface that can be used
at this point are command-based apis (anything thatsubprocess.runcan invoke). But this is typical of all code that is isolated in its own environment.DevelopmentInstallFirst time setupclone the reporun./installTo develop software, run. ./activate.shWindowsThis environment requires you to usegit-bash.LintingRun./lint.shto find linting errors usingruff,flake8andmypy.LicenseThis software is free to use for personal and commercial products. However, if you make changes toisolated-environmentcode you must agree to the
following "good-samaritan" stipulations:All changes toisolated-environmentMUSTbe 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 thatisolated-environmentcan 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 useisolated-environmentas is, without modification,
none of this supplemental license applies to you.Releases1.3.1 - Update readme.1.3.0 - Marks a new interface.1.2.7 - Please useisolated_environment_run()instead ofisolated_environment. The latter has
footguns when using Linux when invokingpythonandshell=True1.2.6 - Update readme1.2.4 - Now support more build options, instead of just --extra-index-url.1.2.3 - All builds green with complex dependencies!1.2.2 - Tested and fixed complex semversion + build number for isolated_environment1.2.1 - Fixesisolated_environment()not installing deps correctly on first go1.2.0 - Now just useisolated_environment(), more simple.1.0.6 -exists->installed(), addspip_list(), addsclean()1.0.5 - Addedexists()1.0.4 - Addedlock()1.0.0 - Initial release