Python – Has default Mac Python 2.7 and Anaconda Python 3

Has default Mac Python 2.7 and Anaconda Python 3… here is a solution to the problem.

Has default Mac Python 2.7 and Anaconda Python 3

I want to have Mac Python as my primary “python”. The reason is the recommendation on the Python website here.

To do this, I installed Anaconda Python to access Conda and then created an environment for my Python3 using the following command:

conda create -n py36 python=3.6 anaconda

When I install Anaconda python, it adds this to my .bash_profile file to access all conda commands:

# added by Anaconda3 4.4.0 installer
# export PATH="/Users/omidb/anaconda/bin:$PATH"

Now my default python is the anaconda python that I don’t want to use.

How can I have the default Mac python as my primary python and then just use source activate py36 when I need Anaconda?

Solution

Updated answer

After testing, I felt it would be appropriate to offer it as a simple solution that uses Mac Python by default and only uses Conda Python when needed.

You need to add/move the conda path to the end of your PATH environment via the export command. This should allow you to use Mac Python as the default and only use Anaconda Python after calling source activate py36.

export PATH="$PATH:/Users/omidb/anaconda/bin"

Path resolution

This solution assumes that you already have /usr/bin/ (where Mac Python is located) in your PATH. The parsing order should check the directory first, assuming it is in the first directory in the PATH. Also, this setting does not require symbolic links in /usr/local/bin. I don’t like manipulating system-level resources for solutions that can be done using user resources (directories).

Default Python settings

After moving the Anaconda path to the end of the PATH environment variable, you can verify which python references /usr/bin/python, the location of Mac Python. By default, you will run Mac python at the command line.

Run Conda Python

As mentioned earlier, when you want to use a conda virtual environment, you must call source activate py36. There is no need to add symbolic links to /usr/local/bin, as they are already available via ~/anaconda/bin/.

In addition, source activates py36 (or any other Anaconda environment), which adds the environment path appropriate for Anaconda python to the beginning of the PATH< environment variable, which will be executed when running as python on the command line (pointing back to path resolution). You can verify this using which python after running source activate py36. conda also stores the previous path as an environment variable CONDA_PATH_BACKUP.

Deactivate Conda

After running source deactivate, the original path is restored, so you will go back to running Mac python.

Related Problems and Solutions