7

I created a virtual environment with python -m venv myenv from the command prompt, but I don't know how to activate it. I tried executing activate.bat from the command prompt but it does not activate.

In other words, I don't see the current path changed to (myenv) C:\Pathname... to indicate that myenv has been activated. When I execute activate.bat the venv is not activated.

Sunny
  • 2,232
  • 7
  • 24
  • 36
  • To clarify, you ran `myenv\Scripts\activate.bat`? – Eryk Sun Aug 02 '15 at 09:41
  • Yes that's correct, am I the only one experiencing this problem? – Sunny Aug 02 '15 at 10:15
  • After running activate.bat, check whether the following prints the path to the virtual environment's python.exe: `python -c "import sys; print(sys.executable)"`. Also check whether `VIRTUAL_ENV` is set to the activated environment path. – Eryk Sun Aug 02 '15 at 10:23
  • It shows `C:\Python34\python.exe`, not the one from the venv. – Sunny Aug 02 '15 at 10:29
  • 1
    Looks like it works on PowerShell, thanks. – Sunny Aug 02 '15 at 10:35
  • To clarify, you switched to using Activate.ps1 in PowerShell? But before you were definitely running cmd.exe and activate.bat didn't work? – Eryk Sun Aug 02 '15 at 10:39
  • Yeah, cmd.exe activate.bat doesn't work, but PowerShell Activate.ps1 works after changing the execution policy to unrestricted. – Sunny Aug 02 '15 at 10:44
  • Activate.ps1 should work with the execution policy set to `RemoteSigned`. Anyway, if it's not too much trouble, could you edit your question to include the activate.bat file that doesn't work? – Eryk Sun Aug 02 '15 at 10:51
  • hmm... no wonder why it didn't work... `activate.bat` is an empty file 0KB! `Activate.Ps1` is not empty though. Don't know why, fresh Windows 10 install and followed the instructions for all the steps from the official python manual. – Sunny Aug 02 '15 at 11:00
  • Check the template file. It should be `C:\Python34\Lib\venv\scripts\nt\activate.bat`. – Eryk Sun Aug 02 '15 at 11:03
  • That template exists 1KB. I just created another venv to test it and it works now in cmd.exe, I see a 1KB `activate.bat`. Don't know why the first venv I created had a 0KB `activate.bat`. – Sunny Aug 02 '15 at 11:10
  • If you can reproduce the problem, file a bug on the issue tracker. Otherwise I wouldn't worry about it. – Eryk Sun Aug 02 '15 at 11:15

1 Answers1

7

This thread it old but I had the same problem today and found a working answer for it. I've been using python 3.6 venv for a few months now without issues but today I ran across a new error message:

C:\test>python -m venv vm
Error: Command '['C:\\test\\vm\\Scripts\\python.exe', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1

I scoured Stack and other resources for an answer but didn't find anything specific to Windows 10. (AskUbunto had a solution that was specific to Linux). I did find breadcrumbs spread out across the InterWebs and pieced this together.

You need this python script: https://bootstrap.pypa.io/get-pip.py

  1. Install your virtual environment as usual but without pip:

    python -m venv virtual --without-pip

This method will create all of the necessary files including the activate bat files.

  1. Copy the get-pip.py file into the virtual\Scripts subdirectory

  2. cd into the Scripts subdirectory and "activate" python Note: the cmd line should name the directory from which python was activated:

    (virtual) C:\test\virtual\Scripts>

(if it says (root), it's activated your core installation)

  1. now execute the script

    C:\test\virtual\Scripts>python get-pip.py

once run, I typed:

(virtual) C:\test\virtual\Scripts>pip freeze

to generate a freeze list and verify proper installation. It should return nothing, no error msg, no freeze list.

  1. Then I installed flask, tried pip freeze and noted the return was only for flask and dependent files:

(virtual) C:\test\virtual\Scripts>pip freeze
click==6.7
Flask==0.12.2
itsdangerous==0.24
Jinja2==2.9.6
MarkupSafe==1.0
Werkzeug==0.12.2

mf.cummings
  • 303
  • 6
  • 20
  • Does anyone know what causes Python/Windows to get into this state? – kas Oct 02 '21 at 04:05
  • Actually, I had a custom Python file named "copy.py" in the root of my Python project. Renaming the file to something unique fixed the issue for me. – kas Oct 02 '21 at 04:35