Search code examples
pythonanacondacondapython-venvminiconda

How can I install two versions of Python on a single Conda Environment?


I need to have a conda environment with two versions of python (3.5.3 and 3.7.1).

Is it possible to have multiple python versions installed in a single conda environment? And if yes, how can I do so?


Solution

  • Create multiple environments

    Please take time to read the Conda documentation, which covers using different Python versions.

    One cannot install different Python versions in the same environment. As @Grismar commented, it is a central purpose of environments to isolate different Python installations from each other. Instead, create a separate environment for each Python version (or, better, for each project):

    # v3.5.3 environment (name is arbitrary)
    conda create -n python3_5_3 python=3.5.3  # also include additional packages here
    
    # v3.7.1 env
    conda create -n python3_7_1 python=3.7.1
    

    Then activate to use the environment of choice:

    conda activate python3_7_1
    python
    

    Running multiple Python processes

    @Egret commented that OP may need to run code in two different versions in a single context. However, asking to co-install those two versions in one environment would make this an XY problem - rather than asking how to solve a problem, one has instead asked how to implement a (possibly inappropriate) solution to that problem.

    If this is the case, then please see these related answers on how to execute within multiple environments via subprocesses:

    That is, I'd regard the idiomatic solution as using multiple environments and orchestrating execution within them as appropriate. The hard part left out in those answers is exchanging non-trivial data types across processes (e.g., via serialization), but I think that is outside the scope of this thread.


    Testing co-installation with Conda and Mamba

    Simply trying to install both with Conda doesn't give a clear error, in my opinion:

    $ conda create -n snakes python=3.5.3 python=3.7.1
    Collecting package metadata (current_repodata.json): done
    Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
    Collecting package metadata (repodata.json): done
    Solving environment: failed
    
    PackagesNotFoundError: The following packages are not available from current channels:
    
      - python[version='3.5.3.*,3.7.1.*']
    

    However, Mamba seems to give more sensible feedback (and much faster):

    $ mamba create -n snakes python=3.5.3 python=3.7.1
    
                      __    __    __    __
                     /  \  /  \  /  \  /  \
                    /    \/    \/    \/    \
    ███████████████/  /██/  /██/  /██/  /████████████████████████
                  /  / \   / \   / \   / \  \____
                 /  /   \_/   \_/   \_/   \    o \__,
                / _/                       \_____/  `
                |/
            ███╗   ███╗ █████╗ ███╗   ███╗██████╗  █████╗
            ████╗ ████║██╔══██╗████╗ ████║██╔══██╗██╔══██╗
            ██╔████╔██║███████║██╔████╔██║██████╔╝███████║
            ██║╚██╔╝██║██╔══██║██║╚██╔╝██║██╔══██╗██╔══██║
            ██║ ╚═╝ ██║██║  ██║██║ ╚═╝ ██║██████╔╝██║  ██║
            ╚═╝     ╚═╝╚═╝  ╚═╝╚═╝     ╚═╝╚═════╝ ╚═╝  ╚═╝
    
            mamba (0.11.1) supported by @QuantStack
    
            GitHub:  https://github.com/mamba-org/mamba
            Twitter: https://twitter.com/QuantStack
    
    █████████████████████████████████████████████████████████████
    
    
    Looking for: ['python=3.5.3', 'python=3.7.1']
    
    conda-forge/osx-64       Using cache
    conda-forge/noarch       Using cache
    pkgs/main/osx-64         Using cache
    pkgs/main/noarch         Using cache
    pkgs/r/osx-64            Using cache
    pkgs/r/noarch            Using cache
    
    Encountered problems while solving:
      - cannot install both python-3.7.1-h145921a_1000 and python-3.5.3-0