4

I'd like to have both an Anaconda python v2 and python v3 environment. I've run both Anaconda installers, working in Microsoft's powershell. Then to create the python3 env I run:

PS C:\Users\jo> conda create -n py3 python=3.4
Fetching package metadata: ....
Solving package specifications: .
Package plan for installation in environment C:\Anaconda\envs\py3:

The following NEW packages will be INSTALLED:

    pip:        6.1.1-py34_0
    python:     3.4.3-0
    setuptools: 15.2-py34_0

Proceed ([y]/n)? y

Linking packages ...
[      COMPLETE      ]|##################################################| 100%
#
# To activate this environment, use:
# > activate py3
#

However activating this env ignores the new env:

PS C:\Users\jo> activate py3
Activating environment "py3"...
PS C:\Users\jo> python
Python 2.7.9 |Anaconda 2.2.0 (64-bit)| (default, Dec 18 2014, 16:57:52)  [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>>

.. as we see by looking at which is current in the list of installed envs:

PS C:\Users\jo> conda env list
# conda environments:
#
py3                      C:\Anaconda\envs\py3
root                  *  C:\Anaconda

When sanity reigns, the activate command performs this switch by modifying the path variable. Is there something else I need to do in this environment to get it to work?

John Mark
  • 311
  • 1
  • 8
  • Mulling over the question [install python 3.4 on windows](http://stackoverflow.com/questions/24126269/anaconda-install-python-3-4-on-windows?rq=1) to reveal the version of `activate.bat` in one's path, which sets the `python.exe` path by `set "PATH=%VIRTUAL_ENV%\__VENV_BIN_NAME__;%PATH%" ` The problem is that this works in Windows cmd shell, but not for the path in Windows powershell. – John Mark May 14 '15 at 18:58
  • Possible duplicate of [Conda virtual envinment not changing under Windows](https://stackoverflow.com/questions/29863720/conda-virtual-envinment-not-changing-under-windows) – darthbith Sep 25 '17 at 18:47

2 Answers2

5

Switch first to cmd, then activate py3:

> cmd
> activate py3
forzagreen
  • 2,509
  • 30
  • 38
1

If you run a cmd.exe shell script from PowerShell (a batch file), PowerShell spawns an instance of cmd.exe to run the script. If the batch file sets environment variables, they exist only in the spawned cmd.exe instance. Once that instance terminates (i.e., when the script ends), the environment variables do not propagate to the calling process (PowerShell, in this case). This is by design.

If you want to propagate environment variables, you can use the following Invoke-CmdScript function in PowerShell:

function Invoke-CmdScript {
  param(
    [String] $scriptName
  )
  $cmdLine = """$scriptName"" $args & set"
  & $Env:SystemRoot\system32\cmd.exe /c $cmdLine |
  Select-String '^([^=]*)=(.*)$' | ForEach-Object {
    $varName = $_.Matches[0].Groups[1].Value
    $varValue = $_.Matches[0].Groups[2].Value
    Set-Item Env:$varName $varValue
  }
}

Some more information about this in the following article:

Windows IT Pro: Take Charge of Environment Variables in PowerShell

Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62