Setting PATH with multiple Python versions under MacOS - bash

I would like to make the version of python 2 packaged with MacOS the system default Please show me the recommended approach.
I have installed a Python 3 version from miniconda/anaconda, and my current default Python is:
$ which python
/Users/PatrickT/miniconda3/bin/python
I do not want this to be the default Python anymore. The dilemma is: if I remove conda from the PATH, I can access the Python version packaged with the OS, but then I can no longer access the conda Python...

It sounds like you might want a python version manager, such as https://github.com/yyuu/pyenv
I can't vouch for this specific tool, but there are several similar utilities for ruby (rbenv, rmv) that are great.
OS X (or is it macOS now?) come with an installed version of Python. You almost certainly don't want to mess with that since updates to the OS will likely overwrite your changes. You may want to consider a tool called homebrew for OS X -- it lets you install specific versions of tools like python and manages them externally to the built-in versions.

Edit the .bash_profile
$ nano ~/.bash_profile
1. delete any reference to "export PATH=" to non-default-os python.
2. set up aliases to alternative python versions.
In other words, replace e.g. this:
# added by Miniconda3 4.0.5 installer
export PATH="/Users/PatrickT/miniconda3/bin:$PATH"
with this:
## create alias to miniconda/anaconda
## to make conda command accessible, first run: condainit
alias condainit='export PATH="/Users/PatrickT/miniconda3/bin:$PATH"'
## create alias to other python versions
alias pyconda='/Users/PatrickT/miniconda3/bin/python'
alias python3='/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}'
## After installing pyenv with homebrew and run $ brew info pyenv
## set the following, according to the "caveat instructions":
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
Source the .bash_profile:
$ source ~/.bash_profile
To access conda commands run this in the terminal:
$ condainit
$ conda info -e
To access the Python3 provided by conda, type:
$ pyconda
To access the Python3 installed via the dmg, type:
$ python3
To access the default OS-provided python (currently Python 2.7), type:
$ python
Among other sources, the following was particularly useful: Using two different Python Distributions

Related

An Anaconda environment won't activate?

