Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

having default Mac Python 2.7 and Anaconda Python 3

I want to keep the Mac Python as my main 'python'. The reason for that is the recommendation in Python website here. I also want to add a separate environment for my Python3 (Anaconda).

For doing that I installed the Anaconda Python to get access to Conda and then I made an environment for my Python3 using the following command:

conda create -n py36 python=3.6 anaconda

When I installed the Anaconda python it added this to my .bash_profile file to get access to all conda commands:

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

Now my default python is anaconda python which I don't want to.

How can I have default Mac python as my main python and then when I needed Anaconda, just use source activate py36 ?

like image 486
Omid Avatar asked Jun 19 '26 15:06

Omid


1 Answers

UPDATED ANSWER

After testing this, I feel it's appropriate to offer this as a simple solution for using Mac Python as the default and only using Conda Python when desired.

You need to add/move the conda path to the end of your PATH environment via export command. This should allow you to use the 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 you have /usr/bin/ (where Mac Python is) already in your PATH. Resolution order should check that directory first assuming it's first in the PATH. Also, this setup does not require symlinks in /usr/local/bin. I am not a fan of manipulating system-level resources for solutions that can be done with user resources (directories).

Default Python Setup

After moving the Anaconda path to the end of your PATH environment variable, you can validate that which python references /usr/bin/python, the location for Mac Python. You will run Mac python by default at the command line.

Running Conda Python

As previously noted, you have to call source activate py36 when you want to use the conda virtual environment. There is no need for adding symlinks to /usr/local/bin as they are already available through ~/anaconda/bin/.

Furthermore, source activate py36 (or any other Anaconda environment), it will add the appropriate environment path for Anaconda python to the beginning of your PATH environment variable, which (referring back to Path Resolution) would be executed when run as python on the command line. You can validate this with which python after running source activate py36. conda also stores the previous path as the environment variable CONDA_PATH_BACKUP.

Deactivating Conda

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

like image 139
tdube Avatar answered Jun 21 '26 05:06

tdube



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!