Skip to content

Instantly share code, notes, and snippets.

@bilalesi
Forked from miraculixx/pytry
Created November 19, 2024 10:53
Show Gist options
  • Save bilalesi/41d0fb71c511dc3d0d8bbd4a10ffb1d6 to your computer and use it in GitHub Desktop.
Save bilalesi/41d0fb71c511dc3d0d8bbd4a10ffb1d6 to your computer and use it in GitHub Desktop.

Revisions

  1. @miraculixx miraculixx created this gist Nov 19, 2024.
    35 changes: 35 additions & 0 deletions pytry
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #!/bin/bash

    function setup() {
    python -m venv .pytry
    source .pytry/bin/activate
    pip install $packages
    }

    function tryimport() {
    toimport=$1
    python -i -c "import $toimport; print($toimport)"
    }

    function cleanup {
    deactivate
    [[ $keep -ne 1 ]] && rm -rf .pytry
    }

    # Parse command-line arguments
    while [[ $# -gt 0 ]]; do
    case "$1" in
    --keep)
    keep=1
    shift # Move to the next argument
    ;;
    *)
    packages+=("$1") # Collect packages
    shift # Move to the next argument
    ;;
    esac
    done

    setup >/dev/null 2>&1
    tryimport ${packages[0]}
    cleanup