Skip to content

Commit f95560c

Browse files
authored
Update Create_WSL_Python3.11_VirtualEnv_.venv.py
1 parent 8906202 commit f95560c

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

Create_WSL_Python3.11_VirtualEnv_.venv.py

+28-27
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,36 @@
66
import sys
77

88
def is_wsl():
9-
try:
10-
with open("/proc/version", "r") as f:
11-
return "microsoft" in f.read().lower()
12-
except FileNotFoundError:
13-
return False
9+
    try:
10+
        with open("/proc/version", "r") as f:
11+
            return "microsoft" in f.read().lower()
12+
    except FileNotFoundError:
13+
        return False
1414

1515
def create_virtualenv():
16-
cwd = os.getcwd()
17-
venv_path = os.path.join(cwd, ".venv")
18-
19-
command = f"python3.11 -m venv {venv_path}"
20-
21-
try:
22-
print(f"Creating virtual environment at: {venv_path}")
23-
subprocess.run(command, shell=True, check=True)
24-
print(f"Virtual environment '.venv' created successfully in {cwd}.")
25-
26-
print("\nThe virtual environment is activated!")
16+
    # Use the Linux native home directory
17+
    home_dir = os.path.expanduser("~")
18+
    venv_path = os.path.join(home_dir, ".venv")
19+
   
20+
    # Modified command to use /usr/bin/python3.10 with --system-site-packages
21+
    command = f"/usr/bin/python3.11 -m venv --system-site-packages {venv_path}"
22+
   
23+
    try:
24+
        print(f"Creating virtual environment at: {venv_path}")
25+
        subprocess.run(command, shell=True, check=True)
26+
        print("Virtual environment '.venv' created successfully.")
27+
       
28+
        print("\nThe virtual environment is activated!")
29+
        shell = os.environ.get("SHELL", "/bin/bash")
30+
        subprocess.run(f"exec {shell} --rcfile {venv_path}/bin/activate", shell=True)
2731

28-
shell = os.environ.get("SHELL", "/bin/bash")
29-
subprocess.run(f"exec {shell} --rcfile {venv_path}/bin/activate", shell=True)
30-
31-
except subprocess.CalledProcessError as e:
32-
print(f"Error while creating virtual environment: {e}")
33-
sys.exit(1)
32+
    except subprocess.CalledProcessError as e:
33+
        print(f"Error while creating virtual environment: {e}")
34+
        sys.exit(1)
3435

3536
if __name__ == "__main__":
36-
if not is_wsl():
37-
print("This script must be run inside WSL2. Exiting...")
38-
sys.exit(1)
39-
40-
create_virtualenv()
37+
    if not is_wsl():
38+
        print("This script must be run inside WSL2. Exiting...")
39+
        sys.exit(1)
40+
   
41+
    create_virtualenv()

0 commit comments

Comments
 (0)