Skip to content

Instantly share code, notes, and snippets.

@SerhatTeker
Created May 10, 2022 16:02
Show Gist options
  • Save SerhatTeker/899461f3bcf7b82e115f3ff01a49b21d to your computer and use it in GitHub Desktop.
Save SerhatTeker/899461f3bcf7b82e115f3ff01a49b21d to your computer and use it in GitHub Desktop.
Automate Python Virtual Environment with a Script - https://tech.serhatteker.com/post/2022-04/automate-python-virtualenv
#!/usr/bin/env bash
# -*- coding: utf-8 -*-
# vim: set ft=sh et ts=4 sw=4 sts=4:
# =================================================================================================
#
# Copyright 2022 Serhat Teker <me@serhatteker.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# File : ve
# Descrtiption : Toggle theme mode on Ubuntu 20.04 LTS and MacOS Monterey 12.3
# Author : Serhat Teker <me@serhatteker.com>
# Date : 2022-05-10T18:58:06+0300 | 1652198286
# Last Modified Date : 2022-05-10T18:58:06+0300 | 1652198286
# Notes : Check wheter virtual environment exists in current working directory. If not, create and activate one. If already in a virtual environment warn about it.
# Usage :
#
# Make it executable and put it in your $PATH. Or put it in your zsh/bash/fish functions.
# $ chmod +x ./ve && mv ./ve $HOME/.local/bin
#
# How to run:
# Without arguments it will create virtualenv named `.venv` with `python3.8` version
# $ ve
# or for a specific python version
# $ ve python3.9
# or for a specific python version and environment name;
# $ ve python3.9 ./.venv-diff
#
# For detailed tutorial look at the post:
# https://tech.serhatteker.com/post/2022-04/automate-python-virtualenv
# =================================================================================================
# Bash safeties: exit on error, no unset variables, pipelines can't hide errors
set -o errexit
set -o nounset
set -o pipefail
# Logical conditions:
# 0. If not already in virtualenv:
# 0.1. If virtualenv already exists activate it,
# 0.2. If not create it with global packages, update pip then activate it
# 1. If already in virtualenv: just give info
ve() {
local py=${1:-python3.8} # default python version is python3.8
local venv="${2:-./.venv}" # default virtual environment name is .venv
local bin="${venv}/bin/activate"
# If not already in virtualenv
# $VIRTUAL_ENV is being set from $venv/bin/activate script
if [ -z "${VIRTUAL_ENV}" ]; then
if [ ! -d ${venv} ]; then
echo "Creating and activating virtual environment ${venv}"
${py} -m venv ${venv} --system-site-package
echo "export PYTHON=${py}" >> ${bin} # overwrite ${python} on .zshenv
source ${bin}
echo "Upgrading pip"
${py} -m pip install --upgrade pip
else
echo "Virtual environment ${venv} already exists, activating..."
source ${bin}
fi
else
echo "Already in a virtual environment!"
fi
}
ve "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment