2.1 Installing Python
Key Concepts
Before diving into Django, it's essential to have Python installed on your system. Python is the foundational language for Django, and having the correct version is crucial for compatibility and performance.
1. Python Version
Python comes in two major versions: Python 2 and Python 3. Django, however, requires Python 3. It's important to install the latest stable version of Python 3 to ensure compatibility with the latest Django features and security updates.
2. Installation Process
The installation process varies depending on your operating system. Below are the steps for Windows, macOS, and Linux.
Windows
To install Python on Windows:
- Visit the official Python website: https://www.python.org/downloads/.
- Download the latest version of Python 3 for Windows.
- Run the installer and ensure to check the box that says "Add Python to PATH" during installation.
- Complete the installation and verify by opening Command Prompt and typing
python --version
.
macOS
To install Python on macOS:
- Visit the official Python website: https://www.python.org/downloads/.
- Download the latest version of Python 3 for macOS.
- Run the installer and follow the on-screen instructions.
- Verify the installation by opening Terminal and typing
python3 --version
.
Linux
To install Python on Linux:
- Open Terminal.
- Update the package list using
sudo apt-get update
. - Install Python 3 using
sudo apt-get install python3
. - Verify the installation by typing
python3 --version
.
3. Virtual Environments
A virtual environment is an isolated Python environment that allows you to manage dependencies for different projects. This is particularly useful when working on multiple Django projects with different requirements.
To create a virtual environment:
python3 -m venv myenv
To activate the virtual environment:
# On Windows myenv\Scripts\activate # On macOS and Linux source myenv/bin/activate
4. Verifying Installation
After installing Python and setting up a virtual environment, you can verify the installation by running a simple Python script.
# Create a new Python file echo "print('Hello, Django!')" > hello.py # Run the script python3 hello.py
If the output is "Hello, Django!", then Python is installed correctly.