Skip to content

Instantly share code, notes, and snippets.

@pwalkr
Created October 2, 2024 02:11
Show Gist options
  • Save pwalkr/e20335f174fed0e03f4e50b34a44e74b to your computer and use it in GitHub Desktop.
Save pwalkr/e20335f174fed0e03f4e50b34a44e74b to your computer and use it in GitHub Desktop.
Self-virtualizing python script
import os
import subprocess
import sys
if 'VIRTUAL_ENV' not in os.environ:
os.environ['VIRTUAL_ENV'] = os.path.realpath('.venv') # Replace .venv with the virtual environment to load
virtual_python = os.environ['VIRTUAL_ENV'] + '/bin/python'
subprocess.call([virtual_python] + sys.argv)
sys.exit()
# import only_available_in_venv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment