Setting up the Django Virtual Environment
(Picture Source: https://stock.adobe.com/)
Hello wizards! Welcome to today's gathering where we are going to discuss the Django Virtual Environment. As always, Uncle Harry and I promise to never allow these "big" terms harass our wizards. He's actually here to ensure your safe passage to understanding the Django Virtual Environment. Actually, Uncle Harry will personally explain what the virtual environment is in the simplest of terms. Enjoy!
"Hello there! I'm Uncle Harry, and I'm gonna tell you about something cool - Django virtual environments. So, imagine you're making magic on your computer, building awesome websites with a spellbook called Django.
Now, sometimes you have different magical projects, right? Well, you don't want all the spells and tricks to get mixed up. That's where Django virtual environments come in! Think of it like having special rooms for each project. When you start a new one, you create a magical room (virtual environment) just for it. Inside that room, Django has its own tools and toys without messing up other projects.
So, Django virtual environments are like magical rooms to keep everything neat and tidy. It's like having a secret spot for each project's magic, so they don't accidentally mix up. Cool, right?"
Let's now see Uncle Harry's explanation practically. Please follow through the 4 magic steps below and don't worry if you don't understand everything that you do instantly. All these will become apparent as we proceed!
Step 1 - Install virtualenv
Now that we have Python installed on our computers, open your command prompt and run the following command:
pip install virtualenv
Step 2 - Create a new project folder
Now that you have virtualenv installed, the next step is to create a new folder where your Django project is going to reside in. To do this, you can either use your file explorer on the windows graphical user interface or use your command prompt and inserting the following commands:
mkdir django_project
Then, to navogate into the project you have created, use the following command:
cd django_project
Btw: You can always replace "django_project" with your desired project name.
This folder will serve as your higher level directory that will house all the files related to your django project including the virtual environment we are going to create in the next step.
Step 3 - Create the virtual environment
Now that you are within your "django_project" folder, run the following command to create your virtual environment:
virtualenv venv
Step 4 - Activate the virtual environment:
With that, our virtual environment is now created. The only thing remaining is to activate it so that we can use it. Use the following command to activate it:
venv\Scripts\activate
Please take keen note that we've used a backslash "\" and not a forwardslash "/" to activate our virtual environment. Most wizards make this mistake thus the need to point it out.
To know that it worked, you'll see (venv) at the start of the command line as in the screenshot below:
By the way, to deactivate the virtual environment, use the following command. You'll notice that the (venv) at the start of the command line disappears when you run the command:
venv\Scripts\deactivate
Psst! Seems Uncle Harry has something to share with us as we wrap up. Lets hear what he has for us:
"Hey there! That must've been a fine ride we've had upto this point. Though, let's try to understand what happened here.
We've set up a special folder called "venv" where we store a separate copy of Python. When we activate this "venv," it tells our system to use this local Python instead of the one we installed globally.
When we run the "python" command after activating "venv," it refers to the Python inside the "venv" folder. This helps us keep our project isolated and prevents potential conflicts with other Python projects or the system-wide Python installation.
Inside this isolated environment ("venv"), we also have a tool called "pip," which is used to install Python packages. When we use "pip" to install something, like the Django framework, it gets installed specifically within the "venv" environment. This ensures that each project can have its own dependencies without affecting the global Python setup or other projects.
You'll see how this will help us in installing django in the next step!
That's it for now! Thanks for being part of this gathering and I'll see you in the next one. As usual, I'll leave a useful link to the next gathering somewhere below! Adios!"