Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Setting Up a Local Python Environment Installing Python and Python Libraries Locally Using virtualenv

'virtualenv venv' command returning error "The term 'virtualenv' is not recognized... virtualenv IS installed.

Title. I've already activated it because that's what I thought the problem was.

Windows 10. If it makes a difference I'm using PowerShell.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Depending on where Python is installed, say, "C::\Python3.5", then the "C::\Python3.5\Scripts" directory needs to be added to your PATH.

Or, you could try: "C::\Python3.5\Scripts\virtualenv venv" to run it from the scripts directory.

I think I figured it out... I ran PowerShell as an administrator. reinstalled virtualenv and now I think it works.

I have another problem though... inside the venv folder (the one I created when I typed in virtualenv venv) there is no activate.bat file. There IS one in the venv folder that appeared when I installed virtualenv. If I type "C:...\venv\Scripts\nt\activate.bat" It doesn't throw up any errors but I also don't see the (venv) in front of my prompt. Is this because I'm on windows? How will I know that I've activated it correctly?

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Let's try from the top (your prompts may differ). The comments after the "#" do not need to be typed. Where does this stop working for you?

cd ~   # start in your home directory
mkdir testdir  # create a test directory called 'testdir'
cd testdir  # change to testdir
ls  # list empty directory
virtualenv venv  # make virtualenv in this directory
ls  # list current directory to see new venv directory
ls .\venv\Scripts  # list script directory in venv
.\venv\Scripts\activate  # activate this venv

Here are the above commands run in a Powershell window:

PS C:\> cd ~   # start in your home directory
PS C:\Users\User> mkdir testdir  # create a test directory called 'testdir'

    Directory: C:\Users\User

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         11/6/2015   3:45 PM            testdir

PS C:\Users\User> cd testdir  # change to testdir
PS C:\Users\User\testdir> ls  # list empty directory
PS C:\Users\User\testdir> virtualenv venv  # make virtualenv in this directory
New python executable in venv\Scripts\python.exe
Installing setuptools, pip...done.
PS C:\Users\User\testdir> ls  # list current directory to see new venv directory

    Directory: C:\Users\User\testdir

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         11/6/2015   3:46 PM            venv

PS C:\Users\User\testdir> ls .\venv\Scripts  # list script directory in this venv

    Directory: C:\Users\User\testdir\venv\Scripts

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         11/6/2015   3:46 PM       2309 activate
-a---         11/6/2015   3:46 PM        557 activate.bat
-a---         11/6/2015   3:46 PM       8252 activate.ps1
-a---         11/6/2015   3:46 PM       1129 activate_this.py
-a---         11/6/2015   3:46 PM        348 deactivate.bat
-a---         11/6/2015   3:46 PM      91519 easy_install-2.7.exe
-a---         11/6/2015   3:46 PM      91519 easy_install.exe
-a---         11/6/2015   3:46 PM      91491 pip.exe
-a---         11/6/2015   3:46 PM      91491 pip2.7.exe
-a---         11/6/2015   3:46 PM      91491 pip2.exe
-a---         11/6/2015   3:46 PM      26624 python.exe
-a---         11/6/2015   3:46 PM      27136 pythonw.exe

PS C:\Users\User\testdir> .\venv\Scripts\activate  # activate this venv
(venv) PS C:\Users\User\testdir>

virtualenv venv almost finishes. I get Installing setuptools, pip, wheel...done. then I get a Traceback ending in OSError: ...setuptools pip wheel failed with error code 1

from there i can continue to the last step (activate) and there I get the error "Cannot find path C:...\testdir\venv\Scripts\activate

Here is a copy-paste of the first error.

...Installing setuptools, pip, wheel...done.
Traceback (most recent call last):
  File "c:\program files (x86)\python 3.5\lib\runpy.py", line 170, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\program files (x86)\python 3.5\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Program Files (x86)\Python 3.5\Scripts\virtualenv.exe\__main__.py", line 9, in <module>
  File "c:\program files (x86)\python 3.5\lib\site-packages\virtualenv.py", line 832, in main
    symlink=options.symlink)
  File "c:\program files (x86)\python 3.5\lib\site-packages\virtualenv.py", line 1004, in create_environment
    install_wheel(to_install, py_executable, search_dirs)
  File "c:\program files (x86)\python 3.5\lib\site-packages\virtualenv.py", line 969, in install_wheel
    'PIP_NO_INDEX': '1'
  File "c:\program files (x86)\python 3.5\lib\site-packages\virtualenv.py", line 910, in call_subprocess
    % (cmd_desc, proc.returncode))
