-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvenv.py
More file actions
executable file
·40 lines (32 loc) · 1.15 KB
/
venv.py
File metadata and controls
executable file
·40 lines (32 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3
"""
mkvenv.py — Create or reuse a Python virtual environment in the current directory.
Default directory: .venv
Usage:
$ python mkvenv.py
$ source .venv/bin/activate
"""
import os
import subprocess
import venv
def create_or_use_venv(venv_dir=".venv"):
if os.path.exists(venv_dir):
print(f"[i] Reusing existing virtual environment at: {venv_dir}")
else:
print(f"[+] Creating virtual environment in: {venv_dir}")
try:
venv.create(venv_dir, with_pip=True)
print("[✓] Virtual environment created.")
except Exception as e:
print(f"[✗] Failed to create virtual environment: {e}")
return
python_bin = os.path.join(venv_dir, "bin", "python")
try:
print("[*] Upgrading pip…")
subprocess.check_call([python_bin, "-m", "pip", "install", "--upgrade", "pip"])
print("[✓] pip upgraded successfully.")
except subprocess.CalledProcessError as e:
print(f"[!] pip upgrade failed: {e}")
print(f"\n[→] Activate it using:\n source {venv_dir}/bin/activate")
if __name__ == "__main__":
create_or_use_venv()