A while back I had the opportunity to purchase and set up a new MacMini for my old office. I bought the computer specifically to be used in maintaining a Python/Django-Mako-Plus site (a great framework built by doconix, if you like Django, you should consider giving it a try), and for developing an iOS app I had built.
As I started the set-up process, I realized that it would make a great opportunity to gather all of the various tutorials and StackOverflow articles that I reference in setting up a Mac for Python development into one place: a single blog post. So, without further ado, here is a step-by-step guide in getting a Mac running El Capitan (or Yosemite) set up for Python dev.
Note: If you’re (for whatever reason) running Mavericks still, consider this great tut from doconix himself.
Before you get started…
If you’re running El Capitan (which, given the name of this article, I’m assuming you are), make sure System Integrity Protection is disabled. If you’re not sure what this is, or don’t know how to check, see my other post on Disabling SIP.
Setting Up Python and Pip
First, install the most recent version of Python (at the time of this writing, it’s 3.5.0). Head off to the Python’s site to download it there.
Once installed, you can create symlinks in your path by linking the newest version of python’s binaries. This means that when you’re working in command line, you’ll be able to write “python” rather than “python3”. To do this, enter the following commands in your terminal:
A note about all the commands in the tutorial: Make sure you don’t just copy and paste the commands I have listed here without first checking them for “variables”! By that I mean that I substitute in certain things for version numbers or any other inconsistencies between machines. For example, you’ll see in the commands below PYTHON_VERSION_NUMBER. Read the note following each command set to see what to insert in the place of these variables.
sudo ln -sf /Library/Frameworks/Python.framework/Versions/PYTHON_VERSION_NUMBER/bin/pythonPYTHON_VERSION_NUMBER /usr/bin/python
sudo ln -sf /Library/Frameworks/Python.framework/Versions/PYTHON_VERSION_NUMBER/bin/pythonwPYTHON_VERSION_NUMBER /usr/bin/pythonw
sudo ln -sf /Library/Frameworks/Python.framework/Versions/PYTHON_VERSION_NUMBER/bin/pydocPYTHON_VERSION_NUMBER /usr/bin/pydoc
sudo ln -sf /Library/Frameworks/Python.framework/Versions/PYTHON_VERSION_NUMBER/bin/pythonPYTHON_VERSION_NUMBER-config /usr/bin/python-config
Where PYTHON_VERSION_NUMBER refers to the number of the python version installed previously, like 3.4 or 3.5.
Now you’ll need to upgrade your version of pip, the python package manager. To do this, run the following command:
pip3 install --upgrade pip
Once upgraded, now you can create a symlink for pip3 in your path like you did for python3, for convenience (because it’s so much easier to write “pip” than “pip3″…). Note the PYTHON_VERSION_NUMBER variable in this command as well.
sudo ln -sf /Library/Frameworks/Python.framework/Versions/PYTHON_VERSION_NUMBER/bin/pipPYTHON_VERSION_NUMBER /usr/bin/pip
Get Your Virtual Environment Set Up (Optional, but Recommended)
This section of the setup, though highly recommended, is optional. Virtual environments are a really great way to keep your machine clean and organized (concerning libraries/modules downloaded), and the most effective way to be able to run multiple python projects with different package version needs on one computer.
Think of virtual environments as a container for python libraries. You set up and “enter” your virtual environment (venv), then download all packages and modules that are needed for your respective project. When you’re done with your project, you’ll “exit” your virtual environment and be back to your main libraries. So, as an example, if you were to pip-install Pillow V2.6 in your venv for a given project, then exit your venv, you won’t have access to import Pillow outside of it. Once you “enter” the venv again, you’ll be able to freely import and use Pillow.
Note: If you’re interested in using virtual environments, you will need to have a project directory already planned for whatever project you’ll be working on.
So, to get started, pip-install the virtual environment package
Note: From here on out, this tutorial will refer to pip3 (or 3.4, or whatever version you set in the previous step) as pip, assuming that you have set the symlink. If you haven’t, you should substitute “pip3” for “pip” throughout the rest of this walkthrough.
pip install virtualenv
Next, navigate from the terminal into the project directory where you’ll be working, and create your virtual environment. An important step here is to include the path of the Python interpreter that you would like to use. In the previous section, you downloaded the most recent version of Python and created a symlink to that version, therefore you should include the path to that interpreter (if you followed those steps, it should be “python”).
virtualenv -p /usr/bin/python NAME_OF_VIRTUAL_ENVIRONMENT
Note: If you run into an error here that says something like virtualenv isn’t found in your path, you’ll need to add the following lines to your .bash_profile file (hidden file in your home directory). You can think of this file as a “config” file for your terminal. You can store aliases that you’d like to use, add things to your path, or anything else that you’d like to have preconfigured in bash every time you open your terminal.
This file is hidden, so you can’t normally see it in Finder. Check to make sure you’ve got one by opening up terminal and listing all files in your home directory. If you don’t have a .bash_profile file for whatever reason, created one by executing the following:
Note: You can use whichever editor you’d prefer here. You don’t have to use vi, which might be a little intimidating for first-time users. Nano might be a better starter.
Once open, you’ll want to enter in the following two lines in your file:
Note the two variables in the previous commands: You’ll need to replace the first with the path to your project directory, and the second with whatever name you want for the virtual environment. Feel free to choose whatever here, but you should name it something that has to do with your project…
Now that your venv is set up, you’ll need to activate it before moving on. To do so, use the following command:
You’ll know that your virtual environment is up and running at this point if you see name of your venv on the left-hand side of the command prompt (e.g., (NAME_OF_VIRTUAL_ENVIRONMENT) YOUR-COMPUTER:…)
** Just as an FYI, when you’re done using your venv, make sure you exit it by executing the following command:
** As a final note: depending on how you have named your file structure, you might run into a problem pip-installing anything with your virtual environment activated. If you install your virtual environment in a directory (or through a path) that contains spaces in the folder names, you won’t be able to execute pip. You’ll need to change your folder names to not include spaces, or move your project directory somewhere else.
Setting Up PostgreSQL
Now that python and pip are installed, it’s time to set up your database. I’m a big fan on Postgres as my database for django(-mako-plus) development, so here are the steps to getting your machine rolling with PostgreSQL.
First, head out to PostgreSQL’s website to download and install Postgres. While going through the installer, you’ll create the user “postgres” on your machine. REMEMBER the password that you create for this user. \
Once installed, create the needed symlinks for PostgreSQL (PG). Note the new variable here in these commands, POSTGRES_VERSION_NUMBER.
sudo ln -s /Library/PostgreSQL/POSTGRES_VERSION_NUMBER/bin/psql /usr/local/bin/psql
sudo ln -s /Library/PostgreSQL/POSTGRES_VERSION_NUMBER/bin/pg_restore /usr/local/bin/pg_restore
sudo ln -s /Library/PostgreSQL/POSTGRES_VERSION_NUMBER/bin/pg_backup /usr/local/bin/pg_backup
sudo ln -s /Library/PostgreSQL/POSTGRES_VERSION_NUMBER/bin/pg_config /usr/local/bin/pg_config
sudo ln -s /Library/PostgreSQL/POSTGRES_VERSION_NUMBER/bin/createdb /usr/local/bin/createdb
sudo ln -s /Library/PostgreSQL/POSTGRES_VERSION_NUMBER/bin/pg_dump /usr/local/bin/pg_dump
Now test to make sure you can get into the PG console by typing ‘psql’ into the console. This should open the PG console.
** If you want to create a PG account for your OS X user account, follow these steps (you’ll create a DB for this user as well):
psql -U postgres
create user MAC_ACCOUNT_NAME with password ‘CHOOSE_PASSWORD';
create database MAC_ACCOUNT_NAME;
grant all privileges on database MAC_ACCOUNT_NAME to MAC_ACCOUNT_NAME;
Note the variables in the previous commands!
** If you’d like to make this user a superuser in PG:
alter user MAC_ACCOUNT_NAME with superuser
To exit out of the PG console:
Just as a note, if you’d like to change the password for the postgres user (or any other user for that matter):
sudo -u postgres psql
ALTER USER postgres PASSWORD ‘NEW_PASSWORD’;
Setting Up Psycopg2
This is the part of the set up that gets a little hairy. For the most part, the setup before this section (and after it) is fairly straight-forward, without many bumps. This section, however, is notorious for giving us grief.
Psycopg2, in a simplified sense, is the connector between PostgreSQL and Python (for more details, check out this post from DBStackExchange). It’s written in C, so it’s super fast. It’s also a little touchy. So, let’s hop right in.
Start by pip-installing psycopg2
sudo pip install psycopg2
There is an outside chance that that’s all you’ll need to do (more than likely not). To test to see if you’re done with this section, test out psycopg2 in the python console:
** To exit the python shell:
If your console throws several errors at this point, specifically an error saying “Image not found”, or something like that, do the following (outside of the python shell):
sudo ln -s /Library/PostgreSQL/POSTGRES_VERSION_NUMBER/lib/libssl.1.0.0.dylib /usr/lib
sudo ln -s /Library/PostgreSQL/POSTGRES_VERSION_NUMBER/lib/libcrypto.1.0.0.dylib /usr/lib
Rerun python and attempt to import psycopg2 again.
Note: I’ve found that using Yosemite or above, coupled with PostgreSQL 9.4 (or greater), you’ll need to do execute two more commands (note the variable in the second command):
sudo mv /usr/lib/libpq.5.dylib /usr/lib/libpq.5.dylib.old
sudo ln -s /Library/PostgreSQL/POSTGRES_VERSION_NUMBER/lib/libpq.5.dylib /usr/lib
If at this point, you still can’t get it to work, then hit Google for your specific problems.
Install Other Python Modules
If you’ve made it this far, take a minute to congratulate yourself! You’re almost done! All that’s left is getting some of the python modules installed and running. Here are a couple of ones that you’ll definitely want to pip-install:
pip install Django
Note, if you want a specific version, such as 1.8.4, do the following instead:
pip install Django==1.8.4
pip install Mako
pip install django-mako-plus
There are also other ways to revert commits, for various other use cases, including reverting back to a specific commit, reverting back a range of commits, cherry-picking different changes made by a certain commit, etc… As you become more and more familiar with git, you can start to do some really great things with your project. If you’re interested in learning more about rolling commits back, check out git’s docs.
Alright, you’re done! Your machine should be up and running now, ready to tackle help you achieve your python dev dreams! If at any point you run into some snags, Google is your friend. If you still can’t seem to find answers, feel free to shoot me an email, and I’ll see if I can’t help you along!
If you’re interested in some additional resources, here are several great reads: