# Handling multiple python versions
[Python Tutorial: How to Set the Path and Switch Between Different Versions/Executables (Mac & Linux)](https://www.youtube.com/watch?v=PUIE7CPANfo&list=PL-osiE80TeTt2d9bfVyTiXJA-UTHn6WwU&index=34)
I have installed two pythons in my Mac.
- The one is installed by miniconda.
- The other is installed manually.
## Outline
1. Which Python interpreter are using and where it's located.
2. Troubleshoot imports if they aren't working correctly.
3. Switch between different Python versions and environments.
### 1. Which Python interpreter are using and where it's located.
```bash
which python # /Users/joe/miniconda3/bin/python
which pip # /Users/joe/miniconda3/bin/pip
```
```bash
type python # python is /Users/joe/miniconda3/bin/python
type pip # pip is hashed (/Users/joe/miniconda3/bin/pip)
```
The output shows where and what's the current `python`.
```bash
echo $PATH
```
The output is where to find `python` in the following order:
```
/Users/joe/miniconda3/bin:
/Users/joe/miniconda3/condabin:
/usr/local/bin:
/usr/bin:
/bin:
/usr/sbin:
/sbin
```
### 2. Troubleshoot imports if they aren't working correctly.
Assume the `python` version installed in miniconda3 `Python 3.7.3`.
And I've install `Python 3.7.6` in my Mac, but the `PATH` to `Python 3.7.6` haven't set yet.
Add the `Python 3.7.6` path to the `.bash_profile`.
```bash
nano ~/.bash_profile
```
Add the following code in the `.bash_profile`
```bash
# Setting PATH for Python 3.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
export PATH
```
Check the current `PATH variables` (close the terminal & reopen to make sure the changes have been made)
```bash
echo $PATH
```
```
/Library/Frameworks/Python.framework/Versions/3.7/bin:
/Users/joe/miniconda3/bin:
/Users/joe/miniconda3/condabin:
/usr/local/bin:
/usr/bin:
/bin:
/usr/sbin:
/sbin
```
And the `PATH for Python 3.7` has been set to the first element in the `PATH`.
That's because the `path` is added before `:${PATH}"` (It means to continue with the old path.)
### Care for which Python you are use: `python` might be different from `python3`
For example
```bash
which python # /Users/joe/miniconda3/bin/python
which pip # /Users/joe/miniconda3/bin/pip
which python3 # /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
which pip3 # /Library/Frameworks/Python.framework/Versions/3.7/bin/pip3
```
- `which` is less use because nowadays their maybe some `alias` setting in your `.bash_profile`
- `type` first check for `alias` else it'll show the `path` (It's better!)
```bash
type python # python is /Users/joe/miniconda3/bin/python
type pip # pip is hashed (/Users/joe/miniconda3/bin/pip)
type python3 # python3 is /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
type pip3 # /Library/Frameworks/Python.framework/Versions/3.7/bin/pip3
```
### See which Python we're running...(for debug)
In python:
```python
import sys
sys.executable
```
We can see where our current executable is located
```
'/Library/Frameworks/Python.framework/Versions/3.7/bin/python3'
```
In terminal:
```bash
echo $PATH
```
You could compare the executable location with your path.
```
/Library/Frameworks/Python.framework/Versions/3.7/bin:/Users/joe/miniconda3/bin:/Users/joe/miniconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
```
### pip install a package but when import that package it doesn't work
```
pip show package_1
```
It'll show the infomation of package_1 (including where it is installed).
### 3. Switch between different Python versions and environments
- [Python Tutorial: How I Manage Multiple Projects, Virtual Environments, and Environment Variables](https://www.youtube.com/watch?v=cY2NXB_Tqq0&list=PL-osiE80TeTt2d9bfVyTiXJA-UTHn6WwU&index=17&t=0s)
- The environment automatically gets activated and also sets the environment variables just by **navigating to the project folder** that contains the `environment.yml` file
- Advantages:
1. Don't need to **deactivate** other project's environment beforehand.
2. Keeps you from needing to remember to switch back and forth between the **correct** environments or even needing to remember the **name** of your environment.
- Two Steps:
- [saving-environment-variables](https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#saving-environment-variables)
- conda_auto_env():
- export an `environment.yml` file in your project folder
- copy and paste the following code in the `.bash_profle`
```
# Modified from:
# https://github.com/CoreyMSchafer/code_snippets/blob/master/conda_auto_env.s
# https://github.com/chdoig/conda-auto-env
# Auto activate conda environments
function conda_auto_env() {
if [ -e "environment.yml" ]; then
ENV_NAME=$(head -n 1 environment.yml | cut -f2 -d ' ')
# Check if you are already in the environment
if [[ $CONDA_PREFIX != *$ENV_NAME* ]]; then
# Try to activate environment
conda activate $ENV_NAME &>/dev/null
fi
fi
}
export PROMPT_COMMAND="conda_auto_env;$PROMPT_COMMAND"
```
- [Python Tutorial: Custom Sublime Text Build Systems](https://www.youtube.com/watch?v=xqcTfplzr7c)
###### tags: `Python`