Files
illogical-impulse/scriptdata/uv/README.md
T
2025-10-08 22:25:47 +08:00

125 lines
5.8 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## Why is this important?
Instead of installing python packages via system package manager, we should install them into virtual environment.
This is important because there has been so many complaints about the failure installing/updating python packages via system package manager, see [#1017](https://github.com/end-4/dots-hyprland/issues/1017).
## How to add/remove python package?
1. Edit `requirements.in`. You may refer to [PyPI](https://pypi.org/) for possible package names.
- See also [uv doc](https://docs.astral.sh/uv/pip/dependencies/#using-requirementsin).
2. Run `uv pip compile requirements.in -o requirements.txt` in this folder.[^1]
[^1]: `requirements.txt` is included in git repo. It's for locking package versions to enhance stability and reproducibility. In fact, including package version lock file in git repo is also common in other places, for example the `package-lock.json` of Node.js projects. See [this stackoverflow question](https://stackoverflow.com/questions/48524417/should-the-package-lock-json-file-be-added-to-gitignore) for a deeper explanation.
## How will the python packages get installed?
- They will be installed to the virtual environment `$ILLOGICAL_IMPULSE_VIRTUAL_ENV`.
- The default value of `$ILLOGICAL_IMPULSE_VIRTUAL_ENV` is `$XDG_STATE_HOME/quickshell/.venv`.
- The default value of `$XDG_STATE_HOME` is `$HOME/.local/state`.
- Currently we use `env = ILLOGICAL_IMPULSE_VIRTUAL_ENV, ~/.local/state/quickshell/.venv` in `~/.config/hypr/hyprland/env.conf` to set this environment variable.[^2]
- See the function `install-python-packages()` defined in `/scriptdata/lib/package-installers.sh` for details.
[^2]: Hyprland seems to have weird problem dealing with recursive variable, so we can not use `$XDG_STATE_HOME/quickshell/.venv` even if we had set `$XDG_STATE_HOME` to `~/.local/state` explicitly, else `$XDG_STATE_HOME` will possibly not get expanded but get recognised as literally `$XDG_STATE_HOME`. This problem never happens for some users, but according to some issues when we were using recursive variable setting in the past, it's possible to happen for other users. Reason unknown.
## How to use the python packages installed through here?
Basically you'll need to activate the virtual environment first:
```bash
source $(eval echo $ILLOGICAL_IMPULSE_VIRTUAL_ENV)/bin/activate
```
It will add the python executable located in the venv to `$PATH` and give it the highest priority.
Run `which python` and you'll understand.
This python executable will also search and use the python package inside the venv,
which enables running any python script or running command provided via python package using the venv.
After that you probably need to deactivate it:
```bash
deactivate
```
### Situation 1: As a single command
**Description:** At someplace which accept a single command,
- run a python script,
- or run a command provided by python package.
Example: In `~/.config/quickshell/ii/screenshot.qml`:
```qml
Process {
id: imageDetectionProcess
command: ["bash", "-c", `${Directories.scriptPath}/images/find_regions.py `
command: ["bash", "-c", `${Directories.scriptPath}/images/find-regions-venv.sh `
+ `--hyprctl `
+ `--image '${StringUtils.shellSingleQuoteEscape(panelWindow.screenshotPath)}' `
+ `--max-width ${Math.round(panelWindow.screen.width * root.falsePositivePreventionRatio)} `
```
In this example, python script `${Directories.scriptPath}/images/find_regions.py` is called and receives some arguments.
#### Solution A: shebang
Add the shebang below to the beginning of python script:
```python
#!/usr/bin/env -S\_/bin/sh\_-c\_"source\_\$(eval\_echo\_\$ILLOGICAL_IMPULSE_VIRTUAL_ENV)/bin/activate&&exec\_python\_-E\_"\$0"\_"\$@""
```
And that's it!
**Note:** This is the simplest solution as it only modifies the shebang of python script.
However:
- It's only for python script, not the command provided by python package.
- P.S. Run the script directly, eg. `./foo.py`, not `python3 foo.py`, or the shebang will be ignored.
- It can not deal with complex argument (e.g. filaname containing spaces) passed to the python script.
- The example above is actually unstable, considering that `--image '${StringUtils.shellSingleQuoteEscape(panelWindow.screenshotPath)}'` could be a rather complex argument.
#### Solution B: bash script as wrapper
For this solution, first make sure the python script is using the shebang `#!/usr/bin/env python3`.
Write a wrapper script in bash.
(Take `find_regions.py` as example, write `find-regions-venv.sh` in the same directory.)
```bash
#!/usr/bin/env bash
# Specify the path of the python script.
# The example below only applies when `find_regions.py` and the wrapper script are under the same folder.
PY_SCRIPT="$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)/find_regions.py"
source $(eval echo $ILLOGICAL_IMPULSE_VIRTUAL_ENV)/bin/activate
"$PY_SCRIPT" "$@"
deactivate
```
### Situation 2: Inside a bash script
Note: the solutions for `Situation 1: As a single command` also apply here; but **not** vice versa.
**Description:**
Inside a bash script,
- run a python script,
- or run a command provided by python package.
**Solution:**
- Add "activation command" before the target line,
- Also add "deactivation command" after the target line.
**Example:**
For running a python script,
take `generate_colors_material.py` as example:
```bash
source "$(eval echo $ILLOGICAL_IMPULSE_VIRTUAL_ENV)/bin/activate"
python3 "$SCRIPT_DIR/generate_colors_material.py" "${generate_colors_material_args[@]}" \
> "$STATE_DIR"/user/generated/material_colors.scss
"$SCRIPT_DIR"/applycolor.sh
```
For running a python script provided by python package,
take `kde-material-you-colors` as example:
```bash
source "$(eval echo $ILLOGICAL_IMPULSE_VIRTUAL_ENV)/bin/activate"
kde-material-you-colors "$mode_flag" --color "$color" -sv "$sv_num"
deactivate
```