Ok Let’s Tweak our Terminal!, brew install python— Prepare Our New Dev Rifle — Part 2
In this part2 of preparing our Nu Nu Dev Machine, we will install python 3 from home brew, learn how best to work with virtual environments and tweak our terminal.
First Steps — This assumes that your system is ready to brew, check out our part1 of preparing our Nu Nu Dev machine here. I kindly recommend you to check it out— it’s awesome — and it shows how to initially configure our Dev machine.
Let’s Tweak our Terminal — Terminal Pro!
Oh-My-Zsh — Zsh is a shell designed for interactive use. — Oh My Zsh will not make you a 10x developer…but you might feel like one.
Basic Installation
via curl
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
via wget
sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
Follow the prompts, once installation is done, press command + Q
to close your terminal. Open again your terminal and run this command to check if our default shell is zsh.
$ echo $SHELL
/bin/zsh
Ok, We will add themes later, Let’s jump and install home brew python.
Python — Let’s brew install python
, this will install latest version of Python via Homebrew. Hang on — But, there is python
already installed by default on a macOS — Yes, python bundled with macOS will become out-of-date. Homebrew always has the most recent version which too includes the latest versions of Pip and Setuptools.
brew install python
The command above will install python 3.x via Home brew
Now run this command..
$ brew info python
python: stable 3.7.1 (bottled), HEAD
Interpreted, interactive, object-oriented programming language
--
Install HEAD version
==> Caveats
Python has been installed as
/usr/local/bin/python3Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to `python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/usr/local/opt/python/libexec/binIf you need Homebrew's Python 2.7 run
brew install python@2
Let’s focus on the Caveats, we realise there are some soft links (symlinks) created, so we will modify our environment PATH variable, and make our home brew python the default in the terminal.
Since we installed the zsh shell, check if you have the .zshrc
file
$ ls -la ~/.zshrc
-rw-r--r-- 1 user staff 4093 Nov 8 18:45 /Users/user/.zshrc
Z-shell resource. It’s a script that is run whenever you start zsh. If you have certain paths to set, or initialisations you want performed at the start-up of the shell, they are put in ~/.zshrc.
Make Home brew python the default
$ echo 'export PATH="/usr/local/opt/python/libexec/bin:$PATH"' >> ~/.zshrc
source the file to apply the changes without restarting the terminal app
$ source ~/.zshrc
then check your terminal default python..
$ python --version
Python 3.7.1
Pip — is a package manager for Python packages, or modules if you like.
from the command brew info python
, we noticed a symlink pip
pointing to pip3
. So when we installed python using home brew, also pip was installed.., let’s confirm by running this command.
$ pip --version
pip 18.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
WARNING NOTE: Don’t pip install my_cool_package
at a moment if your are not working with virtual environments.
Virtualenv + Virtualenvwrapper
Virtualenv is a tool that lets you create an isolated Python environment for your project. It creates an environment that has its own installation directories, that doesn’t share dependencies with other virtualenv
environments (and optionally doesn’t access the globally installed dependencies either). You can even configure what version of Python you want to use for each individual environment. It's very much recommended to use virtualenv
when dealing with Python applications.
To install virtualenv
run:
$ pip install virtualenv
Usage
If you have a project in a directory called my-project
you can set up virtualenv
for that project by running:
$ cd my-project/
$ virtualenv venv
This command creates a venv/
directory in your my-project where all dependencies are installed. You need to activate it first though (in every terminal instance where you are working on your project):
$ source venv/bin/activate
You should see a (venv)
appear at the beginning of your terminal prompt indicating that you are working inside the virtualenv
. Now when you install something like this:
$ pip install <package>
It will get installed in the venv/
folder, and not conflict with other projects.
To leave the virtual environment run:
$ deactivate
Virtualenvwrapper — It’s an extension to virtualenv
and makes it easier to create and delete virtual environments without creating dependency conflicts.
To make it easier to work on multiple projects that has separate environments you can install virtualenvwrapper
.
To install virtualenvwrapper
run:
$ pip install virtualenvwrapper
Note: virtualenvwrapper
keeps all the virtual environments in WORKON_HOME variable
, let me set my WORKON_HOME=$HOME/.venvs
Basic Configurations
Add these settings to your ~/.zshrc
file
export WORKON_HOME=~/.venvs
source /usr/local/bin/virtualenvwrapper.sh
Source your ~/.zshrc
file to reload the changes
$ source ~/.zshrc
Usage
To create a virtual environment, run
mkvirtualenv env1
Now we can install some software into the environment.
(env1)$ pip install django
Of course we are not limited to a single virtualenv:
(env1)$ mkvirtualenv env2
Switch between environments with workon env1
(env2)$ workon env1
Deactivate
(env2)$ deactivate
Kindly don’t hesitate to check on the virtualenvwrapper command reference here
Restricting Pip to virtual environments
What happens if we think we are working in an active virtual environment, but there actually is no virtual environment active, and we install something via pip install foobar
? Well, in that case the foobar
package gets installed into our global site-packages, defeating the purpose of our virtual environment isolation
Yes, Pip has an undocumented setting that tells it to bail out if there is no active virtual environment. Let’s set it in the pip configuration file.
Create some directory to store our Pip configuration file:
$ mkdir ~/Library/Application\ Support/pip
Now let us create the pip.conf
file
$ vi ~/Library/Application\ Support/pip/pip.conf
add these settings to our file..
[install]
require-virtualenv = true
[uninstall]
require-virtualenv = true
Now, let’s see what happens when we try to install a package in the absence of an activated virtual environment:
$ pip install flask
Could not find an activated virtualenv (required).
Sweet! Hang on — so how do we install or upgrade a global package? We can temporarily turn off this restriction by defining a new function in ~/.zshrc
gpip(){
PIP_REQUIRE_VIRTUALENV="0" pip3 "$@"
}
(As usual, after adding the above you must run source ~/.zshrc
for the change to take effect.)
If in the future we want to upgrade our global packages, the above function enables us to do so via:
$ gpip install --upgrade pip setuptools wheel virtualenv
Ok! Thats it for today.., Next in our part3, we continue with tweaking our terminal, install themes, iterm2 and pimp our vi editor. #Happy pythoning!