Choose your own Virtual Environment.
Virtual environments are an indispensable part of Python programming. It allows us to keep project-specific dependencies in a separate place than our global site-packages. This is extremely useful when you have different versions of packages for different projects.
Virtual environments are an isolated container containing all the software dependencies for a given project. This is important because by default software like Python and Django is installed in the same directory. This causes a problem when you want to work on multiple projects on the same computer. What if ProjectA uses Django 2.0 but ProjectB from last year is still on Django 1.10? Without virtual environments this becomes very difficult; with virtual environments, it’s no problem at all.
So, I am going to show you the three approaches to create the virtual environment for a new Django project.
1) virtualenv approach
2) pipenv approach
3) conda approach
virtualenv approach
To start a project first you need to create a directory to store all the project files. For that, you have to make a directory:
C:\Users\Shritam\Desktop>mkdir mysite
I am creating a directory folder and naming it as mysite.
Then I’ll redirect to that directory:
C:\Users\Shritam\Desktop>cd mysiteC:\Users\Shritam\Desktop\mysite>
Now we have got a place to create our virtual environment. On mysite folder create a virtual environment, naming it as django_env
C:\Users\Shritam\Desktop\mysite>virtualenv django_env
Using base prefix ‘c:\\users\\shritam\\appdata\\local\\programs\\python\\python37–32’
New python executable in C:\Users\Shritam\Desktop\mysite\django_env\Scripts\python.exe
Installing setuptools, pip, wheel…
done.
Now you can check your mysite a new django_env folder has have been created. After that, you have to activate the virtual environment which you have created:
C:\Users\Shritam\Desktop\mysite>django_env\Scripts\activate.bat
That is because the activate.bat is under the Script folder and the Script folder is under the django_env folder. You can check it through tree command on cmd.
C:\Users\Shritam\Desktop\mysite>tree
Now your virtual environment is ready for a new Django project. You can see a separate django_env environment on your cmd.
(django_env) C:\Users\Shritam\Desktop\mysite>
You can check where the python is and where the pip is. All the project will be isolated under the django_env environment:
(django_env) C:\Users\Shritam\Desktop\mysite>which python
/c/Users/Shritam/Desktop/mysite/django_env/Scripts/python
Now you can check what are the lists present on pip initially:
(django_env) C:\Users\Shritam\Desktop\mysite>pip list
Package Version
— — — — — — — — -
pip 18.1
setuptools 40.6.2
wheel 0.32.3
Now you can install the packages that are needed on your project. We need Django for the project so we’ll install Django in our environment.
(django_env) C:\Users\Shritam\Desktop\mysite>pip install django
Collecting django
Using cached https://files.pythonhosted.org/packages/fd/9a/0c028ea0fe4f5803dda1a7afabeed958d0c8b79b0fe762ffbf728db3b90d/Django-2.1.4-py3-none-any.whl
Requirement already satisfied: pytz in c:\users\shritam\desktop\mysite\django_env\lib\site-packages (from django) (2018.7)
Installing collected packages: django
Successfully installed django-2.1.4
If you need any version of Django you can install it as:
(django_env) C:\Users\Shritam\Desktop\mysite>pip install django==version
Now you are ready to start your Django project.
pipenv approach
Historically Python developers have used either virtualenv to configure virtual environments. But in 2017 prominent Python developer Kenneth Reitz released Pipenv which is now the officially recommended Python packaging tool.
Pipenv is a dependency manager for Python projects. Pipenv manages dependencies on a per-project basis. To install packages, change into your project’s directory (or just an empty directory for this tutorial) and run:
Use pip
to install Pipenv:
pip install --user pipenv
Install Django To see Pipenv in action, let’s create a new directory and install Django. First, navigate to the Desktop, create a new directory mysite, and enter it with cd.
C:\Users\Shritam\Desktop>mkdir mysite
C:\Users\Shritam\Desktop>cd mysite
C:\Users\Shritam\Desktop>mysite>
Now use Pipenv to install Django.
C:\Users\Shritam\Desktop>mysite>pipenv install django==2.0.6
If you are on a Mac you should now see parentheses on your command line with the environment activated. It will take the format of the directory name and random characters.
(mysite-JmZ1NTQw)
The end result is that we will create a new virtual environment with Pipenv for each new Django Project.
conda approach
In this approach, you have to make a directory by mkdir
and change into the newly made directory.
Now we are going to make a virtual environment using conda so we need to figure out what version of python and which packages we would like to use in our project. So to do this we can say:
conda create --name django_env django==1.9
Then it’ll show all the dependencies of packages like here it is Django.
Then it’ll ask for confirmation:
processed [y]/n
Enter y.
To activate the virtual environment for the Django project enter
django-env activate
Now your virtual environment is activated and you are ready to use the Django 1.9 version
(django_env) C:\Users\Shritam\Desktop\mysite>
Now the choice is up to you. You can choose any one process to create a virtual environment. Congratulations, you now know how to effectively manage dependencies and development environments on a collaborative Python project!
Happy coding and writing about coding!