Creating and Managing Python Virtual Environments and Running a Django Project

Python Virtual Environments provide an isolated workspace for managing dependencies, Python versions, and frameworks such as Django. By using virtual environments, developers can avoid conflicts between packages and run multiple projects with different configurations. This article explains how to create virtual environments with specific Python versions, activate them, update pip, install Django (latest or specific versions), and finally run a Django project using the development server.

Python Virtual EnvironmentDjango installationpip management

~2 دقیقه مطالعه · آخرین به‌روزرسانی ۱۵ بهمن ۱۴۰۴

1. What Is a Python Virtual Environment?


A Virtual Environment is an isolated environment that allows a Python project to have its own dependencies, libraries, and Python version without affecting other projects or the system-wide installation.


This approach helps prevent version conflicts and ensures project stability.


2. Creating a Virtual Environment with a Specific Python Version


You can create virtual environments using different installed Python versions.


py -3.14 -m venv myenv

The command above creates a virtual environment named myenv using Python 3.14.


py -3.13 -m venv oldenv

This command creates another environment using Python 3.13.


3. Activating the Virtual Environment on Windows


After creating the environment, it must be activated.


C:\Users\python\Desktop\myenv\Scripts>activate

Once activated, the environment name appears at the beginning of the terminal:


(myenv) C:\Users\python\Desktop\myenv\Scripts>

4. Checking Python and pip Versions


To check the active Python version:


py -V

To check the installed pip version:


pip -V

5. Updating pip to the Latest Version


To upgrade pip inside the virtual environment:


python -m pip install --upgrade pip

6. Listing Installed Packages


To view all installed packages in the environment:


pip list

To export installed packages to a file:


pip freeze > package.txt

This file can be used to recreate the environment on another system.


7. Installing Django in the Virtual Environment


To install the latest version of Django:


pip install django

To install a specific version of Django:


pip install django==5.0.3

Verify the installed Django version:


django-admin --version

8. Creating a Django Project


To create a new Django project:


django-admin startproject myproject

Navigate into the project directory:


cd myproject

9. Running the Django Development Server


To start the Django development server:


python manage.py runserver

The project will be available at the following address:


http://127.0.0.1:8000/

Conclusion


Using a Python Virtual Environment allows developers to safely manage dependencies, Python versions, and frameworks such as Django. This workflow is essential for building scalable, portable, and professional Python projects.


نوشته و پژوهش‌شده توسط دکتر شاهین صیامی

مقالات مرتبط

Django Tasks Framework: A Complete Guide to Background Task Execution in Django 6.0

Django 6.0 introduces the Tasks framework, a built‑in system for defining and queuing background work outside the request–response cycle. This article explains how Tasks work, how to configure backends, how to define and enqueue tasks, how context works, and how to integrate third‑party worker systems for production environments.

ادامه

Asynchronous Support in Django: A Complete Guide to Async Views, ORM, Middleware, Performance, and Safety

This article explains Django’s asynchronous (async) capabilities, including async views, ASGI support, middleware behavior, async ORM features, performance considerations, handling disconnects, and Django’s async safety protections. It also covers how to use sync_to_async(), async ORM methods, and how to safely run synchronous code in async environments.

ادامه

Django System Check Framework: A Complete Guide to Writing, Registering, Running, and Testing System Checks

This article explains Django’s System Check Framework—a powerful mechanism for detecting configuration issues, validating project structure, and ensuring code quality. It covers how checks are executed, how to write custom checks, how messages work, how to register and tag checks, how to extend checks for fields and models, and how to write both unit and integration tests for system checks.

ادامه

Django Signals: A Complete Guide to Listening, Connecting, Sending, and Managing Application Events

This article provides a comprehensive explanation of Django’s signal system—an event‑driven mechanism that allows decoupled applications to react to actions occurring elsewhere in the framework. It covers how to define receivers, connect signals, use decorators, handle specific senders, organize signal code, and follow best practices to avoid complexity.

ادامه

Understanding Django Settings: Configuration, Environment Management, and Best Practices

This article provides a complete overview of Django’s settings system. It explains how settings files work, how to designate a settings module, how to use settings in your code, how to configure Django manually, how to secure sensitive settings, and how to work with custom default settings. It also covers the role of django.setup() for standalone scripts.

ادامه

Serializing and Deserializing Django Objects: A Complete Guide to Django’s Serialization Framework

This article explains Django’s serialization framework, including how to serialize and deserialize model instances, work with subsets of fields, handle inherited models, use different serialization formats (JSON, XML, YAML, JSONL), and understand how relational fields are represented. It also covers DeserializedObject behavior and common pitfalls.

ادامه