Virtual Environment for Python Django

15,217

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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".

Share:
15,217
Steve D.
Author by

Steve D.

Updated on July 28, 2022

Comments

  • Steve D.
    Steve D. almost 2 years

    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!