Anjanesh Lekshminarayanan
Anjanesh

Anjanesh

Running a Django app natively on Windows (for development)

Anjanesh Lekshminarayanan's photo
Anjanesh Lekshminarayanan
·May 12, 2022·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

At work we use Windows 10 Ent edition and have a Django 2.2 website that is run on the developers' localhost via VirtualBox with Ubuntu 20.04 since the beginning. It was good but one had to constantly do sys-admin for the Ubuntu VM. That wasn't a big deal, but differences in versions of libraries kept us awake.

Last year some of the devs created a Dockerfile to run the app via Docker on our local, so this January an email was sent to all that the Docker setup would be the officially supported version for development on localhost.

Though the setup of Docker image on our laptop with the container in our AWS S3 bucket was thoroughly documented, there was one issue everyone faced during development of the Django app - the restart of the image upon editing the code has a long refresh delay - somewhere on the lines of 5 secs or more - which was unacceptable and most switched back to the VirtualBox setup.

Even though I stuck to the Docker setup since the beginning of this year, I didn't mind the 5+ seconds wait time for each F5 (refresh) on the browser. But there were many times when I had to wait for several minutes due to restart of the container which then tested my patience.

I can't switch between Docker and VirtualBox whenever I feel like since Hyper-V needs to be disabled for VirtualBox as it causes problems resulting in the VirtualMachine instance crashing big-time and possibly loosing out on the VM file itself. And disabling / enabling Hyper-V requires Windows restart. So I never opened my VirtualBox after installing Docker.

One thing I was interested in doing was to try running the Django app on my Windows 10 laptop natively. This was frowned upon by many as a not a suitable solution since many backend operations depend on linux features. But I still decided to give it a go. After all, Windows now is not like before and Powershell is more powerful, with the newest version being truly cross-platform. Moreover, me being a front-end UI developer, I was primarily involved in editing UI - HTML (Django templates), CSS and JavaScript and most recently a React project which was a section (DIV) in the main Django app.

First thing first - create a virtual environment (it's a python environment, not an OS level one) : docs.djangoproject.com/en/4.0/howto/windows..

In PowerShell PS D:\projects\project-name> create a virtual environment.

python -m venv project-name

This would create 3 folders in D:\projects\project-name\project-name - Include, Lib and Scripts

(You don't need to create the virtual environment name as project-name - you might as well name it project-name-env which is what I should have done at first, I followed the djangoproject docs which mentions python -m venv project-name as opposed to another name for the environment)

And run activate.bat to activate it

project-name\Scripts\activate.bat

Install the the dependencies :

(project-name) pip install -r requirements.txt

If all is well then run the server, else you may want to install some Windows specific libraries like VC++ redistributables which are required for some Django modules in the project I was working on.

Run the server :

(project-name) PS D:\projects\project-name> python .\manage.py runserver

But my Django 2.2 was throwing an error which failed to start.

ImportError: cannot import name 'Celery' from partially initialized module 'celery' (most likely due to a circular import)

This was because I had a filename celery.py in D:\projects\project-name\project-name which has from celery import Celery and at the same time celery.py was also present in D:\project-name\project-name\Lib\site-packages\celery\bin which caused a circular import and crashed. I had to rename the custom celery.py to another name but I guess this issue won't arise if your environment name is different from your project name.

Now my Django app refreshes without much delay and development is far easier / faster even on Windows.

 
Share this