gerrymanderer
gerrymanderer

Reputation: 33

Why am I stuck with the system default Python 2 interpreter after activating a Python 3 Conda environment?

I created a new Conda environment on an x86-64 Linux mainframe, using the command

conda create --name myenv --file somefile.txt --python=3.8.

I double checked my Python version in this environment using conda list, which returns

...
python                    3.8.3                hcff3b4d_0
...

However, after activating this environment, Python 3 scripts doesn't run, and running which python reveals that the environment defaults to using the default system Python 2 interpreter:

$ which python
/usr/bin/python

My Efforts So Far

First, I added the line export PATH=$PATH:/home/miniconda3/envs/myenv/bin to my ~/.bashrc and ~/.profile files, to no effect. which python still returns /usr/bin/python.

I then checked my alias file to see if python is aliased to Python 2. But there is no entry in the alias file about python.

For your reference

  1. my ~/.bashrc looks like this:
$ cat ~/.bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/miniconda3/etc/profile.d/conda.sh" ]; then
        . "/home/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

alias julia="/home/julia-1.4.0/bin/julia"

export PATH=$PATH:/home/miniconda3/envs/myenv/bin
$ cat ~/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
$ cat ~/.profile
export PATH=$PATH:/home/miniconda3/envs/myenv/bin

Upvotes: 1

Views: 160

Answers (1)

Donald S
Donald S

Reputation: 1753

I think you need to add your env path to the front of the PATH variable so it will find python there first. The OS will look for a file or application in the PATH list, and use the first match that it finds.

Change

export PATH=$PATH:/home/qingyanz/miniconda3/envs/myenv/bin

to this:

export PATH=/home/qingyanz/miniconda3/envs/myenv/bin:$PATH

As an example, here are the settings on my conda environment:

(ds_tensorflow) C:\Users\me>which python /c/Users/me/miniconda3/envs/ds_tensorflow/python

(ds_tensorflow) C:\Users\me>env | grep PATH ... PATH=/c/Users/me/miniconda3/envs/ds_tensorflow:/mingw-w64/bin:/usr/bin:/bin:...

Upvotes: 2

Related Questions