Danny
Danny

Reputation: 35

How to execute script in Anaconda with different installed Python versions?

I want to run a script in Anaconda using Python 2.7. I am using Windows 8 with Anaconda 3 and Python 3.6.5. I created another environment with python 2.7.15 and activated it in Anaconda Prompt like advised here: https://conda.io/docs/user-guide/tasks/manage-python.html

How can I run this: print "HellWorld!"

I remember there was a way to run the script from the spyder console just adding the version to the command line but I cannot remember the syntax.

What I did so far:

I activated py27 by typing into Anaconda Prompt:

conda activate py27

I checked if it was correctly activated (yes, it was) by:

python --version

Upvotes: 2

Views: 1055

Answers (1)

jalazbe
jalazbe

Reputation: 2015

Using conda and environments is easy, once you get to know how to manage environments.

When creating an environment you may choose the python version to use and also what other libraries.

Let's begin creating two different environments.

jalazbe@DESKTOP:~$ conda create --name my-py27-env python=2.7
jalazbe@DESKTOP:~$ conda create --name my-py36-env python=3.6

It might prompt to you a message like: The following NEW packages will be INSTALLED:

    ca-certificates: 2018.03.07-0
    certifi:         2018.8.13-py27_0
    libedit:         3.1.20170329-h6b74fdf_2
    libffi:          3.2.1-hd88cf55_4
Proceed ([y]/n)?

Just type Y and press Enter

So now you have two environments. One of them with python 2.7 and the other one with python 3.6

Before executing any script you need to select which environment to use. In this example I'll you the environment with python 2.7

jalazbe@DESKTOP:~$ conda activate my-py27-env

Once you activate an environment you will see it at the left side of the prompt and between parenthesis like this (environment-name)

(my-py27-env) jalazbe@DESKTOP:~$

So now everything to execute will use the libraries in the environment. if you execute python -V

(my-py27-env) jalazbe@DESKTOP:~$ python -V

The output will be:

Python 2.7.15 :: Anaconda, Inc.

Then you may change to another environment by first: exit the one you are on (called deactivate) and entering (activating) the other environment like this:

(my-py27-env) jalazbe@DESKTOP:~$ conda deactivate
jalazbe@DESKTOP:~$ 
jalazbe@DESKTOP:~$  conda activate my-py36-env
(my-py36-env) jalazbe@DESKTOP:~$ 

At this point if you execute python -V you get

(my-py36-env) jalazbe@DESKTOP:~$ python -V
Python 3.6.5 :: Anaconda, Inc.

To answer your question you need two environments with different libraries and python versions. When executing an script you have to choose which environment to use.

For further usage of conda commands see conda cheet sheet or read documentation about conda

Upvotes: 1

Related Questions