How to Create Django project in a Virtual Environment
May 17, 2021 2022-12-19 7:26How to Create Django project in a Virtual Environment
How to Create Django project in a Virtual Environment
How to Create Django Project in a virtual environment
As we discussed in previous Django tutorials about virtual environment its introduction and why need a virtual environment for the Django project. We will learn different topics/concepts related to Django from time to time. Now we are learning how to create Django project in a virtual environment. Because in a virtual environment your Django project will be safe when a new update release and your project will not be affected. In this Django tutorial we also explaining its importance and method using that you can create Django project in your separate virtualenv properly.
What is a Virtual Environment?
It is the separate Python environment in which you work on your new project or existing project in python that makes it easy for you. If we explain it more, it is the distinct environment in which your python project did not relate with another project. It means each Python project in your system has its own path. Due to their own path, each project will need separate libraries that they need.
How to Create Django Project in virtualenv
You have learned how you can build your first project in Django in previous tutorials. It is a very simple and easy way to do it. First of all, you have to Python installed in your system then install Django, after the installation of Django you can build a project.
Open a terminal
First of all, you have to open terminal (CMD) or visual studio code in which you write the following command.
django-admin startproject your_project_name
This command will create a project named ‘your_project_name‘
django-admin startproject test
Here, your project name will be ‘test’
Install the virtualenv package.
For this, you have to run the following command, 1.7.1.2 is the version of virtualenv you are using.
pip install virtualenv==1.7.1.2
Create the virtual environment.
Now, you need to create virtual environment using this command when you run this command a separate Python environment will be created for you. In which you will develop a Python-based project that will be separate from your global python installed environment.
virtualenv mypython
Activate the virtual environment.
After creating a virtual environment, by default, your global python will be activated and remain. To move the virtual environment as you recently activate you have to activate that environment. As Python commands depends upon your system operating system, that is why, i shared two commands for Mac, Linux, and for Windows users for activating the VE.
For Mac OS / Linux
source mypython/bin/activate
For WindowÂ
mypthon\Scripts\activate
Deactivation
To deactivate the VE you have to write simply deactivate, it will deactivate the environment in which you were in.
deactivateÂ
Install DjangoÂ
After activating the virtual environment, now you need to install the Django web framework in your new virtual environment. After installing Django your Django project is now separated from global python installed. You can create a simple or complex kind of website or web application that will be separated and when you live this web application then it will be safe from new updates affections.
pip install Django
Run the Django server
After doing the basic setting you can run your Django project. It is a very simple command you have to run.
For running Django project you have to use the following command:
python manage.py runserver
We have published tutorials in which you can learn how you can rin your Django project.
Conclusion
I sure, You have learned what I shared with you. We discussed how to Create Django project in a virtual environment, if you have any kind of problems, issues, or questions you can send me a query via email, phone number or mention your question in the command box. We will provide you best solution.
If you use Facebook, Twitter, Pinterest, LinkedIn, or other social media platforms, then share this Django tutorial, if you share, more students will take the chance to learn.
Thanks in advance.