Need python2 for a Windows based Django environment

I have python 3.8 on my Windows 10 laptop.
I have a project which in Django 2.2 and runs on python 2.7 on the server.
Now I want to run that on my localhost which I set up based on django docs.

But now I want to remove python3 from the environment and have python2. Is something like this possible ?

PS E:>.\django-project-x\Scripts\activate
pip uninstall python3
pip install python2

Mainly because of wanting some python2 specific libraries like urllib2 etc.

Hey there!
You don’t need to uninstall python3 to use python2.
In fact, you can have multiple versions of python installed.
Maybe this article from freecodecamp.org helps you!

Side note:
Notice that both python2.7 and django2.2 are deprecated and no longer receive security patches.

So I have to delete the E:\django-project-x\Scripts folder and re-create my virtual environment ?

The virtual environment can be deleted safely anytime. It’s not the real python installation, just a virtual one so you can have multiple simultaneous projects without dependencies clashing between them.
If you have python2.7 already installed, you can just create a new python2.7 virtual environment, activate it and install the dependencies from there.
If you plan to migrate to python3 having multiple environments will also help you test your project in new python / django version.

I need to create a separate environment name like project-name-py2 since the current environment directory project-name contains django project files which were there by default when django was created like settings.py and urls.py.

C:\Python27\python.exe -m venv project-name-py2

So now, I should have 2 environment folders project-name for python3 and project-name-py2 for python2 right ?

Anything wrong before I execute the above step in command line ?

Yeah, i think that’s it! the name of the environment does not affect anything other how you activate it.

[opinion]
When i worked with virtualenv (venv) i liked to name my environments like:
venv - If i had only one env
venv{major}{minor} - if i had multiple environments. That will look something like:

  • venv38 (python3.8)
  • venv39 (python3.9)

[/opinion]

I’m done ! And it’s all working - thanks a bunch.

Though venv is not supported - I had to use virtualenv

C:\Python27\python.exe -m pip install virtualenv
C:\Python27\python.exe -m virtualenv py2
.\django-project-x\py2\Scripts\activate
python -m pip install Django==1.10.3
python .\manage.py runserver

Thing is, the py2 environment has created all the .pyc files all over the place in the same folders as the .py files.
In venv it created a separate folder called __pycache__

1 Like