uv
uv copied to clipboard
is it possible to define a usercustomize hook as part of the uv project?
I want to have a python startup hook (usercustomize.py, or some alternative) that runs whenever an interpreter is launched.
is it possible to define this from a uv pyproject.toml file?
Not right now. What would you use it for?
for now it's to setup some global configurations - mostly can be done using a env file, but some of the values require computation another use cases is applying monkey patching
@ophiry
Try
uv pip install sitecustomize-entrypoints
I use site sitecustomize.py on every project. I co-developed this package that installs a sitecustomize.py in your venv and will run any registered entrypoint upon launch of any python-based executable in your venv.
see https://sitecustomize-entrypoints.readthedocs.io/en/
Note: Ubuntu ships with a non-pip manageable sitecustomize.py, you 'll need to remove that file. The same functionality is provided by this entrypoint: https://github.com/libranet/sitecustomize-apport-hook
my main usecases:
- monkeypatching thirdparty executables
- reading .env-file upon start, https://github.com/libranet/autoread-dotenv
- add the bin-dir to your venv to PATH, without activating the venv beforehand, https://github.com/libranet/autoadd-bindir
Try
uv pip install sitecustomize-entrypointsI use site sitecustomize.py on every project. I co-developed this package that installs a sitecustomize.py in your venv and will run any registered entrypoint upon launch of any python-based executable in your venv.
see https://sitecustomize-entrypoints.readthedocs.io/en/
Note: Ubuntu ships with a non-pip manageable sitecustomize.py, you 'll need to remove that file. The same functionality is provided by this entrypoint: https://github.com/libranet/sitecustomize-apport-hook
my main usecases:
- monkeypatching thirdparty executables
- reading .env-file upon start, https://github.com/libranet/autoread-dotenv
- add the bin-dir to your venv to PATH, without activating the venv beforehand, https://github.com/libranet/autoadd-bindir
interesting - can you provide an example how it's used in uv? (the pyproject.toml part - the doc is poetry specific)
create a new project with a package:
> cd /tmp
> uv init foo --package
> cd foo
in the pyproject.toml, add a project-entrypoint for the name "sitecustomize". and give it a path to a function.
[project.entry-points."sitecustomize"]
foo = "foo:main" # point to main-function in src/foo/__init.py__
> uv sync
> uv run python
Hello from foo!
Python 3.12.6 (main, Sep 9 2024, 22:11:19) [Clang 18.1.8 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
> .venv/bin/python
Hello from foo!
Python 3.12.6 (main, Sep 9 2024, 22:11:19) [Clang 18.1.8 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Add another executable:
> uv add pytest
> .venv/bin/pytest
Hello from foo!
================================================= test session starts ==================================================
platform linux -- Python 3.12.6, pytest-8.3.4, pluggy-1.5.0
rootdir: /tmp/foo
configfile: pyproject.toml
collected 0 items
================================================ no tests ran in 0.01s =================================================
Add another plugin from another package:
> uv add autoadd-bindir
> .venv/bin/python
Hello from foo!
Python 3.12.6 (main, Sep 9 2024, 22:11:19) [Clang 18.1.8 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getenv("PATH")
'/tmp/foo/.venv/bin: ...'
@woutervh The sitecustomize-entrypoints solution won't work if the pyproject.toml doesn't define a [build-system] table, correct? My uv-managed Git repository doesn't have a build system defined, only dependencies. I believe this is why sitecustomize-entrypoints didn't work for me.
I'm trying to add devtools.debug to __builtins__ (as described at https://python-devtools.helpmanual.io/usage/#manual-install), but I haven't found any reasonably easy way to achieve this yet.
Never mind, I created https://github.com/5j9/devtools-sitecustomize and published it on PyPI. Adding it to my development dependencies solved the issue.
@5j9 You can just add it as a dependency. No build-system needed.