1

I would like to use my environment with python 2.7. I created my environment using

conda create -n py27 python=2.7

But when I want to access this environment I am not able to get the python version as 2.7 rather it shows python 3.7. I used this command to activate the environment.

conda activate py27

Do I need to add the environment path to the .bashrc file? I would appreciate your thoughts. Thanks. Following is the .bashrc file

 # ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color|*-256color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
    # We have color support; assume it's compliant with Ecma-48
    # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
    # a case would tend to support setf rather than setaf.)
    color_prompt=yes
    else
    color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

# added by Miniconda3 4.5.12 installer
# >>> conda init >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/home/prashant/miniconda3/bin/conda' shell.bash hook 2> /dev/null)"
if [ $? -eq 0 ]; then
    \eval "$__conda_setup"
else
    if [ -f "/home/prashant/miniconda3/etc/profile.d/conda.sh" ]; then
        . "/home/prashant/miniconda3/etc/profile.d/conda.sh"
        CONDA_CHANGEPS1=false conda activate base
    else
        \export PATH="/home/prashant/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda init <<<
Sam
  • 352
  • 2
  • 4
  • 22
  • It does not work. But here I need a specific python version. – Sam Feb 08 '19 at 06:26
  • I am using miniconda – Sam Feb 08 '19 at 06:27
  • @taurus05 the question is about creating an environment for Python 2.7. – MaJoR Feb 08 '19 at 06:39
  • @taurus05 you can create an environment for Python 2.7 using conda which comes bundled with 3.7. Miniconda is anaconda-lite (less bloated). conda is just the package manager. [This](https://stackoverflow.com/a/45421527/8726146) might help. – MaJoR Feb 08 '19 at 06:50
  • 1
    @taurus05 That's not true, you can install miniconda with 3.7 version of python and can easily install any version of python with it, so to say 2.7. I myself have installed miniconda with python3.7 and installed python2.7 as well with it. see my answer below. – Rohit Feb 08 '19 at 06:55

1 Answers1

1

With conda 4.4 you do not need to add the conda environment path to your bashrc file. The correct way to add conda to your path is by sourcing below path in your bashrc

source <path to miniconda3>/etc/profile.d/conda.sh

Once you have done that conda will be in your path.

Now regarding the conda environment not activating. Try the following things

Do you see the newly create conda environment listed with conda env list like below

conda create -n py27 python=2.7
conda env list                 
# conda environments:
#
base                     /home/rbhanot/tools/miniconda3
py27                     /home/rbhanot/tools/miniconda3/envs/py27
py37                  *  /home/rbhanot/tools/miniconda3/envs/py37

If you do not see the environment listed, then probably it is not created, if you see it then please do below steps

(py37) rbhanot@my-pc ~ » conda activate py27                                                                                                                                                    1 ↵
(py27) rbhanot@my-pc ~ » python
Python 2.7.15 |Anaconda, Inc.| (default, Dec 14 2018, 19:04:19) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'/home/rbhanot/tools/miniconda3/envs/py27/bin/python'
>>> 

As you can see once you activate the environment, the environment name will show on the prompt, that is first indication that environment has been activated. You can also check the path of python executable with sys.executable command.

Rohit
  • 3,659
  • 3
  • 35
  • 57
  • @sam no just add this line `source /etc/profile.d/conda.sh` to your rc file, `.bashrc` or `.zshrc` which ever you use and then close the terminal and launch it again, it should work for you. – Rohit Feb 08 '19 at 06:56
  • thanks Rohit. Is this ---> correct? I have added it at the end of the .bashrc file . it is not working – Sam Feb 08 '19 at 07:00
  • you need to remove `<>` from the path, say for example if your `conda.sh` exists at path `/home/sam/miniconda3/etc/profile.d/conda.sh` and you need to do `source /home/sam/miniconda3/etc/profile.d/conda.sh`. – Rohit Feb 08 '19 at 07:03
  • I could see the env even before adding the mentioned code by you – Sam Feb 08 '19 at 07:07
  • Ok once you activate the env and launch python, what is the path you see with `sys.executable` ? – Rohit Feb 08 '19 at 07:09
  • echo $PATH /home/prashant/miniconda3/bin:/home/prashant/bin:/home/prashant/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin – Sam Feb 08 '19 at 07:10
  • >>> import sys >>> sys.executable '/home/prashant/miniconda3/bin/python3' >>> – Sam Feb 08 '19 at 07:15
  • it always takes the base python and not the env python – Sam Feb 08 '19 at 07:16
  • Do you see the environment listed with `conda env list` if yes then did you activate with `conda activate py27` ? Do you see `envs` subdirectory inside your miniconda folder and inside `envs` do you see folder for your environment. Like in my case this is what I see `ls ~/tools/miniconda3/envs` --> `py27/ py37/` – Rohit Feb 08 '19 at 07:19
  • yes I did activate the environment using conda activate py27 but still it shows the same 3.7 python version. all the envs are present in the envs folder – Sam Feb 08 '19 at 07:23
  • I am working on ubuntu. I do not have such problem on macos. strange – Sam Feb 08 '19 at 07:25
  • Even I use ubuntu and I never had such issue. Infact everything seems to be in place from what you are telling. What i suspect is that your base env is being auto loaded. See if you have this `conda activate` in your `.bashrc`, if yes please remove it and re launch terminal. – Rohit Feb 08 '19 at 07:30
  • if anaconda adds path by itself then it should not be a problem? right? actually, I am stuck on this for several days now. can you please check last few lines of my .bashrc file. I have posted it. thanks a lot – Sam Feb 08 '19 at 07:48
  • Just remove the conda part you have in your bashrc and just source conda.sh. – Rohit Feb 08 '19 at 08:06
  • CONDA_CHANGEPS1=false conda activate base This one? – Sam Feb 08 '19 at 08:38
  • export PATH="/home/prashant/miniconda3/bin:$PATH" source /home/prashant/miniconda3/etc/profile.d/conda.sh – Sam Feb 08 '19 at 08:42
  • I have kept only these two lines. Rest of the lines added by conda I removed – Sam Feb 08 '19 at 08:43
  • you don't need to have this line export PATH="/home/prashant/miniconda3/bin:$PATH" – Rohit Feb 08 '19 at 09:04