Skip to content

Instantly share code, notes, and snippets.

@Xnuvers007
Created July 21, 2025 06:27
Show Gist options
  • Save Xnuvers007/7d7c69f8162f706ec6b44616654f7bbf to your computer and use it in GitHub Desktop.
Save Xnuvers007/7d7c69f8162f706ec6b44616654f7bbf to your computer and use it in GitHub Desktop.
#!/bin/bash
# === macOS/Linux Script ===
# Check if the virtual environment exists
if [ ! -d "venv" ]; then
echo "Creating virtual environment in the current directory..."
python3 -m venv venv
if [ $? -ne 0 ]; then
echo "Failed to create virtual environment."
exit 1
fi
else
echo "Virtual environment already exists."
fi
# Activate the virtual environment
source ./venv/bin/activate
# Optional: Keep the shell open (only useful if run from a terminal emulator directly)
$SHELL
@echo off
REM === Windows Script ===
REM Check if the virtual environment exists
if not exist "venv" (
echo Creating virtual environment in the current directory...
python -m venv venv
if errorlevel 1 (
echo Failed to create virtual environment.
exit /b 1
)
) else (
echo Virtual environment already exists.
)
REM Activate the virtual environment
call .\venv\Scripts\activate.bat
REM Keep shell open after activation
cmd /K
@echo off
REM Check if the current directory has a venv folder
if not exist "venv" (
echo Creating virtual environment in the current directory...
python -m venv venv
call .\venv\Scripts\activate.bat
) else (
echo Virtual environment already exists.
call .\venv\Scripts\activate.bat
)
REM Keep the shell open after activation
cmd /K ".\venv\Scripts\activate.bat"
import os, sys, subprocess, platform
venv_dir = "venv"
def create_venv():
if not os.path.isdir(venv_dir):
print("Creating virtual environment...")
subprocess.check_call([sys.executable, "-m", "venv", venv_dir])
else:
print("Virtual environment already exists.")
def activate_venv():
system = platform.system()
if system == "Windows":
activate_script = os.path.join(venv_dir, "Scripts", "activate.bat")
subprocess.call(["cmd.exe", "/K", activate_script])
else:
activate_script = os.path.join(venv_dir, "bin", "activate")
subprocess.call([os.environ.get("SHELL", "/bin/bash"), "-i", "-c", f"source {activate_script} && exec $SHELL"])
if __name__ == "__main__":
try:
create_venv()
activate_venv()
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment