Installing Python
Do you know how to open the command line yet? If not, look under “Getting Started” on the sidebar. This will still probably be a very stressful experience, but I promise we’ll get through it together.
Python 2 vs Python 3
Once upon a time there was Python 2, and everyone was happy. Except… not really, because Python did a lot of things poorly, especially when you stopped working with the English alphabet (a.k.a. the Latin alphabet). Àçčêñtś were a disaster, and forget about using 中文 or 한글 without running into a hundred problems. Data is international, and that’s why we’re using Python 3.
A lot of people still use Python 2, though. And they can be a little vocal about it, too.
It used to make sense when a lot of software hadn’t been upgraded to use Python 3, but not any more! So when you see people yelling about Python 2 vs Python 3, just know you’re on the right team.
Installing Python 3 & Friends
I’m going to be honest with you: this is a lot of pretty crazy stuff to have to do at orientation.
Installation can cause a lot of headaches, though, and we don’t want anyone to be held up during class by little issues here or there. You might not understand everything that we’re doing below, but you’ll definitely “get it” in a few weeks.
Also, don’t hestitate to ask for help if you seem to run into issues!
If/when you get a prompt that says “Password: []” and you type and type and nothing shows up just type the password and hit enter, it doesn’t look like it’s doing anything but it is!
Step 0: Uninstalling Anaconda (OS X only)
Never heard of Anaconda before, except as the largest snake in the world? You can ignore this step. 99% of the people in the world can skip this step except people who took certain data classes at Columbia Journalism School.
Maybe in a past life someone installed Anaconda on your machine to deal with your Python needs. We’re going to hide it away. You probably only need to do this step if you’re done Python data work previously, or you went to Columbia and took a data course.
Don’t get me wrong, Anaconda is wonderful - but only as long as you’re staying in Anaconda. Once you try to do something else (like we’re going to do!) it causes all sorts of trouble.
Here’s what you do to deactivate anaconda (Note: this doesn’t uninstall it, just kind of hides it a little. We could remove it complete, too, if you’re okay with that.):
- Open up Atom, your text editor
- Go to
File
>
Open`. Select your user directory - the directory that has your name in it. You might need to click up at the very top of the selector where it says the current directory you’re in (Documents, maybe). - Look around on the left-hand side for a file called
.bash_profile
. Double-click it. - If there’s a line that reads
export PATH="/Users/soma/anaconda/bin:$PATH"
, add a#
in front of it. Save the file.
Step 1: Installing prerequisites and Python 3
Mac OS X only
You might need to install the XCode Command Line Tools. Try the following line in a new Terminal window.
xcode-select --install
Homebrew is a package manager for OS X - it helps us install pieces of software more easily than downloading them individually. We’re going to install Homebrew and then use it to install Python 3. First, install Homebrew:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Then paste the following to give Homebrew a little setup magic:
echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile
source ~/.bash_profile
Then use the brew
command to install Python 3.
brew install python3
Do you know what you just did? No? Yeah, you shouldn’t! It’s all magic at this point. Don’t worry, we’ll get there eventually.
Windows only (Babun)
pact
is a package manager for babun, it’s used to install software quickly and easily. We’ll use it to install some prerequisites and then Python 3.
You may need to deactivate Windows Defender/antivirus software for this to work.
pact install python3-setuptools
pact install libxml2-devel libxslt-devel libyaml-devel
pact install python3
Close babun or type exit
.
Because babun is really weird, we now need to do something called “rebasing,” which prevents you from getting strange errors later on. Open up cmd, the Windows Command Prompt, by typing cmd
in the Start Menu search area. Run the following command:
NOTE: BE SURE YOU ARE DOING THIS IN cmd
AND NOT IN BABUN
cd .babun
cd cygwin
cd bin
dash.exe -c '/usr/bin/rebaseall -v'
For some strange reason, when you install things in babun it always gives memory errors unless you run that command. Why? I don’t know, so… every time you use pact install
you gotta run that command.
A lot of things should scroll by when you type that in. If not, open up a brand new cmd
window and try again.
Now close the cmd
window and open up babun. In babun, run the following command:
easy_install-3.4 pip
This installs a friend of Python named pip
.
Step 2: Installing/updating pip
(all platforms)
Next up: let’s get a little familiar with the command known as pip
.
pip
stands for “Pip Installs Packages,” because… pip
is a command to install packages. Packages are little bundles of code that help you do common tasks!
Since we’re using Python 3, we’ll be using the pip3
command for the moment. Try this commands out (no big deal if it doesn’t work):
pip3 install -U pip
Did it yell something about “permissions error” or anything? If it did this one instead (again, no big deal if it doesn’t work):
sudo pip3 install -U pip
Step 2: Installing virtualenv
(all platforms)
If you’re going to the gym, you’re going to dress one way. If you’re going to work, you’re probably going to dress another way. Python is the same way! Not with clothes, though, but with packages.
Let’s say there’s a Python package called BeautifulSoup (which there is!). Maybe in one project you need BeautifulSoup 3 and in one project you need BeautifulSoup 4 - in this step we’re now going to install a tool that helps keep them separate. It’s useful, too - you’ll use BeautifulSoup in just a few weeks.
These separate areas for separate packages (or outfits!) are called virtual environments. To control them we need to install a package called virtualenv
.
Since virtualenv
is a package, we’ll use pip
to install it.
pip3 install virtualenv
Read your results! If you ever get a Permission denied notice when using
pip
, typesudo pip
instead - in this case, your command would besudo pip3 install virtualenv
Set virtualenv
to always look for cool new Python 3 instead of terrible old Python 2.
echo 'export VIRTUALENV_PYTHON=`which python3`' >> ~/.bash_profile
source ~/.bash_profile
Step 2b: Installing virtualenvwrapper
(all platforms)
There are also a few add-ons to virtualenv
that make virtualenv
easier to use. Let’s install one of them called virtualenvwrapper
. It will give us a few friendlier commands.
virtualenvwrapper
is a package, so we’ll use pip3
to install it.
pip3 install virtualenvwrapper
Once it’s done installing (remember the sudo
note up above!), we can configure it.
echo 'export VIRTUALENVWRAPPER_PYTHON=$VIRTUALENV_PYTHON' >> ~/.bash_profile
echo 'export WORKON_HOME=$HOME/.virtualenvs' >> ~/.bash_profile
export VIRTUALENVWRAPPER_SH_PATH=`which virtualenvwrapper.sh`
echo "source $VIRTUALENVWRAPPER_SH_PATH" >> ~/.bash_profile
source ~/.bash_profile
Testing it out (all platforms)
Let’s test this out with an example.
Say we’re going to make a project about schools. Let’s try to set up its virtual environment (a.k.a. the Python outfit). We’ll call it schools
because we’re unimaginative.
If we want to work in a virtual environment, we use the workon
command. Try it out:
workon schools
It sure doesn’t work, though! It tells us we need to create the virtual environment before we use it, which totally makes sense. Try to run mkvirtualenv schools
.
mkvirtualenv schools
Now you should have (schools)
next to your prompt. This is your reminder that you’re in a virtual environment! But does it have our good friend Python 3 in it? Run the following command.
python --version
It should say something about being Python 3. Great work!
Maybe our schools project needs to use BeautifulSoup, the package we mentioned before (you don’t need to know what it does yet, just know it sounds interesting). Since BeautifulSoup is a package, we’ll use pip
to install it.
Because we’re in a Python 3 virtual environment, we can just call it
pip
instead ofpip3
. Think about it like outfits again - if you’re in the gym environment and I say “put on your shoes,” you’re going to put on your gym shoes, not your dress shoes.
pip install beautifulsoup4
Does that seem to have worked? Hoorah. Now let’s leave our virtual environment by typing deactivate
.
deactivate
CONGRATULATIONS, you’ve set up Python 3! Practically everything we do this summer will be easier than that process was.