Skip to content

Instantly share code, notes, and snippets.

@ines
Last active February 28, 2021 19:42
  • Star 18 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ines/c00535e6ac294750ddba95fd24a77db5 to your computer and use it in GitHub Desktop.
Command to activate / create Python virtual environmment
function venv {
default_envdir=".env"
envdir=${1:-$default_envdir}
if [ ! -d $envdir ]; then
python -m venv $envdir
pip install ipython black flake8
echo -e "\x1b[38;5;2m✔ Created virtualenv $envdir\x1b[0m"
fi
source $envdir/bin/activate
export PYTHONPATH=`pwd`
echo -e "\x1b[38;5;2m✔ Activated virtualenv $envdir\x1b[0m"
python --version
}
@damosuzuki
Copy link

Thanks! Do you need to activate the environment before you install dependencies on line 7?

@GarrettMooney
Copy link

GarrettMooney commented Nov 10, 2020

I like the function a lot!

I don't think line 7 installs to the virtualenv (activate then pip freeze to check).

Maybe something like the below would install to the venv:

function venv {
    default_envdir=".env"
    envdir=${1:-$default_envdir}

    if [ ! -d $envdir ]; then
        python3.7 -m venv $envdir
        echo -e "\x1b[38;5;2m✔ Created virtualenv $envdir\x1b[0m"
        source $envdir/bin/activate
        echo -e "\x1b[38;5;2m✔ Activated virtualenv $envdir\x1b[0m"
        pip install ipython black flake8
        echo -e "\x1b[38;5;2m✔ Packages installed"
    fi
    if [ -d $envdir ]; then
        source $envdir/bin/activate
        echo -e "\x1b[38;5;2m✔ Activated virtualenv $envdir\x1b[0m"
    fi
    export PYTHONPATH=`pwd`
    python --version
}

@fonnesbeck
Copy link

fonnesbeck commented Nov 16, 2020

Here's a fish port of the same script:

function venv
    set envdir $argv[1]

    if not test -d $envdir
        command python3 -m venv $envdir
        command pip3 install numpy pandas seaborn
        echo -e "\x1b[38;5;2m✔ Created virtualenv $envdir\x1b[0m"
    end
    source $envdir/bin/activate.fish
    set PYTHONPATH "pwd"
    echo -e "\x1b[38;5;2m✔ Activated virtualenv $envdir\x1b[0m"
    command python --version
end

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