Python venv how-to and tutorial: environments on /psi.ch mounts or autodeploy, by production criticality; sharing is the user's responsibility
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
7b5d70cc46
commit
bbe6df0735
2 files changed
+36
-25
No files matched your search
@@ -11,15 +11,38 @@ tags:
|
||||
|
||||
Outside the RPM packages, `pip` in a virtual environment (venv) is the main way to install a Python tool for the control system; pixi is an optional alternative. The policy and the requirements of each install route are in [Python software](../../2._Services_Portfolio/Services/03_devops.md#python-software). This page gives the commands. Do not build environments in your home on a console, see [Where to keep the environment](#where-to-keep-the-environment).
|
||||
|
||||
## Where to keep the environment
|
||||
|
||||
Decide this before you create anything. Where a Python tool lives depends on how critical it is for production and on who uses it:
|
||||
|
||||
| The tool is | Where it lives |
|
||||
| --- | --- |
|
||||
| Critical for production | Installed locally on the console, as installed software: not a venv. See below. |
|
||||
| A shared tool that other people use | A venv on a network drive (a PSI central storage mount under `/psi.ch`), or autodeployed to the [autodeploy](../../2._Services_Portfolio/Services/11_autodeploy.md) folders |
|
||||
| A private tool, only for you | A venv in your user folder under `/psi.ch` |
|
||||
|
||||
On the LC machines (`<facility>-lc`, `<facility>-lca`) and the other development machines, an environment belongs on a `/psi.ch` mount or in the autodeploy folders. **Never keep it locally in your home directory.** On consoles, homes are local to the machine and should hold nothing at all.
|
||||
|
||||
**Sharing is your responsibility.** Controls IT does not provide a facility for sharing tool installations in venvs. If you put a venv on a network drive for others, you look after it. When other people rely on your tool for their work and it has a problem, you are treated as the service provider for that tool.
|
||||
|
||||
**Critical tools** are not run from a venv. They are installed locally on the consoles as part of the installed software: ask for the tool as an RPM, and the package is added to the package list of the host type, see [Python software](../../2._Services_Portfolio/Services/03_devops.md#python-software) and [Request a change to an existing machine](../../2._Services_Portfolio/Services/02_computingandboot.md#request-a-change-to-an-existing-machine). How to package your own tool as an RPM is in [How-To: Build and Deploy RPMs](07_build_deploy_rpms.md). How you package a tool (a package for the index, an RPM or another form) is your choice.
|
||||
|
||||
The examples on this page keep environments in the folder `$VENVS`. Set it once to the place you chose, for example in your user folder under `/psi.ch`:
|
||||
|
||||
export VENVS=/psi.ch/<your folder>/venvs
|
||||
mkdir -p $VENVS
|
||||
|
||||
On your own laptop or desktop, any folder will do.
|
||||
|
||||
## Create and activate a virtual environment
|
||||
|
||||
1. Create the environment. Use `~/venvs/<name>` for a tool, or `.venv` inside a project directory:
|
||||
1. Create the environment. Use `$VENVS/<name>` for a tool, or `.venv` inside a project directory:
|
||||
|
||||
python3 -m venv ~/venvs/mytool
|
||||
python3 -m venv $VENVS/mytool
|
||||
|
||||
2. Activate it in your shell:
|
||||
|
||||
source ~/venvs/mytool/bin/activate
|
||||
source $VENVS/mytool/bin/activate
|
||||
|
||||
3. Upgrade pip before you install anything:
|
||||
|
||||
@@ -27,7 +50,7 @@ Outside the RPM packages, `pip` in a virtual environment (venv) is the main way
|
||||
|
||||
The pip that comes with a new venv on RHEL9 (Python 3.9) is too old: it installs packages with legacy `setup.py` data files, such as `cas-iocutils`, through `setup.py install` and fails with "Permission denied" on `/etc/bash_completion.d`.
|
||||
|
||||
Check that it worked: `which python` and `which pip` print paths inside `~/venvs/mytool/bin/`, and `pip --version` shows the new version. `deactivate` leaves the environment.
|
||||
Check that it worked: `which python` and `which pip` print paths inside `$VENVS/mytool/bin/`, and `pip --version` shows the new version. `deactivate` leaves the environment.
|
||||
|
||||
## Install a Controls Python tool
|
||||
|
||||
@@ -40,15 +63,15 @@ Example: the IOC utilities (`ioc` command, package `cas-iocutils`). Cloning need
|
||||
|
||||
2. Create and activate the environment and upgrade pip, as above:
|
||||
|
||||
python3 -m venv ~/venvs/iocutils
|
||||
source ~/venvs/iocutils/bin/activate
|
||||
python3 -m venv $VENVS/iocutils
|
||||
source $VENVS/iocutils/bin/activate
|
||||
pip install --upgrade pip
|
||||
|
||||
3. Install the tool from the checkout:
|
||||
|
||||
pip install .
|
||||
|
||||
Check that it worked: `which ioc` prints `~/venvs/iocutils/bin/ioc`, and `ioc --help` lists the commands. The dependencies (`PyYAML`, `requests`, `python-dateutil`, `click`) come from the package index. Bash completion for `ioc` is not activated in a venv. To enable it, in the activated environment:
|
||||
Check that it worked: `which ioc` prints `$VENVS/iocutils/bin/ioc`, and `ioc --help` lists the commands. The dependencies (`PyYAML`, `requests`, `python-dateutil`, `click`) come from the package index. Bash completion for `ioc` is not activated in a venv. To enable it, in the activated environment:
|
||||
|
||||
source "$(python -c 'import site; print(site.getsitepackages()[0])')/etc/bash_completion.d/iocutils"
|
||||
|
||||
@@ -60,7 +83,7 @@ Every Controls Python tool states its requirements per install route in its READ
|
||||
|
||||
In an editable install, the environment uses your source files directly, so a change in the code takes effect without reinstalling.
|
||||
|
||||
1. In your project directory (with `pyproject.toml` or `setup.py`), create and activate `.venv` and upgrade pip:
|
||||
1. In your project directory (with `pyproject.toml` or `setup.py`; keep the project itself on a `/psi.ch` mount too, not in your home directory), create and activate `.venv` and upgrade pip:
|
||||
|
||||
cd ~/git/myproject
|
||||
python3 -m venv .venv
|
||||
@@ -101,30 +124,18 @@ A service, timer or cron job does not activate anything. Call the interpreter or
|
||||
|
||||
In a systemd unit, put that path in `ExecStart=`, and the user in `User=` must be able to read the environment. Let the service write to stdout and stderr so its output reaches the journal, see [Logging](../../2._Services_Portfolio/Services/03_devops.md#what-your-software-must-do). How to run software as a service in a container is in [How-To: Use Containers](09_containers.md). The repository [example-systemd-service](https://gitea.psi.ch/controls_it/example-systemd-service) contains an example service unit, a timer that runs it every minute, and an Ansible playbook that installs both.
|
||||
|
||||
## Where to keep the environment
|
||||
|
||||
| Machine | Where |
|
||||
| --- | --- |
|
||||
| Your own machine | your home directory (`~/venvs/<name>`) or the project directory (`.venv`) |
|
||||
| LC machines (`<facility>-lc`, `<facility>-lca`) and other development machines | your home directory or the project directory; keep the requirements file in Git so the environment can be rebuilt on any machine |
|
||||
| Consoles | nowhere: homes on consoles are local to the machine and should hold nothing, and a re-created console loses them |
|
||||
|
||||
A virtual environment is for developing and for your own use. Getting a finished tool onto Controls hosts is done with [autodeploy](../../2._Services_Portfolio/Services/11_autodeploy.md); how you package it (a package for the index, an RPM or another form) is your choice.
|
||||
|
||||
On a console you work from the installed software. If you need a Python tool there as a package, ask for it as an RPM: a missing package is added to the package list of the host type, see [Python software](../../2._Services_Portfolio/Services/03_devops.md#python-software) and [Request a change to an existing machine](../../2._Services_Portfolio/Services/02_computingandboot.md#request-a-change-to-an-existing-machine). How to package your own tool as an RPM is in [How-To: Build and Deploy RPMs](07_build_deploy_rpms.md).
|
||||
|
||||
## Rebuild, remove, or change the Python version
|
||||
|
||||
Remove an environment by deleting its directory; only services or timers that call it by path refer to it:
|
||||
|
||||
deactivate
|
||||
rm -rf ~/venvs/mytool
|
||||
rm -rf $VENVS/mytool
|
||||
|
||||
Rebuild one when it is broken or its requirements changed: remove it, create it again, upgrade pip and run `pip install -r requirements.txt`.
|
||||
|
||||
A venv keeps the Python version that created it. To use another version, create a new environment with that interpreter:
|
||||
|
||||
python3.12 -m venv ~/venvs/mytool-py312
|
||||
python3.12 -m venv $VENVS/mytool-py312
|
||||
|
||||
Which Python versions exist on RHEL9 hosts, according to the Puppet configuration:
|
||||
|
||||
@@ -165,6 +176,6 @@ pixi itself is provided and supported by AIT. Controls-curated standard pixi env
|
||||
|
||||
**`error: could not create '/etc/bash_completion.d': Permission denied`**, after `Using legacy 'setup.py install'`: pip in the environment is too old. Run `pip install --upgrade pip` in the activated environment and install again.
|
||||
|
||||
**`command not found` after the install**: the environment is not active in this shell. Run `source ~/venvs/<name>/bin/activate`, or call `~/venvs/<name>/bin/<command>` by its path. After `pip install --user`, the scripts are in `~/.local/bin`, which must be on your `PATH`.
|
||||
**`command not found` after the install**: the environment is not active in this shell. Run `source $VENVS/<name>/bin/activate`, or call `$VENVS/<name>/bin/<command>` by its path. After `pip install --user`, the scripts are in `~/.local/bin`, which must be on your `PATH`.
|
||||
|
||||
**pip cannot reach the package index**: on Puppet-managed PSI hosts, `/etc/pip.conf` points pip to the PSI PyPI cache, `https://pypi.psi.ch/simple`, or `https://pypi-dmz.psi.ch/simple` in the DMZ, extranet and tier3 zones. `pip config list` shows the setting that applies. Hosts in the machine networks reach this index too, so `pip install` works there. If a host still cannot reach it, contact Controls IT.
|
||||
@@ -1,6 +1,6 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 960 330" role="img" aria-labelledby="t d">
|
||||
<title id="t">What goes into Git and what is rebuilt</title>
|
||||
<desc id="d">Left: the project repository in Gitea holds the source code, pyproject.toml or setup.py, and requirements.txt from pip freeze, or for pixi the files pixi.toml and pixi.lock. Right: on each machine the virtual environment, for example ~/venvs/mytool or .venv in the project directory, is created from these files with python3 -m venv and pip install -r requirements.txt. The environment is never committed and never copied to another machine; it is recreated.</desc>
|
||||
<desc id="d">Left: the project repository in Gitea holds the source code, pyproject.toml or setup.py, and requirements.txt from pip freeze, or for pixi the files pixi.toml and pixi.lock. Right: on each machine the virtual environment, for example $VENVS/mytool or .venv in the project directory, is created from these files with python3 -m venv and pip install -r requirements.txt. The environment is never committed and never copied to another machine; it is recreated.</desc>
|
||||
<style>
|
||||
text { font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; fill: #212121; }
|
||||
.mono { font-family: "JetBrains Mono", SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; }
|
||||
@@ -49,7 +49,7 @@
|
||||
<rect class="tile" x="20" y="20" width="44" height="44" rx="8"/><use href="#i-server" class="ic" x="30" y="30" width="24" height="24"/>
|
||||
<text x="78" y="40" class="h">Rebuilt, not in Git</text>
|
||||
<text x="78" y="60" class="cap">the virtual environment</text>
|
||||
<use href="#i-folder" class="icm" x="20" y="88" width="18" height="18"/><text x="46" y="102" class="mono code">~/venvs/mytool</text>
|
||||
<use href="#i-folder" class="icm" x="20" y="88" width="18" height="18"/><text x="46" y="102" class="mono code">$VENVS/mytool</text>
|
||||
<text x="46" y="128" class="cap">or <tspan class="mono code">.venv</tspan> in the project directory</text>
|
||||
<path d="M20 150h332" stroke="#e1e4f2" stroke-width="1.5"/>
|
||||
<text x="20" y="180" class="mono code">python3 -m venv ...</text>
|
||||
|
||||
|
Before Width: | Height: | Size: 4.9 KiB After Width: | Height: | Size: 4.9 KiB |
Reference in new issue
Block a user