Install Python3, Virtualenv, Django and Start a new project on your MacOS
Django is a Python-based free and open-source web framework. It is a powerful high-level framework. Its fast, secure, versatile and very scalable.
In this article we are going to learn how to install Python 3, virtualenv, create virtual environments, and install Django and fianally creae a fresh project in macOS.
Overview of steps:
- Install python3
- Install pip
- Install VirtualEnvironment
- cd to project directory and create virtualenv
- activate virtualenv
- Install django
- Create your project
Install Python3
- Go to https://www.python.org/downloads/ and downlaod a copy of python 3. Your browser will automatically detect if your are using macOS. The Mac installer is in
.pkg
format, so once it’s downloaded, click the file to run the package installer - After successful installation python3, open a new terminal and type
python3
to make sure python3 has been installed properly. useexit()
command to exit from python3 shell.
Install Pip
Pip is the package management system used to install and manage software packages written in Python. And the good news is
- Python3 package consists of pip with it so we do not need to install pip separately.
Install virtualenv
Virtual Environments, keep project dependencies mostly isolated from one another. This helps us to have multiple python projects with completely different package dependencies on the same machine. So, It is recommend that each project you create, you should use a different virtual envionrment.
To install virtual environment with pip, type this command in your terminal.
sudo pip3 install virtualenv
Create Virtualenv
*** We have already installed all required global packages (python3, pip, virtualenv) now its time to work for a specific project.
So here, we will create a folder for a project and create virtual environemnt for the project.
Create Project Directory
On your terminal, cd to desired directory and create new directory for your project.
mkdir blog_project
You can give the folder any name that makes sense to you.
Create Virtualenv
Go to your blog_project directory and create virtualenv with python 3 —
virtualenv env_blog -p python3 // Here env_blog is the virtualenv name
Here, env_blog is my virtual environement name. Now you will find with named env_blog
underblog_project
folder. If you open the folder, you will see the following:
on mac, we will see
/bin
/Include
/Lib
pyvenv.cfg
Activiate Vitualenv
After successfully creating our ‘env_blog’ we need to activate it to install our required projetc packages. To activate ‘env_blog’, Go to project directory (Here the directory is blog_project) type the following in your terminal —
source env_blog/bin/activate // Here env_blog environment name
This will run the activate script inside your virtual environment’s folder. You will notice your command prompt has now changed:
(env_blog) ... <yourusername>$
The (env_blog)
at the beginning of the command prompt lets you know that you are running in the virtual environment.
Since, we created env_blog with python3 specifically we can now use just python instead of python3 as well as just pip instead of pip3.
Deactivate Virtual Environment
To stop using a virtual environment, either close the command-line console or enter deactivate
in the Terminal.
Install Django
Go to blog_project directory and activate env_blog (if deactivated) and use pip to install django.
pip install django
This will instruct pip
to install the latest version of Django into your virtual environment.
You can also check if Django has been installed from the command using the following command.
python -m django --version
Create Project.
I am assuming at this stage you are still running the virtual environment from the previous installation step. If not, go to blog_project directory and activate again. Now run the following command:
django-admin startproject blog
This command will automatically create a blog
folder in your blog_project folder, Here, our project name is blog.
Your directory structure would be something like-
blog_project/ ----- (level1)
|-- blog/ ---- (level2)
| |-- blog/ ---- (level3)
| | |-- __init__.py
| | |-- settings.py
| | |-- urls.py
| | |-- wsgi.py
| +-- manage.py
+-- env_blog/
where:
blog_project (level1) is the home directory of your project
blog (level2) is the django project directory
blog (level3) is the root app of the project
env_blog is the virtual env_blog containing our blog(level2)
Run Your Project
Django includes several applications by default (e.g., the admin program and user management and authentication). Some of these applications makes use of at least one database table, so we need to create tables in the project database before we can use them. To do this, run the following command (Make sure you are in blog
folder)
python manage.py migrate
Now Its time to start the project server by typing the following command. Make sure you are in blog
folder.
python manage.py runserver
This will start the Django development server — a lightweight Web server written in Python. The development server was created so you can develop things rapidly, without having to deal with configuring a production server until you’re ready for deployment
When the server starts, Django will output a few messages before telling you that the development server is up and running at http://127.0.0.1:8000/
. If you were wondering, 127.0.0.1 is the IP address for local host, or your local computer. The 8000 on the end is telling you that Django is listening at port 8000 on your local host.
You can change the port number if you want to, but I have never found a good reason to change it, so best to keep it simple and leave it at the default.
Now that the server is running, visit http://127.0.0.1:8000/
with your web browser. You’ll see Django’s default welcome page, complete with a cool animated rocket.
Congratulations! You have just ran your first python-django project. Be sure to give claps if you find his article helpful. :)