Skip to content

Instantly share code, notes, and snippets.

@SerhatTeker
Last active August 23, 2026 21:34
Show Gist options
  • Select an option

  • Save SerhatTeker/899461f3bcf7b82e115f3ff01a49b21d to your computer and use it in GitHub Desktop.

Select an option

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
# Description : Create/activate a Python virtual environment in the current directory
# Author : Serhat Teker <me@serhatteker.com>
# Date : 2022-05-10T18:58:06+0300 | 1652198286
# Last Modified Date : 2026-08-24T00:32:25+0300 | 1787520745
# Notes : Checks whether a virtualenv exists in the current working directory.
# If not, creates and activates one. If already inside a virtualenv,
# warns 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.9` version
# $ ve
# or for a specific python version
# $ ve python3.11
# or for a specific python version and environment name;
# $ ve python3.11 ./.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.9}" # default python version is python3.9
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-packages
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