Skip to main content

Create Your first Django Project via Python Virtualenvironment

 Python virtual environment is especially for Python 2 which support is now ended. In Python 3 the virtualenv is called the venv. Today we are going to create our first Django project in Python3 with the virtual environment. The virtual environment helps us separate different versions of libraries and manage different libraries for different projects. It also provides us clean installation for every project. 

Let's create a new project for Django Python 3. Open a command window and navigate to your desired directory for creating your Django Python Projects. In my case, I always prefer the Django-Projects directory under the main Python-Projects Directory in My Documents. 

Create Python venv for Django Project

Now let's create a virtual environment for Python 3 and the Django project which we are just going to create. If you go to the official venv documentation you will find pretty good details about all the required steps to create it. We are just following few steps from the official documentation. All you have to do is to enter the following command in command prompt or Linux Terminal (if you are using the Linux)

python -m venv c:\path\to\myenv

Here the path to the "myenv" is just our current folder which we are currently in. So all we need to do is to create a folder for our project and navigate to that folder. After navigating just type the name of your environment. Like follows, 


>>mkdir DjangoEx01
>>cd DjangoEx01
>>python -m venv DjangoEx01


Activate the new virtual environment

Now that we have successfully created our virtual environment for our Python Django Project, We are ready to activate it. But before activating, let's navigate to the newly created directory with the name of your virtual environment and see what's inside. 

Python venv Scripts folder

Here you can see we now have the files of our own script with pip, pip3, and also a Python.exe, Pythonw.exe. This indicates that a clean installation now resides in our virtual environment and this will not conflict with our globally installed Python as well as libraries. Also, you can create see an activate.bat file that will be used to activate our virtual environment. So let's do that 





C:\Users\Strange Lab\Documents\Python Projects\Django-Projects>mkdir DjangoEx01

C:\Users\Strange Lab\Documents\Python Projects\Django-Projects>cd DjangoEx01

C:\Users\Strange Lab\Documents\Python Projects\Django-Projects\DjangoEx01>python
 -m venv DjangoEx01

C:\Users\Strange Lab\Documents\Python Projects\Django-Projects\DjangoEx01>dir
 

 Directory of C:\Users\Strange Lab\Documents\Python Projects\Django-Projects\Dja
ngoEx01

09/24/2021  07:51 PM    <DIR>          .
09/24/2021  07:51 PM    <DIR>          ..
09/24/2021  07:51 PM    <DIR>          DjangoEx01
               0 File(s)              0 bytes
               3 Dir(s)  21,394,034,688 bytes free


C:\Users\Strange Lab\Documents\Python Projects\Django-Projects\DjangoEx01>cd Dja
ngoEx01

 
C:\Users\Strange Lab\Documents\Python Projects\Django-Projects\DjangoEx01\Django
Ex01>dir
 

 Directory of C:\Users\Strange Lab\Documents\Python Projects\Django-Projects\Dja
ngoEx01\DjangoEx01

09/24/2021  07:51 PM    <DIR>          .
09/24/2021  07:51 PM    <DIR>          ..
09/24/2021  07:51 PM    <DIR>          Include
09/24/2021  07:51 PM    <DIR>          Lib
09/24/2021  07:51 PM               123 pyvenv.cfg
09/24/2021  07:52 PM    <DIR>          Scripts
               1 File(s)            123 bytes
               5 Dir(s)  21,393,821,696 bytes free

C:\Users\Strange Lab\Documents\Python Projects\Django-Projects\DjangoEx01\Django
Ex01>cd Scripts

C:\Users\Strange Lab\Documents\Python Projects\Django-Projects\DjangoEx01\Django
Ex01\Scripts>dir
 Volume in drive C has no label.
 Volume Serial Number is A4A0-91BD

 Directory of C:\Users\Strange Lab\Documents\Python Projects\Django-Projects\Dja
ngoEx01\DjangoEx01\Scripts

09/24/2021  07:52 PM    <DIR>          .
09/24/2021  07:52 PM    <DIR>          ..
09/24/2021  07:51 PM             2,352 activate
09/24/2021  07:51 PM             1,087 activate.bat
09/24/2021  07:51 PM             1,558 Activate.ps1
09/24/2021  07:51 PM               368 deactivate.bat
09/24/2021  07:52 PM           102,844 easy_install-3.7.exe
09/24/2021  07:52 PM           102,844 easy_install.exe
09/24/2021  07:52 PM           102,826 pip.exe
09/24/2021  07:52 PM           102,826 pip3.7.exe
09/24/2021  07:52 PM           102,826 pip3.exe
09/24/2021  07:51 PM           522,768 python.exe
09/24/2021  07:51 PM           522,256 pythonw.exe
              11 File(s)      1,564,555 bytes
               2 Dir(s)  21,393,821,696 bytes free

C:\Users\Strange Lab\Documents\Python Projects\Django-Projects\DjangoEx01\Django
Ex01\Scripts>cd..

C:\Users\Strange Lab\Documents\Python Projects\Django-Projects\DjangoEx01\Django
Ex01>cd..

C:\Users\Strange Lab\Documents\Python Projects\Django-Projects\DjangoEx01>.\Djan
goEx01\Scripts\activate

(DjangoEx01) C:\Users\Strange Lab\Documents\Python Projects\Django-Projects\Djan
goEx01>


Here you can see all my console logs and finally our virtual environment is now active. 


Create Python Django Project

Now that our virtual environment is ready and activated, we are good to install the Django Framework along with other required libraries. 

pip install django 

once hit the above command, it will do all the trick and will install the Django framework inside your virtual environment. 

You may check your Django version by hitting the following command in your Terminal 

...\> py -m django --version
This will display the Python Django verison which in my case is 3.2.7

Django Create Project Command 

Now that we want to create a new project for our Django Python framework we can use the following command to create a new project with the Python Django framework

 django-admin startproject mysite

Now we are done creating our new Django project with the name "mysite". You may name it whatever you like. This project will be created with the following files and directory structure

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py


Running Python Django Project

Now we are ready to finally run our default project which we just created. You may run this manage.py file which is the framework created for you to do such tasks automation. 

>>cd mysite
>>py manage.py runserver

this command will load the server on the URL of http://localhost:8000

The default port number of Django project is 8000

finally to close the server just hit CTRL+C from the keyboard and to deactivate the venv just use the following command 

>>deactivate 







Comments

Popular posts from this blog

Laravel Auth Tutorial

 After creating your project with the composer in Laravel, mostly next step is to enable the authentication. It is very simple to enable the Laravel auth with PHP artisan command. The most simple commands to follow is as below Adding SQLite Database to Laravel  First of all, you have to add the database into the " .env"  file. The simplest database that you could use is the SQLite database with Laravel. You can create a simple SQLite file in the database folder and add the absolute path in the ".env" file.  >> touch database\database.sqlite The above command is for creating the database file. You may use create the file manually if you are using windows.  You can change the .env file like this.  DB_CONNECTION=sqlite DB_DATABASE=/Applications/MAMP/htdocs/rtest/database/database.sqlite Enable Auth Middleware  The Larvel Authentication documentation  explains it very well. According to that documentation, you need to use the Laravel Auth...