4

I've installed Python 3.5 using Anaconda and 2.7 using the following command:

conda create -n py27 python=2.7 anaconda

This successfully installed 2.7 as a separate environment with the Anaconda3 folder:

# original installation path for 3.5
C:\Users\username\AppData\Local\Continuum\Anaconda3

# path for 2.7 after conda create -n is run
C:\Users\username\AppData\Local\Continuum\Anaconda3\envs\py27

This works great as long as I'm using an IDE like Spyder or IPython Notebook, where I can choose which Python version. However, I have not been able to use py27 (Python 2.7) in the PowerShell. I've tried using:

activate py27

yet python command still fires up 3.5 and py27 causes this error:

py27 : The term 'py27' is not recognized as the name of a cmdlet, function, script file, or operable program. 
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1 + py27
    + ~~~~ 
    + CategoryInfo   : ObjectNotFound: (py27:String) [], CommandNotFoundException
     + FullyQualifiedErrorId : CommandNotFoundException

I've tried everything I know or have read here on SO, but nothing I've tried has been able to resolve the issue. I'm not sure if this helps, but here is my PATH variable for reference:

C:\Users\username\AppData\Local\Continuum\Anaconda3;C:\Users\username\AppData\Local\Continuum\Anaconda3\Scripts;C:\Users\username\AppData\Local\Continuum\Anaconda3\Library\bin;%SystemRoot%\system32\WindowsPowerShell\v1.0\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;[stuff in between];C:\Users\username\AppData\Local\Continuum\Anaconda3\envs\p27;C:\Users\username\AppData\Local\Continuum\Anaconda3\envs\p27\Scripts\;C:\Users\username\AppData\Local\Continuum\Anaconda3\envs\p27\Library\bin
Ryan Erwin
  • 807
  • 1
  • 11
  • 30
  • 1
    What is `activate`? If it is a batch file, the environment variables it sets will not propagate to PowerShell. To fix this, you can use the `Invoke-CmdScript` function from [this article](http://windowsitpro.com/powershell/take-charge-environment-variables-powershell). – Bill_Stewart Mar 17 '16 at 20:18
  • @Bill_Stewart, `activate` is the command I found here on SO, to "activate" the `2.7` environment within the Anaconda3 installation. It is the most common solution I read on the forum. However, even though the command ran with no problem in the PowerShell, it didn't actually allow me to run `2.7` from the command line. Here is an example post [here](http://stackoverflow.com/questions/24405561/how-to-install-2-anacondas-python-2-7-and-3-4-on-mac-os-10-9) – Ryan Erwin Mar 19 '16 at 16:47
  • Again: If `activate` is a batch file (cmd.exe shell script) and it sets environment variables, you will need to run it using the `Invoke-CmdScript` function in the article. – Bill_Stewart Mar 19 '16 at 21:58
  • To recapitulate differently: PowerShell can run a batch file, but if the batch file sets environment variable values that are supposed to persist after the batch file terminates, said batch file won't work properly in PowerShell because the cmd.exe process that ran the batch file terminated. The workaround is to "capture" the environment variable changes that the batch file makes, then propagate the changes to PowerShell. That is the purpose of the `Invoke-CmdScript` function in the aforementioned article. – Bill_Stewart Mar 21 '16 at 19:26
  • @Bill_Stewart , thanks for the script. I'm sorry I'm just getting back to this. I've read the article and downloaded the script. However, I have no clue where to put it: where do I put the text file and are there any further commands to run once I've got it in the right place? – Ryan Erwin Mar 31 '16 at 16:10
  • I would recommend reading some introductory training material about PowerShell. I published some articles (the links to which are posted in [this thread](https://social.technet.microsoft.com/Forums/scriptcenter/en-US/1ba3ecfb-2dac-4960-ba75-0b5f5f0dc090/)) that may be of help. – Bill_Stewart Mar 31 '16 at 17:34
  • @Bill_Stewart, thanks. I've got it installed, sourced it and made the functions available. `activate` is a `.bat` file that is installed with th Anaconda python distribution. The purpose of the file is to switch between the 2 and 3 environments. I just have not been able to get the `Invoke-CmdScript` to function. I pass `Invoke-CmdScript .\activate.bat` but nothing appears to take place. – Ryan Erwin Mar 31 '16 at 18:27
  • You would have to add the `Invoke-CmdScript` function into your current scope. You can do this by dot-sourcing a script file (see the section `Using Dot Source Notation with Scope` - run `help about_Scopes` in PowerShell) that contains the function, or you can add the function your PowerShell profile (run the command `help about_Profiles` in PowerShell for more information). – Bill_Stewart Mar 31 '16 at 19:31
  • 1
    @Bill_Stewart, I added the Environment.ps1 directory to my PATH variable, dot-sourced the Environment.ps1 file, which gave me access to the `Invoke-CmdScript` function. At this point, I ran the command `Invoke-CmdScript .\activate.bat`. I get `'.\activate.bat' is not recognized as an internal or external command, operable program or batch file.` in return. – Ryan Erwin Mar 31 '16 at 22:35
  • I can't see your screen, but `.\activate.bat` means "the copy of `activate.bat` this is sitting in the current directory." If `activate.bat` is not in the current directory, you need to tell PowerShell where it is. – Bill_Stewart Mar 31 '16 at 22:36
  • Short story: I've fixed it, and I really appreciate your help. I'll update my answer below. – Ryan Erwin Mar 31 '16 at 22:47

4 Answers4

2

I've decided to close this question with the following solution: use Cmd.exe or use the PowerShell with Bill Stewart's Invoke-CmdScript (see comments). First, the simpler solution using the Command Prompt (Cmd.exe):

# to activate
C:\Users\me\> activate py2

# to deactivate and return to Python 3.5
C:\Users\me\> deactivate

Which activates the Python 2.7 environment I created using conda. Now, for the Powershell version. First, go this site and download the Environment.ps1 script. For me, it downloaded as text file, so I opened the file, removed the .txt extension and replaced it with .ps1. So, let's say I saved Environment.ps1 in the following directory C:\Users\user_name\AppData\Local\invoke_cmdscript. Take this path and add it to the PATH user variable within the Environment variables form. Now, the commands to type in your PowerShell window:

# dot-source my file
. Environment.ps1

# activate 2.7 environment (if not in the directory of activate.bat, specify full path)
Invoke-CmdScript .\activate.bat py2


# deactivate 2.7 environment (same note about current directory and full paths)
Invoke-CmdScript .\deactivate.bat    
Ryan Erwin
  • 807
  • 1
  • 11
  • 30
1

If you want python 3 to run every time you type python3, and python 2 to run every time you type python2:

Depending on where your executables are, simply make some aliases using the following codes:

for python2:

Set-Alias python2 C:\Users\yourusername\python2location\python.exe

for python3:

Set-Alias python3 C:\Users\yourusername\python3location\python.exe

to learn how to make them permanent, check my response at the following page

Community
  • 1
  • 1
0

From your PATH variable it appears you created the py27 env with the name "p27" Try 'activate p27' You can always double check the env names by looking in the "envs" directory in your conda installation folder.

superbeck
  • 501
  • 1
  • 4
  • 9
  • I've used the 'activate p27' to no avail. To be honest, I'm not sure what steps I'm supposed to take after activating the environment. For example, to use the python version in p27 after activating, do I use python, python2, p27, or py -2 to run python 2.7? I've tried all of them, and nothing seems to do the trick. – Ryan Erwin Mar 19 '16 at 16:50
0

A workaround if you love powershell compared to cmd without dealing with changing 'stuff' is to type:

cmd #to get internally into command prompt from ps
activate envname
powershell #to get back
oblong
  • 625
  • 2
  • 6
  • 8