Activating Python Virtual Environment on Windows 11
Asked Answered
C

5

5

I am trying to create a venv virtual enviroment for Python in Window's command prompt. I created the enviroment; however, I am having difficulties using it because when I run the "activate" command it is not working. I think the problem is related to that the virtual enviroment does not have a scripts file like other window machines do, but rahter a bin file which has the activate script. When I run the activate command with the bin in the file directory I still get an error.

enter image description here

I have been trying to solve the problem for the past 4-5 hours and am completely stuck. I tried destroying and reconstructing the virtual enviroment, I tried using different extensions (.bat, .exe, .ps1, and just \activate), and tried using powershell.

Please let me know if you have any ideas what I am doing wrong!

Culbertson answered 31/12, 2022 at 0:44 Comment(1)
E
6

Open a command prompt terminal by either searching command prompt in the Windows search bar, or press the Windows Key + R and enter cmd.

Create the virtual environment in a desired directory using the following command:

python -m venv env

This will create a new folder called env inside the directory where you executed the command.

You can activate the created virtual environment by running the following command in the same directory where you executed the last command:

cd env/Scripts && activate && cd ../../

I hope this helps.

Evante answered 31/12, 2022 at 0:49 Comment(1)
this works in CMD but not powershell ...Orran
V
3

If you are a Windows user, my two cents (if it's worth anything) are as follows:

  1. Always install Python via MSI.
  2. Always use py to create a new venv by running py -3.X -m venv .venv (where ".venv" is the name of the folder/venv directory)

This way, you are allowing the platform to pick the right python distribution for your venv as opposed to you managing it manually (by maintaining portable folders everywhere)

You can also easily switch between different (installed) versions of Python by just changing the 3.X version in the command line. So, for example, you basically can create two venvs that use two different versions of Python and activate them separately to test the same code.

For activating your new venv:

  • Run ./.venv/Scripts/Activate.ps1 via Powershell.
  • Or run ./.venv/Scripts/Activate.bat via cmd.
  • Or on WSL/Linux, simply run source ./.venv/Scripts/activate

Being a Windows user, you could also benefit from using tools like Microsoft Terminal. When Microsoft Terminal is customized using Oh My Posh - depending on your setup, it can show you the current venv you are in at any point. It makes things whole lot easier.

Verse answered 3/12, 2023 at 10:53 Comment(0)
D
0
python -m venv venv 

#To run this command prompt on python terminal to create a virtual environment

venv\Scripts\activate 

#Run this command prompt on python terminal

Dungaree answered 16/2, 2023 at 10:53 Comment(1)
This does not add anything to the existing answer.Zosima
M
0

Another work around would be to use Git bash as a replacement for your command prompt, On windows while Installation of git, just check the box that asks to create a new profile on terminal. It worked for me, Got the hint from here : https://medium.com/@presh_onyee/activating-virtualenv-on-windows-using-git-bash-python-3-7-1-6b4b21640368#:~:text=I%20don't%20like%20Powershell,activate%20to%20activate%20virtualenv%20.

Microsporangium answered 11/11, 2023 at 14:7 Comment(0)
F
0

In PowerShell, I had to use ";" instead of "&&" and "\" instead of "/" as follows:

cd venv\Scripts\; .\activate.ps1 ; cd ..\..

substitute "venv" for whatever you called you virtual environment directory.

Friendship answered 10/1 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.