I am currently making my first steps with Python and try to get Anaconda running on my mac (macOS Catalania 10.15) as my default option for python. When I type "python" in my terminal though, it is still Python 2.7 that is showing up.
I found out that in order to run anaconda as a default, I need to customize my .bash_profile. But as it seems, anaconda is already set there:
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/opt/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/opt/anaconda3/etc/profile.d/conda.sh" ]; then
. "/opt/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/opt/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
I found this question that seems to solve the same problem, but I am not sure how to apply the solution of it to my problem. A comment there states, that starting with macOS 10.15 I need to change the .zprofile. Unfortunately, I can't find the file.
Mac using default Python despite Anaconda install
I would be very greatful if any of you guys could help out!
René
Catalina by default runs zsh, which you can confirm with echo $0. The file you changed works for bash.
Copy the following anaconda init lines to .zshrc and you should be good to go:
__conda_setup="$('/opt/anaconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/opt/anaconda3/etc/profile.d/conda.sh" ]; then
. "/opt/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/opt/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
The only difference with respect to your file is replacement of shell.bash with shell.zsh.
Most commonly, the installation is fine but you can't see that until you refresh your terminal window. Just close it and open a new one.
Related
I've got Python 3.9 (thru Anaconda) and Matlab R2021b installed on my Mac (Monterey). It is my personal computer, so I should have full administrator privileges. In following the instructions here, I ran open .bash_profile and found that when setting up Anaconda it had added the following:
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('~/opt/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "~/opt/anaconda3/etc/profile.d/conda.sh" ]; then
. "~/opt/anaconda3/etc/profile.d/conda.sh"
else
export PATH="~/opt/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
so that the Python interpreter should indeed be in-PATH (?). Then, in the Matlab terminal I found that my matlabroot is /Applications/MATLAB_R2021b.app so I ran
cd /Applications/MATLAB_R2021b.app/extern/engines/python
python setup.py install
without (I think) any issues. Back in Matlab, I ran
cd (fullfile(matlabroot,'extern','engines','python'))
system('python setup.py install')
but was met with
...
running install_lib
creating /Library/Python
error: could not create '/Library/Python': Permission denied
(the ... are other, presumably successful, copy operations, which I can add back if it would be helpful).
I'm very uncomfortable working with filesystem stuff and don't understand too well how to debug/navigate these kinds of issues, so any help with getting the Matlab API up and running would be enormously helpful. Thanks in advance.
This question already has answers here:
How to choose which version of python runs from terminal?
(5 answers)
Closed 5 months ago.
On my Linux server. My python version is python3.6.5.
I run my python files by typing python3 [.py file] in terminal to use python3.6.5.
However, after I download Anaconda3 on server, the python version change to python3.8.8(type python3 --version it shows python3.8.8)
So I guess Anaconda modified ~/.bashrc to change my python version(actually I am not sure which file Anaconda has modified)
I am trying to add
export PYTHONPATH=$PYTHONPATH:/usr/lib/python3.6/site-packages
to change python3 version back to python3.6.5.
But it didn't work.(type python3 --version it still show python3.8.8)
I would like to know which Python3 version the system will choose when I type python3 in terminal.
How can I change my python3 version back to python3.6.5(python3 --version python3.6.5)
my ~/.bashrc:
export PATH=$HOME/bin:/bin:/usr/bin:/usr/local/bin:/opt/bin
export MAIL=/var/spool/mail/$USER
if [ $(uname -s) = 'SunOS' ]; then
export PYTHONPATH=$PYTHONPATH:/usr/lib/python3.6/site-packages
export PATH=$PATH:/usr/ucb:/usr/ccs/bin:/usr/local/workshop/bin
export PATH=$PATH:/usr/X11R6/bin:/usr/X11R5/bin:/usr/openwin/bin
export MANPATH=/usr/man:/usr/local/man:/usr/X11R6/man:/usr/X11R5/man:/usr/motif1.2/man:/usr/share/catman:/opt/SUNWspro/man
# for CXterm
export HZINPUTDIR=/usr/X11R6/lib/X11/cxterm.dic
export HBFPATH=/usr/local/chinese/fonts/cnprint:/usr/X11R6/lib/X11/fonts/chpower
alias b5hztty='hztty -O hz2gb:gb2big -I big2gb:gb2hz'
fi
export PS1='\h:\w> '
alias ls='ls -aF'
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'
ulimit -c 0
umask 077
#Cache Server
...
##
## put command run after interactive login in ~/.bash_profile
##
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/research/dept8/msc/xcxia21/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/research/dept8/msc/xcxia21/anaconda3/etc/profile.d/conda.sh" ]; then
. "/research/dept8/msc/xcxia21/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/research/dept8/msc/xcxia21/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
You can create alias that call python3 and pointing to python 3.6.
alias python3='directory to python 3.6'
I used anaconda3 for a python3 install. And now it's my default python:
$ which python
/home/xy/anaconda3/bin/python
$ which python3
/home/xy/anaconda3/bin/python
But I need python2 as my default python.
$ which python2
/usr/bin/python2
I tried to edit my .bashrc shown below,
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/xy/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/nu
ll)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/home/xy/anaconda3/etc/profile.d/conda.sh" ]; then
. "/home/xy/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/home/xy/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
By change the line of export PATH... to
export PATH="$PATH:/home/xy/anaconda3/bin"
It didn't change anything.
How should I set python2 back as the default?
I think the cleanest way to go forward is to do following changes:
1) Edit your ~/.bashrc and do following modifications
Keep this block. Do not edit it. If you already deleted it, you can recreate it by typing conda init bash.
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/xy/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/nu
ll)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/home/xy/anaconda3/etc/profile.d/conda.sh" ]; then
. "/home/xy/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/home/xy/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
2) Make sure you /home/xy/anaconda3/bin is not added to PATH outside of this block. If so, delete those.
3) Call conda config --set auto_activate_base False in your shell
From now on, you have to activate the anaconda environment manually with conda activate base. If you do not call this, you will default back to your system python.
I have created an image with a bash script called by ENTRYPOINT that itself launches an executable from a conda environment. I'm building this from a single layer directly (for now) which I realize is not best practice, but let's ignore that for a hot second...
Dockerfile
FROM alexholehouse/seq_demo:demo_early
SHELL ["/bin/bash", "-c"]
ENTRYPOINT ["/seq_demo/launcher/launcher.sh"]
Where launcher.sh is
#!/bin/bash
# source bashrc which includes conda init section (and works fine in an interactive terminal)
source /root/.bashrc
# activate the conda environment
conda activate custom_conda
if [ -d /mount ]
then
cd /mount
# run the executable from the conda environment
demo_seq -k KEYFILE.kf
else
echo "No storage mounted..."
fi
Now the problem is that when I build the image using the above Dockerfile, the .bashrc file doesn't get sourced because of the following (standard) line at the top of .bashrc.
[ -z "$PS1" ] && return
... <bashrc stuff>
__conda_setup="$('/root/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/root/miniconda3/etc/profile.d/conda.sh" ]; then
. "/root/miniconda3/etc/profile.d/conda.sh"
else
export PATH="/root/miniconda3/bin:$PATH"
fi
fi
unset __conda_setup
So running the image using
docker run -it -v $(pwd):/mount b29c47a060
Means .bashrc is not sourced and the launcher.sh fails because conda can't be found.
If, on the other hand, I edit .bashrc so all the conda stuff is above the [ -z "$PS1" ] && return line then (a) conda gets sourced and (b) the rest of the .bashrc is read too.
Now, editing .bashrc solves my issue but this cannot be the right way to do this! So, what's the correct way to set up an image/Dockerfile so:
(a) A specific bash script gets run upon running the container and
(b) That bash script sources the .bashrc
I feel like I'm just missing something super obvious here...
$PS1 is containing your command prompt (symbol/ e.g. '#: ').
Example of changing prompt
So you have to figure out why PS1 isnt set in the first place. Because [ -z "$PS1" ] && return will exit the script only when $PS1 is not set at all.
When the baseimage youre using doesnt provide any you might just add on in your Dockerfile via ENV PS1.
In case you never ever login into your container to use command line you might just drop that check.
See here for more information about how PS1 is propagated in bash.
I'm executing with pycharm the following:
print(os.environ["PATH"]) # returns '/usr/bin:/bin:/usr/sbin:/sbin'
But when I execute echo $PATH in a shell this is returned:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin:/opt/local/sbin
I tried to edit it in Preferences > Console > Python Console > Environment Variables, setting
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin:/opt/local/sbin
but this is not working
any idea?
On Ubuntu, using zsh, I stumbled upon the same problem.
The hack I use in order to have the same environment variables in PyCharm and my shell is to launch PyCharm from my terminal instead of using the icon. It looks like this way that the PyCharm shell inherits from the main shell it's been launched from.
I hope it can solve other people's problem as I wasn't able to replicate #Steve Tarver's solution on Linux (.../terminal/.zshrc was read only on /snap/, even when using sudo).
#fj123x, I'm going to guess from your post that you are
on a mac
using a shell other than bash (perhaps zsh)
If true, the problem is that the JetBrains jediterm terminal emulator is not executing all shell startup files in the correct order.
If you are using zsh, you can fix that root problem by editing the terminal plugin's .zshrc. PyCharm is in your Applications folder, open /Applications/PyCharm.app/Contents/plugins/terminal/.zshrc and replace the contents with:
#!/bin/zsh
# starver mod
# Jetbrains uses jediterm as a java terminal emulator for all terminal uses.
# There are some apparent limits on use:
# - must use old-style shebang - not the #!/usr/bin/env zsh
# - must implement the startup file loading here
#
# Note: original contents are in lib/terminal.jar
# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
bindkey '^[^[[C' forward-word
bindkey '^[^[[D' backward-word
ZDOTDIR=$_OLD_ZDOTDIR
if [ -n "$JEDITERM_USER_RCFILE" ]
then
source "$JEDITERM_USER_RCFILE"
unset JEDITERM_USER_RCFILE
fi
if [ -n "$ZDOTDIR" ]
then
DOTDIR=$ZDOTDIR
else
DOTDIR=$HOME
fi
if [ -f "/etc/zshenv" ]; then
source "/etc/zshenv"
fi
if [ -f "$DOTDIR/.zshenv" ]; then
source "$DOTDIR/.zshenv"
fi
if [ -n $LOGIN_SHELL ]; then
if [ -f "/etc/zprofile" ]; then
source "/etc/zprofile"
fi
if [ -f "$DOTDIR/.zprofile" ]; then
source "$DOTDIR/.zprofile"
fi
fi
if [ -f "/etc/zshrc" ]; then
source "/etc/zshrc"
fi
if [ -f "$DOTDIR/.zshrc" ]; then
source "$DOTDIR/.zshrc"
fi
if [ -n $LOGIN_SHELL ]; then
if [ -f "/etc/zlogin" ]; then
source "/etc/zlogin"
fi
if [ -f "$DOTDIR/.zlogin" ]; then
source "$DOTDIR/.zlogin"
fi
fi
if [ -n "$JEDITERM_SOURCE" ]
then
source $(echo $JEDITERM_SOURCE)
unset JEDITERM_SOURCE
fi
If you are interested in all the gory details, or you want to see how I solved this problem so you can develop a solution for another shell, see this answer: https://stackoverflow.com/a/51006003/1089228
I work on the command line in bash and my environment, including $PATH, is set in .bash_profile. The default terminal in PyCharm is tcsh.
I changed it to bash by going
File ... Default Settings ... Tools ... Terminal ... Shell Path
and then restaring. The embedded terminal worked as expected.