2

Lets assume a I've a dict of venv and the path to the activate.bat files.

    {"env": "C:\\Users\\basic\\Projects\\some\\env\\Scripts\\activate.bat", 

    "test": "C:\\Users\\basic\\python_virtual_envs\\Test\\actiavte.bat"}

Now the user is allowed to input the name of the env they want to activate, and I want a function that can activate this venv.

The code that I've so far. (That does not work)

import subprocess


def activate_venv(path: str) -> None:
    subpocess.run(path, shell=True)

paths = {"env": "C:\\Users\\basic\\Projects\\some\\env\\Scripts\\activate.bat", 
    
        "test": "C:\\Users\\basic\\python_virtual_envs\\Test\\activate.bat"}

env_name = input("Enter the name of the venv you want to activate: ")
activate_venv(paths[env_name])

Related questions I've found

Activate a virtualenv with a Python script The OP is exactly what I'm looking for but none answers makes sense, all the answers are targeted in running a python script using a venv. I'm not looking for, running a python script using a venv, I just want to activate the venv.

basic mojo
  • 128
  • 2
  • 12
  • A venv is just activated in the current shell, right? – Jeppe May 17 '21 at 05:00
  • yeah, when i enter the name of the venv it will be activated in the current shell – basic mojo May 17 '21 at 05:01
  • 1
    Activating a `venv` just sets some variables & modifies others int he current shell. Without a shell, it makes no sense to just activate a venv. – rdas May 17 '21 at 05:02
  • The first answer in the link you posted should work for you. https://stackoverflow.com/a/66167081/5272038 It suggests using the `activate_this.py `file in the `bin` directory of the venv, instead of using the .bat file – Shubham Vasaikar May 17 '21 at 06:15
  • As a side note. If you are doing this for convenience reasons; like maybe activating using the venv name only instead of having to call the bat file everytime. You might want to look into `virtualenvwrapper`. https://pypi.org/project/virtualenvwrapper-win/ – Shubham Vasaikar May 17 '21 at 06:19

0 Answers0