Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

workon command doesn't work in Windows PowerShell to activate virtualenv

I have installed virtualenvwrapper-win and when I try this command

workon <envname>

In CMD it works, but not in Windows PowerShell.

In Windows PowerShell I have to do Scripts\activate.ps1 and then I get the envname before the prompt.

Can you please let me know how can I make workon command working in PowerShell?

like image 299
Raj Avatar asked Aug 14 '16 17:08

Raj


People also ask

How do I enable virtual environment in Windows PowerShell?

To activate the virtual environment, go to the directory where you installed it and run the following command by replacing "env" with your environment name. To install any package make sure the virtual environment is active. Use the following command to install the package.

How do I enable virtualenv on Windows?

Setting up virtualenv This can be done by activating the activate script in the Scripts folder. The following command creates an activate. bat batch file after activation. This creates an activate.

What is the command to activate virtualenv?

To install virtualenv, just use pip install virtualenv . To create a virtual environment directory with it, type virtualenv /path/to/directory .

Which command is used to activate virtual environment after creating it?

Now after creating virtual environment, you need to activate it. Remember to activate the relevant virtual environment every time you work on the project. This can be done using the following command: Note: source is a shell command designed for users running on Linux (or any Posix, but whatever, not Windows).


1 Answers

workon is a batch script. If you run it from PowerShell it's launched in a new CMD child process, doing its thing there, then exit and return to the PowerShell prompt. Since child processes can't modify their parent you lose all modifications made by workon.bat when you return to PowerShell.

You basically have two options:

  • Rewrite workon.bat (and the other batch scripts it calls) in PowerShell.

  • Run workon.bat in without exiting from the CMD child process:

    & cmd /k workon <envname>
    

    If you just want a shortcut workon that you can call directly from PowerShell you can wrap that commandline in a function and put the function definition into your PowerShell profile:

    function workon($environment) {
      & cmd /k workon.bat $environment
    }
    

    Use the scriptname with extension here to avoid infinite recursion.

like image 78
Ansgar Wiechers Avatar answered Sep 28 '22 18:09

Ansgar Wiechers