OSError: Command C:\Users\Fooly_000\t...v\Scripts\python.exe -c "import sys, pip; sys...d\"] + sys.argv[1:]))" setuptools pip wheel failed with error code 1```
Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Victor, can you run the following commands to get your pip, virtualenv, setuptools versions?

pip --version pip show virtualenv pip show setuptools

Also, do you recall how you installed Python 3.5? There maybe a side-effect from the directory name having a space in it: 'Python 3.5".

pip version is 7.1.2 virtualenv version is 13.1.2 setuptools version is 18.2

I had python 2.7 installed already. For Python 3.5 I just went to the python website (the one Kenneth Love goes to in his video) and downloaded it from there.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

My advice would be to reinstall Python 3.5 to remove spaces in the path.

I went to the webpage https://www.python.org/downloads/release/python-350/ and downloaded the "Windows x86-64 executable installer". This downloads the file python-3.5.0-amd64.exe.

If you run the exe file, it puts python in the "C:\Users<your-user-name>\AppData\Local\Programs\Python\Python35\" by default. I don't prefer that.

Instead, I install this way:

  • On Front install page, I select Add Python 3.5 to PATH checkbox
  • Select Customize Installation
  • For Optional Features, all boxes checked, select next
  • Select install for all users checkbox (others leave as default)
  • Change installation directory to C:\Python35 (my preference as it's easy to remember and get to)
  • Select Install

So your last suggestion seems to do the trick except when I get to the activate command. It doesn't do anything. I don't see any (venv) prompt. Tried to deactivate in case it was active but that didn't do anything. So I don't know if it's working, but I don't think so.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

In the newly created venv directory, there should be a PowerShell script .\venv\Scripts\activate.ps1.

If present, let's run it with debugging turned on, and post the results.

PS C:\Users\User\testdir>
PS C:\Users\User\testdir> Set-PSDebug -Trace 1
PS C:\Users\User\testdir> .\venv\Scripts\activate
DEBUG:    1+  >>>> .\venv\Scripts\activate
DEBUG:    5+  >>>> $script:THIS_PATH = $myinvocation.mycommand.path
DEBUG:    6+  >>>> $script:BASE_DIR = split-path (resolve-path "$THIS_PATH/..") -Parent
DEBUG:    7+  >>>> $script:DIR_NAME = split-path $BASE_DIR -Leaf
DEBUG:   33+  >>>> deactivate -nondestructive
DEBUG:    9+ function global:deactivate ( [switch] $NonDestructive ) >>>> {
DEBUG:   11+     if (  >>>> test-path variable:_OLD_VIRTUAL_PATH ) {
DEBUG:   11+     if (  >>>> test-path variable:_OLD_VIRTUAL_PATH ) {
DEBUG:   16+     if (  >>>> test-path function:_old_virtual_prompt ) {
DEBUG:   16+     if (  >>>> test-path function:_old_virtual_prompt ) {
DEBUG:   21+     if ( >>>> $env:VIRTUAL_ENV) {
DEBUG:   26+     if (  >>>> !$NonDestructive ) {
DEBUG:   30+  >>>> }
DEBUG:   35+  >>>> $VIRTUAL_ENV = $BASE_DIR
DEBUG:   36+  >>>> $env:VIRTUAL_ENV = $VIRTUAL_ENV
DEBUG:   38+  >>>> $global:_OLD_VIRTUAL_PATH = $env:PATH
DEBUG:   39+  >>>> $env:PATH = "$env:VIRTUAL_ENV/Scripts;" + $env:PATH
DEBUG:   40+ if ( >>>> ! $env:VIRTUAL_ENV_DISABLE_PROMPT) {
DEBUG:   42+      >>>> $function:_old_virtual_prompt = $function:prompt
(venv) PS C:\Users\User\testdir>

if you do not have a activate.ps1 file, please list the Scripts directory and post the output.

DEBUG:    1+  >>>> .\venv\Scripts\activate.ps1
DEBUG:   19+                                         if ( &  >>>> { Set-StrictMode -Version 1; $_.PSMessageDetails } )
{
DEBUG:   19+                                         if ( & {  >>>> Set-StrictMode -Version 1; $_.PSMessageDetails } )
{
DEBUG:   19+                                         if ( & { Set-StrictMode -Version 1;  >>>> $_.PSMessageDetails } )
{
DEBUG:    1+ &  >>>> { Set-StrictMode -Version 1; $this.Exception.InnerException.PSMessageDetails }
DEBUG:    1+ & {  >>>> Set-StrictMode -Version 1; $this.Exception.InnerException.PSMessageDetails }
DEBUG:    1+ & { Set-StrictMode -Version 1;  >>>> $this.Exception.InnerException.PSMessageDetails }
DEBUG:    1+ & { Set-StrictMode -Version 1; $this.Exception.InnerException.PSMessageDetails  >>>> }
DEBUG:   19+                                         if ( & { Set-StrictMode -Version 1; $_.PSMessageDetails  >>>> } )
{
DEBUG:   26+                                         $errorCategoryMsg = &  >>>> { Set-StrictMode -Version 1;
$_.ErrorCategory_Message }
DEBUG:   26+                                         $errorCategoryMsg = & {  >>>> Set-StrictMode -Version 1;
$_.ErrorCategory_Message }
DEBUG:   26+                                         $errorCategoryMsg = & { Set-StrictMode -Version 1;  >>>>
$_.ErrorCategory_Message }
DEBUG:   26+                                         $errorCategoryMsg = & { Set-StrictMode -Version 1;
$_.ErrorCategory_Message  >>>> }
DEBUG:   42+                                         $originInfo = &  >>>> { Set-StrictMode -Version 1; $_.OriginInfo }
DEBUG:   42+                                         $originInfo = & {  >>>> Set-StrictMode -Version 1; $_.OriginInfo }
DEBUG:   42+                                         $originInfo = & { Set-StrictMode -Version 1;  >>>> $_.OriginInfo }
DEBUG:   42+                                         $originInfo = & { Set-StrictMode -Version 1; $_.OriginInfo  >>>> }
.\venv\Scripts\activate.ps1 : File
C:\users\Fooly_000\OneDrive\Documents\Programming\TreeHouse\venv\Scripts\activate.ps1 cannot be loaded. The contents
of file C:\users\Fooly_000\OneDrive\Documents\Programming\TreeHouse\venv\Scripts\activate.ps1 might have been changed
by an unauthorized user or process, because the hash of the file does not match the hash stored in the digital
signature. The script cannot run on the specified system. For more information, run Get-Help about_Signing..
At line:1 char:1
+ .\venv\Scripts\activate.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

The error has becoming clear. The powershell is instituting a security policy to prevent you from running unknown scripts. The various PowerShell Execution Policies establish what is allowed to run.

In your powershell window, you can see the current policy level using

(venv) PS C:\Users\User\testdir> get-executionpolicy
Unrestricted
(venv) PS C:\Users\User\testdir>

Mine is set to "Unrestricted". I suspect your is set to a more limited policy. After testing, the "RemoteSigned" policy seems to work. Try that before jumping to the more lax "Unrestricted" policy:

  • Start a powershell using Run as Administrator
  • at shell prompt, type set-executionpolicy remotesigned
  • when asked Do you want to change the execution policy?, type Y, or just hit Enter

OH MY GOD! THANK YOU SO MUCH!!!! Man, it took forever to figure that out! Thank you thank you thank you!!!! You are seriously THE BEST!

Thank you!

P.S. THANK. YOU.

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

If you are in a virtual env, the environment variable VIRTUAL_ENV is set. In Powershell, you can check your environment variables by:

PS C\: > cd env:
PS Env: > ls virtual_env
ls : Cannot find path 'virtual_env' because it does not exist....

# Back to C:
PS Env: > cd c:

# Activate the venv in the current directory
PS C: > .\venv\Scripts\activate
(venv) PS C: > 

# Check for virtual_env
(venv) PS C: > cd  env:
(venv) PS Env: > ls virtual_env

Name
----
VIRTUAL_ENV                   c:\Users\User\devel\venvtest\venv

# Back to C:
(venv) PS Env: > cd c:
(venv) PS C: > 

You can also use Env: to examine your path variable which will change when the venv is active.

So I'm getting an error on the 2nd comment. When I try venv\Scripts\activate I get "The term '.\venv\Scripts\activate" is not recognized...

in my venv\Scripts folder is only python.exe, python35.dll and pythonw.exe

the only activate file is in C:..\Lib\venv\scripts\nt

there i have activate.bat activate.ps1 and deactivate.bat

If I run .\activate it says it cannot run because it is not signed.

I looked up the user guide for virtualenv and saw something that looked like a workaround:

C:> Set-ExecutionPolicy AllSigned

But that didn't work either...