Skip to content

Class State Environment

Module for open a virtual environment and clsoe the virtual environment

StateEnv

activate

activate()

Method to activate a virtual environment

Source code in fenv\state_env.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def activate(self):
    """Method to activate a virtual environment"""

    IS_POWER_SHELL = len(os.getenv("PSModulePath", "").split(os.pathsep)) >= 3
    IS_LINUX_OR_WSL = os.path.exists("/proc/sys/kernel/osrelease")

    if ENV_NAME:
        if os_win:
            if IS_POWER_SHELL:
                subprocess.run(
                    [
                        "powershell.exe",
                        "-NoExit",
                        "-Command",
                        rf".\{path_env_win}\activate",
                    ],
                )
            elif os.environ.get("TERM") == "xterm-256color":
                subprocess.run(
                    [
                        "cmd.exe",
                        "/c",
                        rf".\{path_env_win}\activate.bat",
                    ]
                )
            else:
                print(
                    f"It cannot be enabled, but you can run it using the \n {colors.SPRING_GREEN}`{ENV_NAME}\\Scripts\\activate`{colors.ENDC} command"
                )

        elif os_linux:
            if EnvAll().get_terminal_bash():
                if IS_LINUX_OR_WSL:
                    subprocess.run(
                        [
                            "bash.exe",
                            "-c",
                            rf".\{path_env_win}\activate",
                        ],
                    )
                    print(
                        f"Virtual environment {colors.NAVY}{colors.SPRING_GREEN}{ENV_NAME}{colors.ENDC} is enabled"
                    )
                else:
                    subprocess.run(
                        [
                            "bash",
                            "-c",
                            rf".\{path_env_win}\activate",
                        ],
                    )
                    print(
                        f"Virtual environment {colors.NAVY}{colors.SPRING_GREEN}{ENV_NAME}{colors.ENDC} is enabled"
                    )
    else:
        print(
            f"Virtual environment {colors.NAVY}{colors.SPRING_GREEN}{ENV_NAME}{colors.ENDC} is not enabled because it does not exist"
        )