156

I created a virtualenv around my project, but when I try to activate it I cannot. It might just be syntax or folder location, but I am stumped right now.

You can see below, I create the virtualenv and call it venv. Everything looks good, then I try to activate it by running source venv/bin/activate

I'm thinking it might just have to do with my system path, but not sure what to point it to (I do know how to edit the path). I'm on python 7 / windows os, virtual env 2.2.x

Processing dependencies for virtualenv
Finished processing dependencies for virtualenv

c:\testdjangoproj\mysite>virtualenv --no-site-packages venv
The --no-site-packages flag is deprecated; it is now the default behavior.
Using real prefix 'C:\\Program Files (x86)\\Python'
New python executable in venv\Scripts\python.exe
File venv\Lib\distutils\distutils.cfg exists with different content; not overwri
ting
Installing setuptools.................done.
Installing pip...................done.

c:\testdjangoproj\mysite>source venv/bin/activate
'source' is not recognized as an internal or external command,
operable program or batch file.

c:\testdjangoproj\mysite>source venv/bin/activate
'source' is not recognized as an internal or external command,
operable program or batch file.

c:\testdjangoproj\mysite>source mysite/bin/activate
'source' is not recognized as an internal or external command,
operable program or batch file.

c:\testdjangoproj\mysite>
mrj
  • 605
  • 7
  • 14
user1157538
  • 1,563
  • 2
  • 10
  • 4

26 Answers26

390

source is a shell command designed for users running on Linux (or any Posix, but whatever, not Windows).

On Windows, virtualenv creates a .bat/.ps1 file, so you should run venv\Scripts\activate instead (per the virtualenv documentation on the activate script).

Just run activate, without an extension, so the right file will get used regardless of whether you're using cmd.exe or PowerShell.

John Flatness
  • 29,079
  • 5
  • 71
  • 76
23

I was also facing the same issue in my Windows 10 machine. What steps i tried were:

Go to andconda terminal Step 1

pip3 install -U pip virtualenv

Step 2

virtualenv --system-site-packages -p python ./venv

or

virtualenv --system-site-packages -p python3 ./venv

Step 3

.\venv\Scripts\activate

You can check it via spider tool in anaconda by typing import tensorflow as tf

gerwitz
  • 976
  • 8
  • 12
21

I had the same problem. I was using Python 2, Windows 10 and Git Bash. Turns out in Git Bash you need to use:

 source venv/Scripts/activate
jainilvachhani
  • 378
  • 4
  • 12
  • 2
    I have a Windows computer and using the Git Bash terminal was the only terminal that worked for me. (The other terminals I tried to use was Command Prompt and the VS Code terminal.) – Gwen Au Oct 16 '18 at 17:00
17
  1. For activation you can go to the venv your virtualenv directory by cd venv.

  2. Then on Windows, type dir (on unix, type ls). You will get 5 folders include, Lib, Scripts, tcl and 60

  3. Now type .\Scripts\activate to activate your virtualenv venv.

Your prompt will change to indicate that you are now operating within the virtual environment. It will look something like this (venv)user@host:~/venv$.

And your venv is activated now.

Jerome
  • 43
  • 1
  • 5
Gautam Kumar
  • 996
  • 10
  • 16
6

Ensure venv is there and just follow the commands below. It works in Windows 10.

Go to the path where you want your virtual enviroments to reside:

> cd <my_venv_path>

Create the virtual environment named "env":

> python -m venv env 

Add the path to the git ignore file (optional):

> echo env/ >> .gitignore

Activate the virtual env:

> .\env\Scripts\activate
MrBean Bremen
  • 7,785
  • 3
  • 13
  • 30
  • # python -m venv venv /usr/bin/python: No module named venv # python3 -m venv venv # source activate -bash: activate: No such file or directory root@t# source venv/bin/activate (venv) root@testdocker:~/r# – Vineeth sivaraman May 23 '20 at 09:30
  • worked for me in windows 10 cmd – Tokci Feb 08 '21 at 09:17
5

For windows, type "C:\Users\Sid\venv\FirstProject\Scripts\activate" in the terminal without quotes. Simply give the location of your Scripts folder in your project. So, the command will be location_of_the_Scripts_Folder\activate.enter image description here

3
  1. Open your powershell as admin
  2. Enter "Set-ExecutionPolicy RemoteSigned -Force
  3. Run "gpedit.msc" and go to >Administrative Templates>Windows Components>Windows Powershell
  4. Look for "Activate scripts execution" and set it on "Activated"
  5. Set execution directive to "Allow All"
  6. Apply
  7. Refresh your env
2

I have a hell of a time using virtualenv on windows with git bash, I usually end up specifying the python binary explicitly.

If my environment is in say .env I'll call python via ./.env/Scripts/python.exe …, or in a shebang line #!./.env/Scripts/python.exe;

