Skip to content

Instantly share code, notes, and snippets.

@ines

ines/.zshrc

Last active Feb 28, 2021
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@damosuzuki damosuzuki commented Nov 10, 2020

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

@GarrettMooney

This comment has been minimized.

Copy link

@GarrettMooney 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

This comment has been minimized.

Copy link

@fonnesbeck 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