To install Django on Mac, install the virtual environment package virtualenv to isolate the Django project and then install the Django framework using pip command.
Follow the below steps.
- If you have not installed Python 3, then please install Python 3.
- Install pip or upgrade pip.
- Install the virtual environment package virtualenv to isolate the Django project.
- Install the Django framework using pip.
We often find tutorials for separate technologies but not precise, compact forms. This article aggregates the basic setup to start a Python-Django project on macOS X.
Here we will create a virtual environment on your system containing your project with python3.6 and django2.0. Well, we will install it from scratch. So first, I will show you how to install Python on Mac, install virtualenv, and install Django on that virtual environment.
Step 1: Install Python on Mac.
Python is already installed on a new Mac, but its version is old. So what we do is install the latest versionPythonthon using homebrew.
brew install python3
After installing, you will type the following command to check the version. First, type python3 in your terminal. Since MacOS X is already Python installed, we cannot uninstall the old version because otherwise, the Python 2-supported programs will be stopped, and we need to reinstall the mac. So we need to type python3 instead of Python.
python3
It will display the version, and now we can execute the python code. I am using iTerm2, so your terminal may be different, but it does not matter with any commands.
Okay, so we have successfully installed python 3. To get out from the python console, we need to type exit().
Step 2: Install the Pip package management system.
Pip is the package management system used to install and manage software packages writtenPythonthon. Now, install it using the following command.
sudo easy_install pip
Step 3: Install virtualenv Pythonthon.
Type this command in your terminal.
sudo pip install virtualenv
After installing, we need to create a virtual environment. Now you will create this folder while putting in your other projects. Mine is desktop >> code folder. So navigate to that and type the following command. In my case, My virtualenv folder name is thanos.
virtualenv thanos
It will install the required folders. Now, go into that folder.
cd thanos
Activate the virtual environment by typing the following command. First, please Make sure you are in the virtual environment directory.
source bin/activate
You will see what your terminal looks like below.
Our virtual environment has started. Now is the time to install the Django Framework.
Step 4: Install Django On Mac
Now stay in that folder and install Django using the following command.
The latest official version is 3.0.1. So first, read the 3.0.1 release notes, then install it with pip:
sudo pip install django==3.0.1
It will download the Django project. Check the Django version using the following command.
python -m django --version
In our case, we have explicitly defined that we are installing 3.0.1. So it will display that version.
Step 5: Create the Django Project.
We need to install the skeleton of the Django project. Type the following command.
django-admin startproject thanosback
Here, our project name is thanosback. So, next, go into that folder.
cd thanosback
Finally, we have created the project. Now start the project server by typing the following command.
python manage.py runserver
It has started the development server, and we can access it via localhost:8000
Navigate to the browser, and you will see a screen like this.
Step 6: Open Django on Visual Studio Code.
If you have not installed Visual Studio Code on Mac, please check out my tutorial below.
Related Post: How To Install Visual Studio Code On Mac
This step is optional because if you are using Visual Studio Code, then it will be helpful to you.
You can skip this step using another IDE like pycharm or an editor like sublime text. But first, perform the following step.
Install the Python extension.
Now, open our project using the following command in the visual studio code.
code .
Please make sure you are at the root of the thanosback folder.
You will get the folder structure like this.
Get the latest development Django version.
The latest and most excellent Django version is the one that’s in our Git repository (our revision-control system).
This is only for experienced users who want to try incoming changes and help identify bugs before an official release. Get it using this shell command, which requires Git:
That’s it for this tutorial.
hello
python manage.py migrate is not working
(myproject) Teen-MacBook-Air:myproject chok$ python manage.py runserver
python: can’t open file ‘manage.py’: [Errno 2] No such file or directory
are you in you project folder
you can use “ls” to check
always execute with python3 if you has been install it with that version. By default, Mac always use the old version (2.x)
You saved my day, thank you so much!!
Thanks man!!
Hi, Very helpful indeed.
Can you help me in setting up MSSQL Connection from Python 2.7, Django 1.8 using Virtual Env?
I have tried whatever I could find on internet with no help.
I’m confused. You install python3 but then use “python” and “pip”. When I used “pip install django=2.03”
, it wanted to install django v1 for python 2. I had to use “python3” and “pip3”. Did I miss something about getting “python” to actually invoke “python3”?
I have installed Python3 (machine already has 2.7).
Thanks for your work, but I got a problem.
When I do “sudo easy_install pip” it returns
“Processing pip-9.0.2-py2.7.egg”
This seems to be not what I want.
How do I make it use Python3 ?
When you install virtualenv, make sure you use `pip3` or else it would be using the mac default python 2.7.
$ pip3 install virtualenv
When you use the virtualenv command, it would use python3. Note: you might have to uninstall and reinstall, in case you previously installed virtualenv with v.2.7
Traceback (most recent call last):
File “manage.py”, line 14, in
import django
ModuleNotFoundError: No module named ‘django’
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “manage.py”, line 17, in
“Couldn’t import Django. Are you sure it’s installed and ”
ImportError: Couldn’t import Django. Are you sure it’s installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
I am getting this error
File “manage.py”, line 16
) from exc
^
SyntaxError: invalid syntax
(Django) Admins-MacBook-Pro:button apple$ python manage.py runserver
File “manage.py”, line 16
) from exc
^
SyntaxError: invalid syntax
you are mixing up with preinstalled Python (2) and Python3.
Command should be updated described here https://stackoverflow.com/questions/38938205/how-to-override-the-pip-command-to-python3-x-instead-of-python2-7/55494352#55494352
using “virtualenv”, how would you install different versions of python on a single system?
you can’t. if your projects require different versions of python, only “pyenv” can provide that.
Thank youuuu!!!!!! it was really helpful
Thanks!! Keep Sharing.
Breaks soon! “sudo easy_install pip” gives “invalid syntax error”. Three days spent at trying to install pip and then django, after successfully importing Python 3.8 end then 3.9. I fear there will be a fourth one!
Why is it so difficult to work with the terminal??? Why? Why? Why? Always tons of “…not found”. Apple and Python are so “wonderfull” and so “user dedicated” that they should provide “Download, click and use.”
Thank-you for your concise tutorial. I had ruined a django install by installing Pihole as part of a larger server app. I couldn’t repair my django installation after the server crashed until I followed this tutorial exactly.
I think that I will recommend you most highly and look forward to your future posts.