Package: envless
envless: a python utility for running scripts with dependencies declared inlinePython scripts are great and useful, but for simple tasks that require a
dependency or two, it can be annoying to manage virtualenvs. This is especially
annoying if you want to write the scripts to manage your dev environment in python.envlessprovides a self-contained way for python scripts to declare and load
their dependencies. To make the idea clearer, here's an example script that
fetches and prints thepython.orgsite:importenvlessenvless.script_dependencies({"requests":"==2.28.2"},__file__)importrequestsresponse=requests.get("https://python.org")print(response.text)In short, this will run the script, installingrequestsif needed, so that
you don't need to have it preinstalled or set up a virtualenv or anything
first. If this script is calledshow_python.py, then you just runpython show_python.py, and it works!This is heavily inspired by howdenoandgoautomatically determine dependencies
based on the source code rather than declaring them manually in a separate file.InstallationThe only requirement is that you need to be able toimport envlesswhen
you're running the python you want to use for the script. This usually means
that you shouldpip install --user envless.If you have multiple versions of python on your system, this means you should
install with the correspondingpip. So if your system default python is 3.8,
but you also have 3.11 installed, and you want to run with 3.11, you might need
topip3.11 install --user envlessand thenpython3.11 <script>to run your
script.envlessshould always support allcurrently supported python versions,
except python3.7, which is about to go end-of-life at the time of writing this
package.How it worksThescript_dependenciesfunction turns the requirements dict into arequirements.txttempfile. It then hashes that tempfile and the running
python version to get a version ID, creates a virtualenv with that ID if
needed, and installs the dependencies into that virtualenv. Then itexecs the
current interpreter with that virtualenv active so that the dependencies are
available to the script.Note that this implies that the dependencies do not contaminate your global
environment, and also that virtualenvs can be shared among scripts with the
same dependency declarations (so don't go messing with them manually).If you want to clear out old envs, they live in your platform's equivalent of~/.local/share/envless/virtualenvs. It should always be safe to delete this
directory if there isn't a script actively running; envs will be recreated as
needed.APIenvless.script_dependencies: the only function in the package you should need
to use from your code.Args:requirements: adictwhere keys are package names that can be installed
viapipand values are version specifiers for those packages (again, any
version specifier thatpipcan handle). If you don't care about the version
you get, just pass""as the version specifier.source_file: the path of the file that is calling this function. You should
always just pass the python builtin variable__file__for this argument.ExamplesSee thetestsdir, which contains scripts that useenvless.LimitationsYour call toenvless.script_dependenciesmust come before you import any code
outside the standard library.envlessonly works well when used in the script that you're using as an
entrypoint. Importing another script that uses envless will end up executing
that other script as the entrypoint, which is probably not what you want. If
you think you might need to import a script that uses envless, you can put the
call toenvless.script_dependenciesinside anif __name__ == "__main__":block (which still must occur prior to any imports outside the standard
library). For example:if__name__=="__main__":importenvlessenvless.script_dependencies({"requests":"==2.28.2"},__file__)importrequestsresponse=requests.get("https://python.org")print(response.text)Note, however, that this means that the script doing the importing is
responsible for providing all the dependencies; in this example,requestswill not be installed automatically if the script is not the entrypoint.