Both assuming your working directory contains your virtualenv (.env).

ThorSummoner
  • 12,194
  • 11
  • 114
  • 129
  • also, some things don't behave correctly *cough _ansible_ cough* when the virtualenv isn't activated, so if things are weird you may have to activate the venv anyway :( – ThorSummoner Jun 18 '18 at 20:15
2

You can run the source command on cygwin terminal

Paras Singh
  • 323
  • 1
  • 3
  • 11
2

source command is officially for Unix operating systems family and you can't use it on windows basically. instead, you can use venv\Scripts\activate command to activate your virtual environment.

Made a Screenshot of how venv\Scripts\activate command works correctly

Mohammad Heydari
  • 2,355
  • 1
  • 21
  • 30
2

open the folder with any gitbash console. for example using visualCode and Gitbash console program: 1)Install Gitbash for windows

2) using VisualCode IDE, right click over the project open in terminal console option

3) on window console in Visualcode, looking for a Select->default shell and change it for Gitbash

4)now your project is open with bash console and right path, put source ./Scripts/activate

btw : . with blank space = source

enter image description here

Calos
  • 1,597
  • 15
  • 26
1

A small reminder, but I had my slashes the wrong way on Win10 cmd. According to python documentation the activate command is: C:\> <venv>\Scripts\activate.bat When you're browsing directories it's e.g. cd .env/Scripts

So to create my venv I used python -m venv --copies .env and to activate .env\Scripts\activate.bat

louisav
  • 342
  • 1
  • 3
  • 23
1

If you see the 5 folders (Include,Lib,Scripts,tcl,pip-selfcheck) after using the virtualenv yourenvname command, change directory to Scripts folder in the cmd itself and simply use "activate" command.

FalconUA
  • 7,927
  • 5
  • 28
  • 61
1

if you already cd your project type only in windows 10

Scripts/activate

That works for me:)

Bernardo Olisan
  • 559
  • 1
  • 13
1

If wants to open virtual environment on Windows then just remember one thing on giving path use backwards slash not forward.

This is right:

D:\xampp\htdocs\htmldemo\python-virtual-environment>env\Scripts\activate

This is wrong:

D:\xampp\htdocs\htmldemo\python-virtual-environment>env/Scripts/activate
heySushil
  • 312
  • 3
  • 12
1

Tried several different commands until I came across:

source venv/Scripts/activate

This did it for me. Setup: Win 10, python 3.7, gitbash. Gitbash might be the culprit for not playing nice with other activate commands.

fuggerjaki61
  • 812
  • 1
  • 9
  • 22
Shah_z51
  • 23
  • 4
1

For python 3.8 and above, following code will work. Make sure to start with 'source'

source your_environment_name/Scripts/activate
1

For windows Microsoft Tech Support it might be a problem with Execution Policy Settings. To fix it, you should try executing Set-ExecutionPolicy Unrestricted -Scope Process

0

If you’re using Windows, use the command "venv\Scripts\activate" (without the word source) to activate the virtual environment. If you’re using PowerShell, you might need to capitalize Activate.

Thenhan
  • 1
  • 2
0

If you are using windows OS then in Gitbash terminal use the following command $source venv/Scripts/activate. This will help you to enter the virtual environment.

0
  1. Open your project using VS code editor .
  2. Change the default shell in vs code terminal to git bash.

  3. now your project is open with bash console and right path, put "source venv\Scripts\activate" in Windows

shreya
  • 1
0

Navigate to your virtualenv folder eg ..\project1_env> Then type

source scripts/activate

eg ..\project1_env>source scripts/activate

0

If some beginner, like me, has followed multiple Python tutorials now possible has multiple Python versions and/or multiple versions of pip/virtualenv/pipenv...

In that case, answers listed, while many correct, might not help.

The first thing I would try in your place is uninstall and reinstall Python and go from there.

Mate Mrše
  • 6,305
  • 6
  • 26
  • 53
0

The best way is, using backward slahes and using .bat at the end of activate

C:\Users>your_env_name\Scripts\activate.bat
Ahmad Waqar
  • 341
  • 2
  • 6
0
:: location of bat file
::C:\Users\gaojia\Dropbox\Projects\free_return\venv\Scripts\activate.bat 
:: location of the cmd bat file and the ipython notebook
::C:\Users\gaojia\Dropbox\Projects\free_return\scripts\pre_analysis
source ..\..\venv\Scripts\activate
PAUSE
jupyter nbconvert --to html --execute consumer_response_DID.ipynb
PAUSE

Above is my bat file through which I try to execute an ipython notebook. But the cmd window gives me nothing and shut down instantly, any suggestion why would this happen?

Jason Goal
  • 834
  • 1
  • 9
  • 20
0

If you are using windows, just run .\Scripts\activate. Mind that the backslash plays the trick!

Zidane
  • 1
  • 1