004-Django - Create Virtual Environment

Objectives: Django - Create Virtual Environment

Django - Create Virtual Environment

Django - Create Virtual Environment

Why Virtual Environments?

A virtual environment is like a separate workspace for each Django project. It keeps your project’s dependencies (libraries) isolated, so one project does not break another.

Real-life Example:

Imagine you are cooking Italian food in one kitchen and Indian food in another. Each kitchen has its own ingredients and recipes — no mixing up! That’s exactly what virtual environments do for projects.

Advice:

Always use a virtual environment for Django projects. Without it, you might face conflicts between different library versions.

1. Create Virtual Environment

Python comes with a tool called venv. To create a virtual environment, first navigate to the folder where you want your project, then run:

python -m venv myworld

This will create a folder named myworld with subfolders and files:

myworld/
  Include/
  Lib/
  Scripts/
  .gitignore
  pyvenv.cfg
  

Real-life Example:

Think of myworld as a new "sandbox" where you can build without affecting your main computer setup.

2. Activate the Environment

Before using it, you need to activate the virtual environment:

  • Windows: myworld\Scripts\activate.bat
  • Unix/MacOS: source myworld/bin/activate

If successful, your terminal prompt will change:

Windows:

(myworld) C:\Users\Your Name>

Unix/MacOS:

(myworld) ... $

Advice:

Each time you open a new terminal session, you must activate the virtual environment again. Otherwise, Python will use the global environment.

3. Install Django

With your environment activated, you can now install Django by typing:

pip install django

Real-life Example:

Installing Django inside myworld is like stocking your kitchen with ingredients — ready to start cooking (coding)!

Advice:

Keep your virtual environment folder (myworld) outside your project folder. This keeps things organized and avoids mistakes when sharing projects.

Reference Book: N/A

Author name: SIR H.A.Mwala Work email: biasharaboraofficials@gmail.com
#MWALA_LEARN Powered by MwalaJS #https://mwalajs.biasharabora.com
#https://educenter.biasharabora.com

:: 1::