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
11
  • 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. Commented Mar 17, 2016 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 Commented Mar 19, 2016 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. Commented Mar 19, 2016 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. Commented Mar 21, 2016 at 19:26
  • 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. Commented Mar 31, 2016 at 22:35

4 Answers 4

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    
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

1 Comment

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.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.