Skip to content

Instantly share code, notes, and snippets.

@itsjfx
Last active February 2, 2025 04:14
Show Gist options
  • Save itsjfx/f4943b2e65afa453bc6f763377279bbe to your computer and use it in GitHub Desktop.
Save itsjfx/f4943b2e65afa453bc6f763377279bbe to your computer and use it in GitHub Desktop.
uv shell (uvs) command
#!/usr/bin/env python3
import os
import shlex
import subprocess
import sys
from itertools import chain
from pathlib import Path
def find_venv(path):
for parent in chain([path], path.parents):
venv = parent / '.venv'
if venv.exists():
return venv
def list_executables(path):
for file in path.iterdir():
if file.is_file() and os.access(file, os.X_OK):
yield file
def main():
if venv := os.environ.get('VIRTUAL_ENV'):
raise Exception(f'Already in virtual env: {venv}')
venv = find_venv(Path.cwd())
if not venv:
raise Exception('No .venv found')
shell = os.environ.get('SHELL', '/bin/zsh')
shell = shlex.quote(shell)
script = venv / 'bin' / 'activate'
script = shlex.quote(str(script.absolute()))
commands = [f.name for f in list_executables(venv / 'bin')]
print('Commands available:', ' '.join(commands), file=sys.stderr)
subprocess.run([shell, '-i', '-c', f'source {script} && exec {shell}'])
if __name__ == '__main__':
sys.exit(main())
@itsjfx
Copy link
Author

itsjfx commented Feb 2, 2025

I have it stored in my PATH as uvs here: https://github.com/itsjfx/dotfiles/blob/master/bin/uvs
and have my status bar write 🐍folder to keep track

Can't wait for this to be part of uv as the script has a slight delay 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment