Lesson 6 of 60 – Python Virtual Environment
10%

🐍 Python Virtual Environment

In this lesson, you will learn about Python virtual environments and how they are used in Django projects. A virtual environment creates an isolated Python environment for a project.

Note: Using a virtual environment is a recommended practice when working with Django projects because each project can have its own packages and package versions.

1. What Is a Virtual Environment?

A Python virtual environment is an isolated environment where Python packages can be installed separately from the system Python installation.

It is commonly used when developing Django and other Python applications.

2. Why Use a Virtual Environment?

Different projects may require different versions of Python packages. A virtual environment keeps project dependencies separated.

  • Separates project dependencies
  • Prevents package conflicts
  • Keeps projects organized
  • Makes project setup easier to reproduce

3. Virtual Environment and Django

Django is installed as a Python package. Installing Django inside a virtual environment means that the Django installation belongs to that particular project environment.

python -m pip install Django

4. Create a Virtual Environment

Python provides the venv module for creating virtual environments.

python -m venv venv

The first venv refers to the Python module, while the second venv is the name of the environment folder.

5. Choose a Virtual Environment Name

The environment folder can have another name. For example:

python -m venv myenv

This creates a virtual environment named myenv.

6. Windows Virtual Environment

On Windows, you can create an environment using:

python -m venv venv

A directory named venv is created inside the current project directory.

7. Linux and macOS Virtual Environment

Linux and macOS users can create an environment using:

python3 -m venv venv

This creates the virtual environment in the venv directory.

8. Activate Virtual Environment on Windows

After creating the environment, activate it on Windows with:

venv\Scripts\activate

After activation, the environment name normally appears in the terminal.

9. Activate Virtual Environment on Linux/macOS

On Linux and macOS, activate the environment using:

source venv/bin/activate

10. How to Know It Is Activated

When the environment is activated, its name generally appears before the command prompt.

(venv) C:\DjangoProject>

Here, venv indicates that the virtual environment is active.

11. Install Django in the Environment

After activating the environment, install Django using pip:

python -m pip install Django

Django will be installed into the active environment.

12. Check Django Version

After installing Django, verify the installation:

python -m django --version

The command displays the Django version installed in the active environment.

13. Virtual Environment Contains Its Own Packages

Packages installed inside a virtual environment are kept separate from packages installed globally.

This allows different projects to use different package versions.

14. Global Python Installation

The system Python installation is available outside the virtual environment. Packages installed globally can potentially be used by multiple projects.

For project development, an isolated environment is usually easier to manage.

15. Virtual Environment Folder

When you create a virtual environment named venv, Python creates a directory containing the environment's files.

python -m venv venv

The folder should generally be treated as an environment directory rather than as application source code.

16. Deactivate the Environment

When you finish working with the environment, you can deactivate it using:

deactivate

After deactivation, the terminal returns to the normal system Python environment.

17. Activate It Again

A virtual environment can be activated again whenever you return to the project.

Windows:

venv\Scripts\activate

Linux/macOS:

source venv/bin/activate

18. Check Installed Packages

After activating the environment, you can see the installed packages with:

python -m pip list

This is useful for checking which packages are installed in the active environment.

19. Check the Python Location

You can check which Python executable is being used.

On Windows:

where python

On Linux/macOS:

which python

This can help confirm whether the active environment's Python is being used.

20. Multiple Django Projects

You can create separate virtual environments for different Django projects.

For example:

ProjectA/
    venv/

ProjectB/
    venv/

Each project can manage its dependencies independently.

21. Package Version Conflicts

Suppose one project needs one version of a package while another project needs a different version. Installing everything globally can create dependency conflicts.

Virtual environments help keep these project dependencies separated.

22. requirements.txt

Python projects commonly use a file named requirements.txt to record required packages.

A package list can be generated with:

python -m pip freeze > requirements.txt

This records installed package versions in the current environment.

23. Install Packages from requirements.txt

Another environment can install the packages listed in requirements.txt.

python -m pip install -r requirements.txt

This is useful when setting up an existing Python or Django project.

24. Do Not Usually Upload venv

The virtual environment contains installed packages and environment-specific files. It is normally recreated on another computer instead of being copied as part of the project source.

A requirements.txt file can be used to describe the required packages.

25. Virtual Environment Workflow

A typical workflow is:

  1. Create the project folder
  2. Create a virtual environment
  3. Activate the environment
  4. Install Django
  5. Build the Django project
  6. Deactivate the environment when finished

26. Complete Windows Example

Example:

mkdir MyDjangoProject
cd MyDjangoProject
python -m venv venv
venv\Scripts\activate
python -m pip install Django
python -m django --version

27. Complete Linux/macOS Example

Example:

mkdir MyDjangoProject
cd MyDjangoProject
python3 -m venv venv
source venv/bin/activate
python -m pip install Django
python -m django --version

28. Common Virtual Environment Mistake

A common mistake is installing Django before activating the intended virtual environment.

If Django is installed in the wrong environment, your project may not find the expected package.

Always activate the correct environment before installing project packages.

29. Best Practice for Django Projects

For each Django project, create and activate an appropriate virtual environment before installing project dependencies.

This keeps your development environment cleaner and makes dependency management easier.

30. Virtual Environment Summary

A virtual environment provides an isolated Python environment for a project. It is especially useful for Django development because projects can have separate dependencies.

python -m venv venv
venv\Scripts\activate
python -m pip install Django

On Linux/macOS, activation is commonly done with:

source venv/bin/activate

📌 Key Points

  • A virtual environment isolates Python project dependencies.
  • Python provides the venv module for creating environments.
  • Use python -m venv venv to create an environment.
  • Activate the environment before installing project packages.
  • Use deactivate to leave the active environment.
  • Django can be installed inside the active environment using pip.
  • requirements.txt can record project dependencies.

🧠 Quick Quiz

Question: Which command creates a Python virtual environment named venv?