Why do i need to use a virtual envirotment with django?

I'm new to django and i want to know why i need to use a virtual enviroment for django.

The python installed on your system has its set of packages. When you create any python project, as you grow using Python and Django, you will see that it's not necessarily all the packages you wish to use for that project.

A virtual environment would allow you to have a somewhat isolated environment where you can use the Python version you want, the list of packages you want, and install new packages for that environment only.

A time will come when you would be required to list all the packages necessary to run a particular Django project and put these in a "requirements.txt" file using the desired package manager of your choice. A virtual environment would make it easy to track both the packages and their versions. You can read the Whys here:

Virtual Environment is generally about control of versions of different libraries used in your project.

When the only computer is used and the only project is coded, you probably won't deal with versions. But imagine that you have two projects on two computers - one on Django 2.3 and another one - on Django 4. Some of third-party packages will require versions of Django not higher or not lower than the exact version. So if you wanted to switch projects between computers, you would have to reinstall all the libraries on your computers according to needed versions and struggle with some conflicts.

installing libraries inside of a virtual environment solves the problem.

You don't, but it's nice to have. If you plan on working on other python projects or having other people work on it, it'll probably be a good idea to have it..

then to set up the project in any pc all you need is:

virtualenv django
pip install -r requirements.txt
# ^ remember to make a req! it's super nice
# init the db
python manage.py runserver

Super easy!

Back to Top