Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtual Environment for Python Django

I'm currently a novice in web programming. I've been working on this Django project lately, and I've been reading about virtual environments. At the start of my project, I was unable to set up a virtual environment, and so I proceeded with the project without it. My questions are

Whether or not this virtual environment is really necessary?

If I want to make more Django projects in the future, will I need this virtual environment to differentiate the projects since right now I'm running all the commands in the command prompt from my main C: directory?

Does this virtual environment differentiate multiple projects or does it separate each project with respect to the version of Django/Python it's coded with or both? I'm wondering because I currently input commands such as python manage.py runserver (without the virtual environment) in my main C:drive directory. So does that mean I can't do multiple projects at once without a virtual environment for each? Can I still work on multiple projects without a virtual environment? (I've been confused about this especially)

Should I just try to set up a virtual environment for my next project or can I still do it for this current one (I'm halfway through the project already, I've already made models, views, templates, etc.)?

Any answers to clarify my confusion is greatly appreciated!

like image 998
Steve D. Avatar asked Dec 22 '16 09:12

Steve D.


People also ask

Do I need a virtual environment for Django?

Before installing Django, it's recommended to install Virtualenv that creates new isolated environments to isolates your Python files on a per-project basis. This will ensure that any changes made to your website won't affect other websites you're developing.

Which environment is best for Django?

As a result, experienced Python/Django developers typically run Python apps within independent Python virtual environments. This enables multiple different Django environments on a single computer. The Django developer team itself recommends that you use Python virtual environments!


2 Answers

Without virtual environments, all your projects will use the same installed packages.

When you want to move a project to a server when it's done, you don't know which packages are needed for this project, so your only option is to also install all of those packages there. It will quickly become a long list and many of the packages won't be necessary for that particular project.

When using a virtual environment, you have a set of installed packages for each project, and they don't mix. Much nicer.

You can start using virtual env now. In your project directory, do:

pip install virtualenv  

Now you have the virtualenv command (for all projects).

virtualenv env

Now you have a directory "env" in your project directory that will contain this project's virtualenv.

env\Scripts\activate

Now you are using this virtualenv (your prompt changed to reflect that).

pip install django

Installs Django only for this project.

pip freeze

Shows you which packages are installed, now only for this project.

pip freeze > requirements.txt

Creates a requirements.txt that you can use to remember which packages need installing, and as input for

pip install -r requirements.txt

That installs them. And that's more or less all you need.

like image 200
RemcoGerlich Avatar answered Sep 28 '22 04:09

RemcoGerlich


Well, this is one of the most common questions among beginners. I, myself have faced the question and did build multiple projects without worrying about the virtual environment.

But, of late, I have realized the importance of using virtual environments. Some of the benefits of using virtual environments are:

  1. Dependency Management: Prevent conflicts between dependencies of multiple projects.
  2. Ease of installation and setting up new project on different machines: Store your dependencies in requirements.txt file and run pip install -r requirements.txt to install the dependencies wherever you want.
like image 22
Pulkit Pahwa Avatar answered Sep 28 '22 06:09

Pulkit Pahwa