Ensure that Python is installed on your system. Most Python installations come with the 'venv' module, which is used to create virtual environments. You can check if Python is installed by opening a command-line interface and typing:
python --version
It's a good practice to create a new directory for your Django project. This helps keep things organized:
mkdir my_django_project
cd my_django_project
Once you're in the project directory, create a virtual environment. The following command creates a virtual environment named venv in your current directory:
python -m venv venv
This command uses Python to create a virtual environment in a folder named venv.
Before installing Django, you need to activate the virtual environment. The activation command depends on your operating system:
venv\Scripts\activate
With the virtual environment activated, you can now install Django using pip:
pip install django
This command downloads and installs Django and its dependencies into your virtual environment.
To confirm that Django is installed, you can run the following command:
django-admin --version
If Django is installed correctly, you should see its version number.
When you're done working in the virtual environment, you can deactivate it with the following command:
deactivate
This command exits the virtual environment, and your command prompt should return to normal. To reactivate the virtual environment later, use the activation command from Step 4.
With these steps, you've created a virtual environment for Django, installed Django, and learned how to activate and deactivate the environment. Now you can start building your Django project in a clean, isolated workspace without interfering with other Python projects or system settings.