Activating Python virtual environment on Windows
This is my CheatSheet when I install python on windows via PowerShell.
First install python 2.7x from https://www.python.org/downloads/
Then add the Python and Scripts folder to the path variable (system wide)
# Add Python and Python Scripts to path
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine)
$PythonPath = "C:\Python27"
$PythonScriptsPath = "C:\Python27\Scripts"
if ($env:Path -notlike "*$PythonPath*") {
$env:Path = $env:Path + ";$PythonPath"
}
if ($env:Path -notlike "*$PythonScriptsPath*") {
$env:Path = $env:Path + ";$PythonScriptsPath"
}
# Save to machine path
[Environment]::SetEnvironmentVariable( "Path", $env:Path, [System.EnvironmentVariableTarget]::Machine )
# Check machine path
[System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine)
Then install virtualenv via pip
pip install virtualenv
Activate an virtualenv
virtualenv venv
. .\venv\Scripts\activate
If using Powershell, the
activate
script is subject to the execution policies on the system. By default on Windows 7, the system’s excution policy is set toRestricted
. In order to use the script, you can relax your system’s execution policy toAllSigned
, meaning all scripts on the system must be digitally signed to be executed. As an administrator run:Set-ExecutionPolicy AllSigned
Deactivate a virtualenv
deactivate
Comments
-
Barka over 3 years
Note: People have marked this as a duplicate of another question but it is not. There is something off about my virtualenv and I have not been able to resolve it. It might have to do with how Visual Studio sets it up.
I have been following along with this excellent tutorial on flask
I ran into a problem when I tried to activate the virtual environment on Windows. How do you execute
$ venv\Scripts\activate
? Is this supposed to be from the command prompt or Powershell? I have used Visual Studio as my IDE. It creates for you a VS solution that has a basic flask app to start with. In the process of creating the app it asks you to create a virtual environment. It creates that virtual environment in a directory similar to the one shown in the tutorial.\venv\Scripts
exits but it does not have a file or executable called "activate".here is the content of the Scripts folder:
api-ms-win-core-console-l1-1-0.dll api-ms-win-core-datetime-l1-1-0.dll
api-ms-win-core-debug-l1-1-0.dll
api-ms-win-core-errorhandling-l1-1-0.dll
api-ms-win-core-file-l1-1-0.dll api-ms-win-core-file-l1-2-0.dll
api-ms-win-core-file-l2-1-0.dll api-ms-win-core-handle-l1-1-0.dll
api-ms-win-core-heap-l1-1-0.dll api-ms-win-core-interlocked-l1-1-0.dll
api-ms-win-core-libraryloader-l1-1-0.dll
api-ms-win-core-localization-l1-2-0.dll
api-ms-win-core-memory-l1-1-0.dll api-ms-win-core-namedpipe-l1-1-0.dll
api-ms-win-core-processenvironment-l1-1-0.dll
api-ms-win-core-processthreads-l1-1-0.dll
api-ms-win-core-processthreads-l1-1-1.dll
api-ms-win-core-profile-l1-1-0.dll
api-ms-win-core-rtlsupport-l1-1-0.dll
api-ms-win-core-string-l1-1-0.dll api-ms-win-core-synch-l1-1-0.dll
api-ms-win-core-synch-l1-2-0.dll api-ms-win-core-sysinfo-l1-1-0.dll
api-ms-win-core-timezone-l1-1-0.dll api-ms-win-core-util-l1-1-0.dll
api-ms-win-crt-conio-l1-1-0.dll api-ms-win-crt-convert-l1-1-0.dll
api-ms-win-crt-environment-l1-1-0.dll
api-ms-win-crt-filesystem-l1-1-0.dll api-ms-win-crt-heap-l1-1-0.dll
api-ms-win-crt-locale-l1-1-0.dll api-ms-win-crt-math-l1-1-0.dll
api-ms-win-crt-multibyte-l1-1-0.dll api-ms-win-crt-private-l1-1-0.dll
api-ms-win-crt-process-l1-1-0.dll api-ms-win-crt-runtime-l1-1-0.dll
api-ms-win-crt-stdio-l1-1-0.dll api-ms-win-crt-string-l1-1-0.dll
api-ms-win-crt-time-l1-1-0.dll api-ms-win-crt-utility-l1-1-0.dll
concrt140.dll msvcp140.dll pyexpat.pyd python.exe python3.dll
python36.dll pythoncom36.dll pythonw.exe pywintypes36.dll select.pyd
sqlite3.dll tcl86t.dll tk86t.dll ucrtbase.dll unicodedata.pyd
vccorlib140.dll vcomp140.dll vcruntime140.dll winsound.pyd
xlwings32.dll xlwings64.dll
_asyncio.pyd
_bz2.pyd
_ctypes.pyd
_ctypes_test.pyd
_decimal.pyd
_elementtree.pyd
_hashlib.pyd
_lzma.pyd
_msi.pyd
_multiprocessing.pyd
_overlapped.pyd
_socket.pyd
_sqlite3.pyd
_ssl.pyd
_testbuffer.pyd
_testcapi.pyd
_testconsole.pyd
_testimportmultiple.pyd
_testmultiphase.pyd
_tkinter.pyd
I got all the way to data migration section but here I need to run the
(venv) $ flask db migrate
I am at a loss on how to get into the virtual environment to run this.
-
Barka about 6 yearsThanks. You are in an environment where you get the unix shell(ish) $. Powershell gives me a dos(ish) PS C:\Users\Me> instead. How do you get to that $?
-
Barka about 6 yearsThanks. Tried that both on the command prompt and on Powershell and get: 'activate' is not recognized as an internal or external command, operable program or batch file. There is no file or executable called activate in the Script directory
-
om tripathi about 6 yearsThat means virtual environment is not installed properly.
-
Glenn G about 6 yearsI'm not sure what you mean? The dollar sign is a variable in PowerShell. See: docs.microsoft.com/en-us/powershell/module/… I've added a screenshot of my environment: prnt.sc/ida85x
-
Barka about 6 yearsThanks for the details. It appears that my problem is due to a bug in Visual Studio when creating an Anaconda environment. Since your cheat sheet is so awesome, I am marking this as the answer. Cheers!
-
FanaticD almost 6 yearsWhat if I needed to run these commands from powershell script? I was trying to figure how to create & activate virtualenv from powershell and then run python script inside the virtualenv, but I can not figure out how to run python script AFTER the virtualenv is activated.
-
WebComer over 5 yearsit doesn't work