3

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.

Community
  • 1
  • 1
Barka
  • 8,764
  • 15
  • 64
  • 91
  • 1
    The documentation also explains how to activate virtualenv : https://virtualenv.pypa.io/en/stable/userguide/ – l'L'l Feb 11 '18 at 10:44
  • It seems that this due to a bug in Python Tools For Visual Studio. It only happens when you try to create an Anaconda virtual environment – Barka Feb 17 '18 at 03:41

2 Answers2

7

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 to Restricted. In order to use the script, you can relax your system’s execution policy to AllSigned, meaning all scripts on the system must be digitally signed to be executed. As an administrator run: Set-ExecutionPolicy AllSigned

Deactivate a virtualenv

deactivate
Glenn G
  • 146
  • 3
  • Thanks. 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 Feb 11 '18 at 15:49
  • I'm not sure what you mean? The dollar sign is a variable in PowerShell. See: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_variables?view=powershell-6 I've added a screenshot of my environment: https://prnt.sc/ida85x – Glenn G Feb 11 '18 at 18:55
  • Thanks 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! – Barka Feb 17 '18 at 03:43
  • What 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. – FanaticD Jul 03 '18 at 04:13
-2

path_to_env/Scripts/activate

This works for me.

om tripathi
  • 300
  • 1
  • 5
  • 20
  • Thanks. 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 – Barka Feb 11 '18 at 15:51
  • That means virtual environment is not installed properly. – om tripathi Feb 11 '18 at 16:10
  • it doesn't work – WebComer Oct 29 '18 at 12:54