[Python] My Whirlwind Adventure with Django and Virtual Environments

[Python] My Whirlwind Adventure with Django and Virtual Environments

You might be wondering how I ended up diving into Python and wrestling with virtual environments. Well, to cut a long story short, I found myself in a bit of a pickle while trying to run a company project on my machine. Being a bit of a Django novice, I encountered a slew of perplexing errors along the way. So, I thought, maybe I should try creating a Django project from scratch. But lo and behold, I ran into this frustrating error:

fz3hra@Ummes-MacBook-Pro code % django-admin startproject learn_django
zsh: /opt/homebrew/bin/django-admin: bad interpreter: /opt/homebrew/opt/python@3.11/bin/python3.11: no such file or directory

Yes, I know, not the most convenient situation when you've got Python 3.12 on your machine, and suddenly Python 3.11 is causing trouble. So, my first instinct was to delve into the world of Virtual Environments since I reckoned this could be the one causing issues after having read this article: https://superuser.com/questions/1380418/python3-7-bad-interpreter-no-such-file-or-directory.

Here's my understanding on VE:

Imagine you have a shelf of books. You wouldn't want all your books jumbled together, right? Instead, you'd prefer to neatly organize them by genre or subject. Well, the same principle applies to virtual environments when working with Django on your local machine.

Think of it this way: You may have multiple projects, each requiring different versions of Django. How do you manage this? You create a virtual environment for each project.

The process is pretty straightforward, and here are the steps:

  1. install the python version of your choice
  2. create the virtual environment
  3. and activate the environment
brew install python@version_of_your_choice
python@version_of_your_choice -m venv venv
source venv/bin/activate

Now, let's dig a bit deeper into why the initial issue occurred. It turned out that the conflict arose from having different Python versions due to global pyenv installation on my machine. Removing pyenv resolved the problem. 😉

In conclusion, my encounter with Python virtual environments was quite an enlightening experience. They're a lifesaver when dealing with multiple projects that require different Python and Django versions.


For a better grasp of virtual environments in Django, you might want to check out the following resources:

What is a virtualenv, and why should I use one?
I am trying to install a Python package with this command pip install <name of package> I’m getting permission errors and I’m not sure why. I could run it with sudo, but someone told me th…

About Me

I am Zaahra, a Google Women Techmakers Ambassador who enjoy mentoring people and writing about technical contents that might help people in their developer journey. I also enjoy building stuffs to solve real life problems.

To reach me:

LinkedIn: https://www.linkedin.com/in/faatimah-iz-zaahra-m-0670881a1/

X (previously Twitter): _fz3hra

GitHub: https://github.com/fz3hra

Cheers,

Umme Faatimah-Iz-Zaahra Mujore | Google Women TechMakers Ambassador | Software Engineer