-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
swancustomenvironments: If resolved, install requirements using uv
When creating a custom environment, two types of a requirements file can now be provided: 1. High-level requirements, i.e. not fully resolved. These are provided by a file called requirements.in. 2. Fully resolved requirements, i.e. all the necessary packages including their dependencies and specific versions. These are provided by a file called requirements.txt. If (2) is present, the packages will be installed with uv for performance reasons. Since a full resolution has already been done, it is safe to do so (we don't depend on the resolution of uv). If (2) is not present but (1) is, the packages will be installed with pip, probably in a slower way but with the dependency resolution of pip. This guarantees a more stable solution in the medium-long term.
- Loading branch information
1 parent
36d7172
commit 220b804
Showing
3 changed files
with
70 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 25 additions & 4 deletions
29
SwanCustomEnvironments/swancustomenvironments/scripts/builders/venv.sh
100644 → 100755
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,37 @@ | ||
#!/bin/bash | ||
|
||
# Create the environment | ||
python3 -m venv ${ENV_PATH} 2>&1 | ||
# Create a middle layer for installing ipykernel, putting it apart from the user environment | ||
uv venv $SWAN_ENV --seed 2>&1 | ||
source $SWAN_ENV/bin/activate | ||
uv pip install "ipykernel==${IPYKERNEL_VERSION}" | ||
SWAN_PACKAGES_PATH=$(python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])') | ||
deactivate | ||
|
||
if [ "${RESOLVED_REQ}" = true ]; then | ||
uv venv ${ENV_PATH} --seed 2>&1 | ||
else | ||
python -m venv ${ENV_PATH} 2>&1 | ||
fi | ||
|
||
# Activate the environment | ||
_log "Setting up the environment..." | ||
ACTIVATE_ENV_CMD="source ${ENV_PATH}/bin/activate" | ||
eval "${ACTIVATE_ENV_CMD}" | ||
|
||
# Install packages in the environment and the same ipykernel that the Jupyter server uses | ||
# Install user-requested packages in the environment. | ||
# Use uv for better performance if environment is fully resolved; | ||
# Otherwise, use pip for resolution (more reliable long-term). | ||
_log "Installing packages from ${REQ_PATH}..." | ||
pip install -r "${REQ_PATH}" "ipykernel==${IPYKERNEL_VERSION}" 2>&1 | ||
if [ "${RESOLVED_REQ}" = true ]; then | ||
uv pip install -r "${REQ_PATH}" 2>&1 | ||
else | ||
pip install -r "${REQ_PATH}" 2>&1 | ||
fi | ||
if [ $? -ne 0 ]; then | ||
exit 1 | ||
fi | ||
|
||
# Inject middle layer packages into the user environment by adding a .pth file to | ||
# the environment site-packages that contains the path to the middle layer site-packages | ||
USER_PACKAGES_PATH=$(python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])') | ||
echo ${SWAN_PACKAGES_PATH} > ${USER_PACKAGES_PATH}/$(basename $SWAN_ENV).pth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters