What are bashrc and zshrc?
The .bashrc file (for Linux or other Bash environments) and the .zshrc files (for MacOS or other Zsh environments) are both files on your local system used to create aliases, which are like keyboard shortcuts for your shell or terminal. Zshrc is more in-depth as it can not only be used to set aliases but can also include setup scripts and default environment variables. In this article, we’ll just be covering aliases, using either .bashrc or .zshrc.
As an example, we’ll be setting the aliases for ‘python’ and ‘pip’ in your local shell.
*If you’re using pyenv to manage your Python versions, then you can just follow the steps below to set up an alias for ‘pip’, as pyenv already handles the ‘python’ alias for you.
My .bashrc file
Where art thou?
The .bashrc file is located here: ~/.bashrc
The .zshrc file is located here: ~/.zshrc
Background
If you’re like most even casual Python developers, you’ve worked with Python 2, which was the standard for years. The Python Software Foundation announced the End of Life (EOL) for Python 2, meaning the core development team will no longer support, update or provide new versions of Python 2 as of January 1, 2020. Please see Sunsetting Python 2 on Python.org for more details - https://www.python.org/doc/sunset-python-2/)
With the upgrade from Python 2 to Python 3, also came the upgrade from pip, the package installer for Python 2, to pip3, the package installer for Python3.
This left a lot of people wondering, including myself, which versions of Python and Pip am I invoking when I type 'python' or 'pip'. Is it version 2 or version3?
Instead of always wondering or worrying which version you’re using, let’s set it up so we’re ALWAYS referring to Python 3 and pip 3 when we type the aliases ‘python’ or ‘pip’.
The instructions below cover Linux and Mac OS systems. If you’re on a Windows system and want to create aliases for ‘python’ and ‘pip’ to refer to their version 3 incarnations, the process is completely different, so please see this article:
Consider Python - Making aliases on Windows
Creating aliases for ‘python’ and ‘pip’ in Linux and Mac OS systems
STEP 1: Figure out where Python 3 and pip 3 are installed. (If they’re not installed, you’ll want to do that first using yum, apt-get, brew, etc.).
which python3 # /usr/local/bin/python3
which pip3 # /usr/local/bin/pip3
Your response may be different, depending on the operating system you use. Make a note of each response, so that you can add these paths to your shell environment’s config files.
STEP 2: Confirm ‘python’ and ‘pip’ aliases are different than the ‘python3’ and ‘pip3’ aliases
which python
If it’s already aliased to Python 3, the response will look something like this: ‘aliased to /usr/local/bin/python3’
So, if the response is something like ‘usr/local/bin/python’ or ‘python: command not found’, then we still have work to do. On to Pip...
which pip
If it’s already aliased to pip 3, the response will look something like this: ‘aliased to /usr/local/bin/pip3’
So, if the response is something like ‘usr/local/bin/pip’ or ‘pip: command not found’, then we still have work to do.
STEP 3: Add an alias for ‘python’ and ‘pip’ to your config file.
# Linux or other bash environment
echo "alias python=/usr/local/bin/python3" >> ~/.bashrc
echo "alias pip=/usr/local/bin/pip3" >> ~/.bashrc
# Mac OS or other zsh environment
echo "alias python=/usr/local/bin/python3" >> ~/.zshrc
echo "alias pip=/usr/local/bin/pip3" >> ~/.zshrc
STEP 4: Don’t forget to load the config file:
source ~/.bashrc
source ~/.zshrc
STEP 5: Confirm the alias has been set:
which python #python: aliased to /usr/local/bin/python3
which pip #pip: aliased to /usr/local/bin/pip3
That’s it. Every time you type ‘python’ or ‘pip’, it will be for version 3. If later, in order to work with legacy code, you need to use Python 2.7, you can invoke it with ‘python2.7’.
For tips on how to manage multiple python versions and environments, see our new article pyenv for Managing Python Versions and Environments <-- click here