Before running django with anaconda, lets discuss some terms that will be helpful for understanding the blog in depth.

Django

Django is the most popular free and open source Python Web framework that believes in development of the website on the go. Django provides lots of built in features(such as admin panel, sqlite, etc.) which helps developers to focus on the core content of their app. Most popular IT companies in the world like Disqus, Instagram, Spotify, Youtube and others  used django (Click link for more info).

Anaconda

Anaconda is an open source distribution of the Python and R programming languages developed by Anaconda, Inc. for scientific computing. Here in this blog, we will going to use conda package manager which manages the package versions in Anaconda. If you have not installed anaconda in your pc yet follow  instructions by click here .

Problems running django with Pip

Pip is the python package manager which already included at the python installer uses in installing packages from the Python Package Index, PyPI. Pip package management processes are hectic as we can't able to manage packages for particular app in django. It has no built in support for environments so have to depends on tools like virtualenv or venv to create isolated environments.

Alternatively, to solve packages collisions in Pip, conda will be beneficial for package manager which comes along with environment manager. So that we don't have to depend upon other environments as like Pip.

To know more about pip and conda differences click here

Note: We can use pip commands inside conda environments but vice versa is not true.

I hope all set up were done, now lets run django in conda environment.

Step 1:

Create a new environment named django_env with specific version of python inside your working directory. I am working on my django_project directory inside Desktop folder in Windows computer.

Note: If we do not mention any python version, then the environment will be created with the latest version available.

Command line


C:\Users\SHIV\Desktop>mkdir django_project

C:\Users\SHIV\Desktop>cd django_project

C:\Users\SHIV\Desktop\django_project>conda create --name django_env python=3.8

It will take some time to download packages.

While creating environment there you will be asked to Proceed ([y]/n)?

Enter 'y' and continue.

The following NEW packages will be INSTALLED:

  ca-certificates    pkgs/main/win-64::ca-certificates-2020.6.24-0
  certifi            pkgs/main/win-64::certifi-2020.6.20-py38_0
  openssl            pkgs/main/win-64::openssl-1.1.1g-he774522_0
  pip                pkgs/main/win-64::pip-20.1.1-py38_1
  python             pkgs/main/win-64::python-3.8.3-he1778fa_0
  setuptools         pkgs/main/win-64::setuptools-47.3.1-py38_0
  sqlite             pkgs/main/win-64::sqlite-3.32.3-h2a8f88b_0
  vc                 pkgs/main/win-64::vc-14.1-h0510ff6_4
  vs2015_runtime     pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_2
  wheel              pkgs/main/win-64::wheel-0.34.2-py38_0
  wincertstore       pkgs/main/win-64::wincertstore-0.2-py38_0
  zlib               pkgs/main/win-64::zlib-1.2.11-h62dcd97_4

As we can see that pip package also installed in our newly created environment.

Step 2:

Now its time to activate the newly created Conda environment

Command line


C:\Users\SHIV\Desktop\django_project>conda activate django_env

(django_env) C:\Users\SHIV\Desktop\django_project>

django_env inside the parenthesis denotes that the environment is activated.

Step 3:

Upto the point we created and activated the environment in our working directory. Now we install django package in django_env.

Command line


(django_env) C:\Users\SHIV\Desktop\django_project>conda install -c anaconda django 

It will take time to install django with other necessary packages.

The following NEW packages will be INSTALLED:

  asgiref            anaconda/noarch::asgiref-3.2.7-py_0
  django             anaconda/noarch::django-3.0.3-py_0
  pytz               anaconda/noarch::pytz-2020.1-py_0
  sqlparse           anaconda/noarch::sqlparse-0.3.1-py_0

Step 4:

Create a new Django project called mysite making sure to include the period . at the end of the command so that it is installed in our current directory

Command line


(django_env) C:\Users\SHIV\Desktop\django_project>django-admin startproject mysite .

(django_env) C:\Users\SHIV\Desktop\django_project>tree /F
Folder PATH listing
Volume serial number is E206-9EEA
C:.
|   manage.py
|
|---mysite
        asgi.py
        settings.py
        urls.py
        wsgi.py
        __init__.py

Our working directory should look like this after creating mysite project.

Step 5:

We successfully created our project now lets run it in local development server.

Command line


(django_env) C:\Users\SHIV\Desktop\django_project>python manage.py runserver

 Step 6:

Now open your favourite browser and type address 127.0.0.1:8000 and see the magic.

output from django server

To exit from the local development server press 'Ctrl+c' in your terminal.

Some useful conda commands:

  • To deactivate current environment

conda deactivate

  • To see the installed packages list

conda list

  • To see the all conda environment lists

conda env list

  • To remove the just created environment

conda env remove –n django_env

For conda cheet sheet click here

Conclusion

In this blog, we were successful to run django with conda environment.

Happy coding :-)