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 ?

Community
  • 1
  • 1
Omid
  • 1,959
  • 25
  • 42
  • Remove `/Users/omidb/anaconda/bin` from `PATH` and create symbolic links of `/Users/omidb/anaconda/bin/activate` and `/Users/omidb/anaconda/bin/deactivate` in `/usr/local/bin`. From here on, your default python will be the one Mac came with. To use the `anaconda python`, run `source activate py36`. – alvits Aug 15 '17 at 00:01
  • Totally agree with @alvits that you do not want your `conda` environment added to your `PATH` environment variable. AFAIK, you do not need to add the symlinks, though. – tdube Aug 15 '17 at 01:08
  • @tdube - I am in the same predicament as the OP quite a while back. If I didn't create the symlinks, I would have to type the full pathname to `activate`. – alvits Aug 15 '17 at 01:11
  • @alvits This may work but it's not that nice because I have to add other things like conda and .... – Omid Aug 15 '17 at 01:12
  • It is nice and I have this setup running without issues. I even have 4 environments of different python versions. The only symlinks I made are `activate`, `deactivate` and `conda`. – alvits Aug 15 '17 at 01:13
  • And if you prefer to keep it in the PATH, just move it to the end of the PATH. But `deactivate` could potentially remove this path whenever you source it. – alvits Aug 15 '17 at 01:14
  • @alvits I never made symlinks and I have multiple environments all working correctly. – tdube Aug 15 '17 at 01:15
  • @tdtube - you are either implying that `activate` is in the `PATH`, which means `anaconda python` is also in the `PATH`. This will only work if they are the last in the PATH or you won't be able to run the standard python that came with Mac. Or you are calling it using full pathname like `source anaconda3/bin/activate envname`. Otherwise, how will the shell find where `activate` is? Care to explain? – alvits Aug 15 '17 at 01:18
  • @tdube - Or you are probably running the default python by specifying the full pathname such as `/usr/bin/python`. Because if you don't, the other python will run. Care to check which is your default python by running `which python`? You'll be surprised of the result. – alvits Aug 15 '17 at 01:20
  • @alvits Okay, so I checked my `PATH` again in a new shell. I have `/home/{user}/anaconda3/bin` at the beginning of my `PATH` which does set anaconda `python` as my default (link in that dir) and provide path to `activate` and `deactivate`. My initial point about the links was that I did not have to add symlinks in `/usr/local/bin/`. – tdube Aug 15 '17 at 01:22
  • @tdtube - you didn't need it becuase you have it in the PATH as I have been explaining. But this makes your default the miniconda python, not the Mac python which the OP has been asking. – alvits Aug 15 '17 at 01:23

2 Answers2

3

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.

tdube
  • 2,453
  • 2
  • 16
  • 25
  • If you don't create the symlinks, then you have to specify the full path to `activate`. You need at least `activate` to be in the path, hence the symlinks suggestion. – alvits Aug 15 '17 at 01:09
  • @tdube In this way, the default Python will be Anaconda not the Mac python because it will be modify the PATH – Omid Aug 15 '17 at 01:10
  • @Omid - I am also guessing that tdtube is running windows. Therefore, default python is not a concern because windows doesn't have any python. – alvits Aug 15 '17 at 01:22
  • @alvits No, I'm on a Mac. For my setup, I just never wanted to use default Mac Python. It's a bit... dated! :) – tdube Aug 15 '17 at 01:23
  • Yes you are on a Mac. **_But your default python is no longer the Mac python_**. The OP wants to **keep** the default python. – alvits Aug 15 '17 at 01:25
  • You just admitted that you are not using the Mac python as default. That makes your answer unsuitable to the OP's question. – alvits Aug 15 '17 at 01:26
  • Does the content of my answer above preclude the ordering for the `PATH`? – tdube Aug 15 '17 at 01:27
  • Your answer is irrelevant to the question. The OP will run `source activate py36` no matter what the solution is. In fact, it's exactly what the OP wanted to run to use the `conda python`. The issue revolves around being unable to keep the default python. – alvits Aug 15 '17 at 01:29
  • @alvits Sorry, you nailed it and apparently I was skimming too much. Updated my answer. – tdube Aug 15 '17 at 01:31
  • @omid If you update the `.bash_profile` to read `export PATH="$PATH:/Users/omidb/anaconda/bin"` instead, the path for `python` will resolve to `/usr/bin/python`. Then you can use Anaconda `python` as you are wanting after `source activate py36`. To go back to `/usr/bin/python`, you can either: (1) start a new shell; (2) run `source deactivate`. – tdube Aug 15 '17 at 19:43
0

Faced the same problem and question is too old but the simplest of doing this which I found is:


1. Let's check if the default python version is pointing to Conda python
which python - If Conda installation updated to your bashrc or zshrc, it will show that path
  1. Running Command:

    conda config --set auto_activate_base false

It's will make sure that when you start the terminal, Conda doesn't get activated as base

Now if you check python --version or which python - It should be pointing to mac default python version

  1. Now whenever you want to use conda, all conda commands are available with conda <command>

  2. Create the virtual env using conda create --name venv and activate it using conda activate <venv_name>

Now, I am able to use different python versions I required with conda and default python version as system default

Uday Shankar Singh
  • 531
  • 1
  • 5
  • 17