I'm on a mac, and I'm trying to download a software called DLITE (which can be found here https://github.com/AllenCellModeling/DLITE). However, the instructions provided don't work I'm using Anaconda, and when I try and activate the environment that I've created, I get the following error message :
conda init dlite
ArgumentError: Invalid shells:
dlite
Currently available shells are:
bash
fish
powershell
tcsh
xonsh
zsh
I haven't used Anaconda or python much before, and I have no idea what to do to fix this, although I've tried a few things.
$ conda create --name dlite python=3.7
$ conda activate dlite
The command above will create the environment (as written in the dlite tutorial.
$ git clone https://github.com/AllenCellModeling/DLITE.git
$ cd DLITE
$ pip install -e .[all]
This will install the needed packages.
Hope this helped.

Why has the link from /usr/local/bin/python3 a different path from "which python3"?

I installed python 3.6 before and installed python 3.7.4 today. When I type python3 in command, it opens python 3.6, I want to change it to python 3.7.4.
which python3 shows /Library/Frameworks/Python.framework/Versions/3.6/bin/python3,
but the link in /usr/local/bin/ is :
python3 -> ../../../Library/Frameworks/Python.framework/Versions/3.7/bin/python3
so is the case of pip3. why? ?
How can I change which python3 to python 3.7.4? I don't want to use alias.
I use MacOS 10.14.2
Your OS uses the PATH environment variable to look for the commands you write into the shell, so if you type python3 it will go through the directories listed in this PATH and check if there's your programm. It takes the first matching program and executes it, so in your case the directory /Library/Frameworks/.../3.6/bin is before the directory usr/local/bin, which means that the python3 from /Library/Frameworks/.../3.6/bin will be used.
You need therefore to change this PATH variable:
export PATH="/Users/sky/Documents/software/Montage-master/bin:/usr/share/file/magic/mercurial:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/mysql/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin"
You can put that into your ~/.bash_profile so that it is permanent, and you don't need to set it every time you open a new terminal window.
Note that this will not automatically update your path for the remainder of the session. To do this, you should run:
source ~/.bash_profile
Are you sure have python3.7 intalled?
you can view the folder
cd /usr/bin
Next you search the python's file:
find /usr/bin/ python3
if donĀ“t exist filename python3.7 install
sudo apt install python3.7
sudo apt update

Mac OS - Installed Python 3 via Anaconda, but no feed back of which python3

As a Python beginner, I installed Python 3 via Anaconda, and have successfully installed it in my Mac laptop. I have no problem to use Python 3 if I launch Jupyter notebook, but I cannot locate it in the terminal.
Specifically, if is type in:
$ which python
I got the feedback of
/usr/bin/python
but if I type in
$ which python3
There is no feedback at all. Just curious if I have missed anything.
When you install a new environment with Anaconda with a specific version of Python, that version of Python will be accessible only if you activate the Anaconda's environment.
If you want to access to your python 3 of your environnement from the terminal, you need to activate it :
source activate my_env
Then you can write "which python3"
For example, on my computer, you can see two different versions of Python depending on the environnement I use or the version installed on my Mac.
// On my mac
which python3
>> /Users/michaelcaraccio/anaconda/bin/python3
Then when I activate the env :
which python3
>> /Users/michaelcaraccio/anaconda/envs/TM/bin/python3
If you need more information, don't hesitate to ask :)

Updating brew package in bash script on OSX

I'm working on a bash script that depends on specific package. This script will be used globally, on different machines. Some of them will have it, and some not.
How could I verify, as part of the bash script, if package is installed and to update/install it if needed?
My package manager (OSX 10.12) is brew.
You could write sth like:
#!/bin/bash
if brew ls --versions the_package_name > /dev/null; then
# whatever you want to do
else
brew install the_package_name
end

`which python` in virtualenvwrapper.sh screws up in arch linux

On arch linux, after install virtualenvwrapper system-wide, via sudo pip2 install virtualenvwrapper and adding this in my user's .bash_profile,
export WORKON_HOME=/home/myuser/.virtualenvs
export PROJECT_HOME=/home/myuser/work
source /usr/bin/virtualenvwrapper.sh
An error shows up whenever I launch a new shell window:-
which: no python in (/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/bin/core_perl)
-bash: : command not found
virtualenvwrapper.sh: There was a problem running the initialization hooks. If Python could not import the module virtualenvwrapper.hook_loader, check that virtualenv has been installed for VIRTUALENVWRAPPER_PYTHON= and that PATH is set properly.
This can be traced to line 50 in the virtualenvwrapper.sh script:-
47 # Locate the global Python where virtualenvwrapper is installed.
48 if [ "$VIRTUALENVWRAPPER_PYTHON" = "" ]
49 then
50 VIRTUALENVWRAPPER_PYTHON="$(\which python)"
51 fi
And is a result of the conflict with arch linux's convention of using python2 for python 2.7 installation.
If I modify line 50 to which python2, everything works perfectly and I will not see the error message whenever I launch a new shell.
What is the appropriate way of resolving this problem? Do I write an explicit check that the current OS is arch linux and introduce an if-else condition to use which python2 in virtualenvwrapper.sh and send the patch to the virtualenvwrapper author? Or is there something I can do with my configuration in my arch linux machine?
Virtualenvwrapper has variables VIRTUALENVWRAPPER_PYTHON and VIRTUALENVWRAPPER_VIRTUALENV which point to your python and virtualenv executables. So, in your .bash_profile you can write something like:
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/bin/virtualenv2
source /usr/bin/virtualenvwrapper.sh
To answer this arch-specific python quirk (the convention of using python2), I specifically create a soft link like this:-
ln -s /usr/bin/python2 /usr/local/bin/python
Since /usr/local/bin precedes /usr/bin in the bash system PATH environment variable, when I type python, or when the virtualenvwrapper.sh script refers to which python in line 50, we will no longer run into the above mentioned virtualenvwrapper conflict and everything works as expected.
The disadvantage of doing this is that it might mess up the "python 3" (being considered as the default python command) conventions arch linux is designed for. So care has to be taken that I do not use python 3 related libraries or package installations relating to python 3 in my target box.
I found there is no need to edit the /usr/bin/virtualenvwrapper.sh script or create a new symbolic link. I was just missing the virtualenvwrapper module for python2. I installed it like the following:
pip2 install virtualenvwrapper
and keeped the exports so the magic is done.

Resources