How to install Python 3, Django and create a virtual environment on macOS
In this short chapter we will learn how to install Python 3, create virtual environments, and install Django in a macOS. By default macOS comes with inbuilt python 2.7 version. Python 3 is the most current version of the language and is considered to be the future of Python.
Step 1 — The Command Line (Terminal)
The command line is a powerful tool that we will use to do the installation.
On a Mac computer, it can be accessed via a program called Terminal that is located in the
Enter the following command in Terminal to confirm the existing python version in your computer
$ python --version
Expected Output is - python 2.7.13
To check if Python 3 is already installed, type the following command in Terminal
$ python3 --version
Expected Output is — python 3.6.2
If you get the above outputs means your computer have python3 installed and you can directly move forward to install virtual environment. However most likely you’ll see an error message since we need to install Python 3 directly.
Step 2 — Xcode Package
Xcode is an IDE that is comprised of software development tools for macOS and iOS. You may have Xcode installed already. Type the following in your Terminal. This will take some time.
$ xcode-select — install
Step 3 — Homebrew
Homebrew is a package manager. Homebrew installs the stuff you need that Apple didn’t. To install Homebrew, type the following into your Terminal window.
$ /usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
To confirm Homebrew installation type the following command in terminal.
$ brew doctor
Expected output — Your system is ready to brew.
Sometimes you get minor warning that can be ignored for time being.
Step 4 — Python 3 installation
Type the following in Terminal to install Python 3
$ brew install python3
After installation type the following to confirm the python version.
$ python3 --V
Expected output is - Python 3.6.2
Now you access python 2 by the command
python and python 3 by the command
when you install python with Homebrew, you also install pip3 and Setuptools. To confirm this type the following command in terminal
Expected output is - pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6)
Step 5 — Creating a Virtual Environment.
Virtual environments enable you to have an isolated space on our computer for Python projects, ensuring that each of your projects can have its own set of dependencies that won’t disrupt any of your other projects.
Create and navigate to folder that will contain your virtual environment and project.
$ mkdir Desktop/myproject will create a folder for your project.
$ cd mkdir Desktop/myproject will navigate to that project.
Following command will create a virtual environment for your project “myproject_env” can be replaced by your name of choice.
$ python3 -m venv myproject_env
After creating the virtual environment we have to activate it by the following command.
$ source myproject_env/bin/activate
Because we used python3 when creating this environment, the default for Python is now Python 3. You can confirm this by typing
python -V and it will show a Python 3 result. However outside of a virtual environment, python will result in Python 2 and python3 results in Python 3.
To stop using a virtual environment, either close the command-line console or enter
deactivate in the Terminal.
Step 6 — Installing Django
After activating the virtual environment, enter the following command to install Django.
(myproject_env) $pip install django
Hopefully you will get this line of message in the end of the log.
Successfully installed django-1.11.5 pytz-2017.2
Now, you’re ready to code!