Last active
March 17, 2025 02:16
-
-
Save boxabirds/f09816d41f77ac9fcf0e72d40a5fb547 to your computer and use it in GitHub Desktop.
uvshell and uvinstall pipenv emulations
This file contains hidden or 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
# pipenv is extremely slow but it's very easy to use. | |
# here are two wrapper scripts to emulate pipenv shell and pipenv install | |
# add to your .zshrc file | |
# emulate pipenv shell | |
function uvshell() { | |
if [[ -d .venv ]]; then | |
source .venv/bin/activate || { echo "Failed to activate virtual env"; return 1; } | |
else | |
echo "No virtual environment found. Setting up with uvinstall..." | |
uvinstall "$@" || { echo "Failed to set up environment"; return 1; } | |
# uvinstall already activates, so no need to source again unless it fails | |
fi | |
} | |
# emulate pipenv install | |
function uvinstall() { | |
local python_version="" | |
# Parse arguments for --python flag | |
while [[ $# -gt 0 ]]; do | |
case "$1" in | |
--python) | |
if [[ -n "$2" && "$2" != --* ]]; then | |
python_version="$2" | |
shift 2 | |
else | |
echo "Error: --python requires a version (e.g., 3.10)" | |
return 1 | |
fi | |
;; | |
*) | |
echo "Unknown option: $1. Use --python <version>" | |
return 1 | |
;; | |
esac | |
done | |
if [[ ! -f pyproject.toml ]]; then | |
uv init || { echo "Failed to initialize project"; return 1; } | |
fi | |
if [[ ! -d .venv ]]; then | |
if [[ -n "$python_version" ]]; then | |
uv venv -p "$python_version" || { echo "Failed to create virtual env with Python $python_version"; return 1; } | |
else | |
uv venv || { echo "Failed to create virtual env"; return 1; } | |
fi | |
fi | |
source .venv/bin/activate || { echo "Failed to activate virtual env"; return 1; } | |
if [[ -f requirements.txt ]]; then | |
uv pip install -r requirements.txt || { echo "Failed to install requirements"; return 1; } | |
else | |
echo "No requirements.txt found: no project-specific dependencies installed." | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment