Virtual Environment for Python Django

Question:

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.)?

Asked By: Steve D.

||

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.

envScriptsactivate

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.

Answered By: 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.
Answered By: Pulkit Pahwa

In java all libs used can be packed into a war or jar file. The advantage is that you don’t need to worry about the environments of the OS.

Python is a pure dynamic language. Without virtual environment, all the python libs need to be installed into system path and shared among all python project.

Imagine that you are developing a django 1.10 project. You find a demo project. You want to run it on your machine. But it is compatible only with django 1.8. You can not install two version of the same lib in the same machine, so you get stuck.

Virtual environment solves problem like this.

But of course virtual environment is not perfect. There are python libs like mysql-python which depends on libmysqld. If those libs are used in your project, it cannot be totally independent with the settings in OS. The best practice I think is to use virtual machine combined with docker. IDE like pycharm supports running remotely via docker

Answered By: bresai

Virtual environment creates virtual installation of python and packages on your computer .
Say,if you have your web application .With pass of time the packages get updated and there are changes that sometimes break backwards compaatibility that your web application or web project may depend on
so what do you do if you want to test out the new features of a package update but you also don’t want to break your web application.
After all you can’t just take down your web site every time a package gets updated .Well that’s where the virtual environment comes in .
You can create a virtual environment that contains the newer version of the package or the virtual environment for your older version of the package
however luckily Anaconda makes this really easy for us .(A virtual handler is already included in Anaconda.)

Answered By: Shulav karki

You need Python virtual environments to manage the Python package dependencies of your project, so it will always have its correct version packages, for example, when installing on another machine. Python virtual environments can be managed with pip (for packages) and virtualenv (for virtual environments), OR with conda (which does both).

For more information, see this article: “Why you need Python environments and how to manage them with Conda“.

Answered By: krubo

first we create virtual wrapper

pip install virtualenv wrapper-win

and after make wrapper environment now
create a virtual env-

mkvirtualenv envname

(command run only 64 bit python)
And if you want to start virtual env then set your workplace(Directory)
using Command Prompt and write command

workon envname

Answered By: Ravendra Kumar
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.