how-run-django-conda-environment

Before running Django with Anaconda, let’s discuss some terms that will be helpful for understanding this blog post in-depth.

Django

Django is the most popular free and open-source Python Web framework that believes in the development of the website on the go. Django provides lots of built-in features(such as admin panel, SQLite, model managers, 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 be going to use conda package manager which manages the package versions in Anaconda. If you have not installed Anaconda on your pc yet follow instructions by clicking here.

 

Problems running Django with Pip

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

Alternatively, to solve packages collisions in Pip, conda will be beneficial for the package manager which comes along with the environment manager. So that we don’t have to depend upon other environments 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.

 

Run Django in a conda environment

I hope all set up were done, now let’s run Django in a conda environment.

Step 1:

Create a new environment named django_env with a specific version of python inside your working directory. I am working on my django_project directory inside the Desktop folder on a 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\XYZ\Desktop>mkdir django_project

C:\Users\XYZ\Desktop>cd django_project

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

It will take some time to download packages.

While creating an environment where 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 the pip package is also installed in our newly created environment.

 

Step 2:

Now its time to activate the newly created Conda environment

Command line

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

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

django_env inside the parenthesis denotes that the environment is activated.

 

Step 3:

Up to 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\XYZ\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\XYZ\Desktop\django_project>django-admin startproject mysite .

(django_env) C:\Users\XYZ\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 let’s run it on the local development server.

Command line

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

 

Step 6:

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

django-runserver-homepage

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

 

Some Useful conda Commands:

  • To deactivate the 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 cheat sheet click here

 

Conclusion

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

Happy coding 🙂

Leave a Reply

Your email address will not be published.