0

I want to activate a python 3.8 environment without having to 'cd' to my environment's path in windows 10. If I use commands like path/to/my/env/Scripts/activate or path/to/my/env/Scripts/activate.bat does not activate my environment. To activate my environment I am forced to use two commands -

  1. cd path\to\my\env\Scripts\
  2. ./activate
  3. cd back\to\my\project\dir

I don't want to change my current project working directory every time to activate the environment in windows 10 powershell. In Linux i can use 1 single source path/to/my/env/bin command. It allows me to stay in my current project directory and activate the environment using its path address. How can achieve similar in windows 10, activating a python environment with 1 single command?

enter image description here

In above picture I get green colored name of my environment once it is successfully activated.

YadneshD
  • 396
  • 2
  • 12
  • [`.\myenv\Scripts\activate`](https://stackoverflow.com/a/69541372/12446721) may help? – imxitiz Jun 05 '22 at 12:27
  • 2
    Does this answer your question? [How to activate virtual environment from Windows 10 command prompt?](https://stackoverflow.com/questions/46896093/how-to-activate-virtual-environment-from-windows-10-command-prompt) – imxitiz Jun 05 '22 at 12:29
  • "If I use commands like `path/to/my/env/Scripts/activate.bat` does not activate my environment" - sure it does. How are you running this? Are you running these commands from some other process? Or is the path perhaps relative and are you unaware of the working directory of the process you're issuing the command from? – Grismar Jun 05 '22 at 12:29
  • @Xitiz No. It does'nt work. because '.' in your command means my environment is in my working directory. But it is not in same directory. It is in fact in different dir. – YadneshD Jun 05 '22 at 12:40
  • @Grismar It does not work. I have added a screenshot for your reference – YadneshD Jun 05 '22 at 12:44
  • The screenshot helps, I've provided an answer and updated your title (I missed that you were running interactively from PowerShell, apologies) – Grismar Jun 05 '22 at 22:45

1 Answers1

1

Running something like this from PowerShell:

C:\Users\Admin\Desktop\pyenv\Scripts\activate.bat

It works, but doesn't have the effect you expect. Running a batch file from PowerShell will launch a new instance of cmd, which will get the activated environment - but once the batch file completes, the cmd process is done and will be terminated, taking the modified environment variables with it.

You could write a batch file like this and it would work (from both Command Prompt and PowerShell) as expected:

call C:\Users\Admin\Desktop\pyenv\Scripts\activate.bat
python myscript.py

It would run myscript.py in the intended environment.

Running just the batch file activation command (with path) from a running Command Prompt works in a way that you probably expected to work in PowerShell as well. It modifies the current environment and allows you to interactively run commands in it afterwards, until you deactivate.

However, if you want to change the current environment on the current PowerShell prompt, you need to run (this is the answer):

C:\Users\Admin\Desktop\pyenv\Scripts\activate.ps1

i.e. run the .ps1 PowerShell script, don't run the .bat Command Prompt script.

Note: something that may cause this confusion is that Windows Commmand Prompt automatically runs script.bat if the script command is entered (and there are no other script.___ files that have an extension that comes before .bat in the PATHEXT variable); however, PowerShell finds the script (no extension) file and settles for that. You can see that this is the case by running:

Get-Command C:\Users\Admin\Desktop\pyenv\Scripts\activate

PowerShell does also use PATHEXT, but matches the filename without extension before it and that file is there (it's the Bash script):

Write-Host $env:PATHEXT

(more on PowerShell's use of environment variables here)

Grismar
  • 27,561
  • 4
  • 31
  • 54