Search code examples
pythonpathanacondaenvironment-variablesconda

Conda using /usr/local/bin/python instead of conda environment python


After I activate my conda environment and I run which python, I get the following

/usr/local/bin/python

As you can see it doesn't use the python which should be in the conda environment, i.e /Users/zeus/opt/anaconda3/envs/po/bin/python

How can I fix this issue? I'm using MacOS

This is my conda info -a

# conda environments:
#
base                     /Users/zeus/opt/anaconda3
discoe                   /Users/zeus/opt/anaconda3/envs/discoe
po                    *  /Users/zeus/opt/anaconda3/envs/po
py38                     /Users/zeus/opt/anaconda3/envs/py38
test                     /Users/zeus/opt/anaconda3/envs/test

sys.version: 3.9.7 (default, Sep 16 2021, 08:50:36)
...
sys.prefix: /Users/zeus/opt/anaconda3
sys.executable: /Users/zeus/opt/anaconda3/bin/python
conda location: /Users/zeus/opt/anaconda3/lib/python3.9/site-packages/conda
conda-build: /Users/zeus/opt/anaconda3/bin/conda-build
conda-content-trust: /Users/zeus/opt/anaconda3/bin/conda-content-trust
conda-convert: /Users/zeus/opt/anaconda3/bin/conda-convert
conda-debug: /Users/zeus/opt/anaconda3/bin/conda-debug
conda-develop: /Users/zeus/opt/anaconda3/bin/conda-develop
conda-env: /Users/zeus/opt/anaconda3/bin/conda-env
conda-index: /Users/zeus/opt/anaconda3/bin/conda-index
conda-inspect: /Users/zeus/opt/anaconda3/bin/conda-inspect
conda-metapackage: /Users/zeus/opt/anaconda3/bin/conda-metapackage
conda-pack: /Users/zeus/opt/anaconda3/bin/conda-pack
conda-render: /Users/zeus/opt/anaconda3/bin/conda-render
conda-repo: /Users/zeus/opt/anaconda3/bin/conda-repo
conda-server: /Users/zeus/opt/anaconda3/bin/conda-server
conda-skeleton: /Users/zeus/opt/anaconda3/bin/conda-skeleton
conda-token: /Users/zeus/opt/anaconda3/bin/conda-token
conda-verify: /Users/zeus/opt/anaconda3/bin/conda-verify
user site dirs:

CIO_TEST: <not set>
CONDA_DEFAULT_ENV: po
CONDA_EXE: /Users/zeus/opt/anaconda3/bin/conda
CONDA_PREFIX: /Users/zeus/opt/anaconda3/envs/po
CONDA_PREFIX_1: /Users/zeus/opt/anaconda3
CONDA_PROMPT_MODIFIER: (po)
CONDA_PYTHON_EXE: /Users/zeus/opt/anaconda3/bin/python
CONDA_ROOT: /Users/zeus/opt/anaconda3
CONDA_SHLVL: 2
CURL_CA_BUNDLE: <not set>
DISCOE_PATH: /Users/zeus/Project/discoe
PATH: /Users/zeus/opt/anaconda3/bin:/Users/zeus/.poetry/bin:/Users/zeus/.nvm/versions/node/v16.13.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/zeus/opt/anaconda3/envs/po/bin:.:/Users/zeus/.local/bin:/Users/zeus/.local/bin
REQUESTS_CA_BUNDLE: <not set>
SSL_CERT_FILE: <not set>

this is what my $PATH is /Users/zeus/.poetry/bin:/Users/zeus/.nvm/versions/node/v16.13.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/zeus/opt/anaconda3/envs/po/bin:.:/Users/zeus/.local/bin:/Users/zeus/.local/bin

I activate my conda environment by this command conda activate po


Solution

  • To resolve the issue, you need to deactivate all Conda environments completely. This includes not only your specific project environment but also the base (default) environment.

    Here are the steps:

    1. If you are in a specific Conda environment, deactivate it using the command: conda deactivate
    2. You might notice that you are still in the base Conda environment. To deactivate from the base environment, use the same command again: conda deactivate

    Now, you should be completely out of all Conda environments.

    The reason behind this is that Conda and other Python environment managers manipulate the system path. When you activate a Conda environment, it prepends the path of the active environment's binary directory to your system PATH. So, when you're in a Conda environment, Python executables and scripts installed in that environment take precedence over system-wide installed ones. This is why it's necessary to deactivate all Conda environments to ensure you're using the system-wide Python.

    For more details, you can refer to this article: https://towardsdatascience.com/python-the-system-path-and-how-conda-and-pyenv-manipulate-it-234f8e8bbc3e