Install Django, Python3, pip, virtualenv, and virtualenvwrapper
Setting up your computer for web development is tricky, even for a professional programmer. As a beginner, it’s likely you’ve had trouble along the way.
This guide will help you properly install Django, Python 3, pip, virtualenv, and virtualenvwrapper on an Apple computer.
1. Install Xcode
Xcode is pretty much mandatory for doing development work on an Apple computer. To install Xcode click on this link https://itunes.apple.com/us/app/xcode/id497799835.
As part of the install process Xcode will ask you to login with your Apple ID and agree to the Xcode release agreement.
Make sure to install the Command Line Tools, too, which for some reason Xcode does not install by default. Open your Terminal application (Finder -> Applications -> Utilities -> Terminal, type the following and hit Return:
This will trigger a pop-up window asking you to install the Command Line Tools. Do so.
2. Install Homebrew
Homebrew is a package manager: it helps us install new software correctly. Be grateful if you haven’t experienced this problem firsthand before.
To install Homebrew copy and paste the following into Terminal and hit Return:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Now we need to update our $PATH environment variable so our computer knows where to find software we’ve downloaded via Homebrew. For example, let’s say I download two different versions of Python and store the code in different folders. When I type “python” on the command line, how does the computer know which version of Python I want to use? The $PATH environment variable is where we can explicitly tell the computer where to look.
Type the following into your Terminal and hit Enter:
We’re using nano, which is a built-in text editor, to open the file call “.bash_profile.” Files that start with a period are known as system files and are usually not displayed when using Spotlight or Finder.
Nano is a text-only editor so your mouse will not work here. Use the up/down and left/right arrows on your keyboard instead to navigate. At the top of the file create a new line and type the following:
We’re telling the computer to look in the directory /usr/local/bin/ for software packages. This is where Homebrew installs things.
Now hold down Control and the “X” key to Exit and say Yes to saving the file.
For the changes to go into effect we need to close Terminal completely (Command + Q) and then open it again (Finder -> Applications -> Utilities -> Terminal).
In our new Terminal window type the following and Return to confirm our changes:
You should see /usr/local/bin: at the beginning of a long string of paths. Don’t worry about the rest of them.
3. Install Python3
Every Apple computer, by default, comes installed with a version of Python 2. To confirm this, type the following:
But we want to use Python 3, which is newer and better. To do so, using Homebrew, type the following:
brew install python3
To confirm Python3 is installed, type:
If you want to execute a command using Python 2 you just type “python” and for Python 3 type “python3.”
4. Install virtualenvwrapper
You should always use a virtual environment for individual Python projects. This way you can run one project with, say Python 2 and Django 1.7.0, and a separate project with Python 3 and Django 1.9.5. Because each project is contained within its own virtual environment, we don’t have to worry about conflicts between different software versions.
pip3 install virtualenv virtualenvwrapper
Note: You may see a warning in your Terminal about updating pip.
You are using pip version 8.0.2, however version 8.1.2 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
If so, go ahead and update pip to make this go away. Type the following into Terminal and hit Return:
pip install --upgrade pip
Next we need to create a folder for all of our virtual environments. Again in Terminal type and hit Return:
Now open your .bash_profile file again by typing the following:
Add three new lines to the file below our previous export PATH=/usr/local/bin:$PATH text:
export PATH=/usr/local/bin:$PATH export WORKON_HOME=~/.virtualenvs VIRTUALENVWRAPPER_PYTHON='/usr/local/bin/python3' source /usr/local/bin/virtualenvwrapper.sh
To save the file type Ctrl + X, type Y for Yes, and then hit Return for the actual save. This will exit Nano and take you back to your main Terminal view.
The first line tells virtualenvwrapper where to store the future virtualenvs that will be created. In our case, we want them in the .virtualenvs folder we just made at ~/.virtualenvs. The second line runs a script to make the updates permanent.
Quit out of Terminal (Command + Q) and then open it again for the changes to your ~/.bash_profile file to occur.
5. Create a virtual environment
To create our first virtual environment, which we’ll call “myvirtualenv” type the following in Terminal:
mkvirtualenv --python=/usr/local/bin/python3 myvirtualenv
This creates a folder called “myvirtualenv” in our virtual environments folder, ~/.virtualenvs. The installation command means it will be up and running. You’ll likely see “myvirtualenv” in parentheses at the start of each line in Terminal.
To deactivate the environment type:
The parentheses go away, showing you’re no longer in a virtual environment.
To startup your existing environment again, just type within Terminal:
6. Install Django
Finally let’s install Django with pip. Making sure we are already within our “myvirtualenv” environment, type the following in Terminal:
pip3 install Django
To confirm it’s installed correctly type:
You should see the newest version of Django:
You’re done! Setting up a proper development environment is one of the hardest and least enjoyable parts of programming.
Going forward, whenever you create new Django projects you simply need to create a new virtual environment, install a fresh version of Django, and away you go!
Check out Django for Beginners, a free online book on how to create and deploy multiple Django applications. Starting with a simple “Hello, World” application it progresses through multiple web applications of increasing complexity showing Django best practices along the way.