Why does Django not support Python 3

Getting started using Python for web development on Windows

  • 17 minutes to read

Below is a detailed guide to get started using Python for web development on Windows using the Windows Subsystem for Linux (WSL).

Setting up the development environment

When building web applications, we recommend installing Python on WSL. Many of the Python web development tutorials and guides are written for Linux users and use Linux-based packaging and installation tools. Most web apps are also deployed on Linux, so you can be sure that there is consistency between your development and production environment.

If you are using Python for anything other than web development, we recommend installing Python directly on Windows 10 from the Microsoft Store. WSL does not support GUI desktops and GUI applications (e.g. PyGame, Gnome or KDE). In these cases, install and use Python directly on Windows. If you're new to Python, check out our guide: Getting Started Using Python on Windows for Beginners. If you want to automate common tasks in the operating system, check out our Getting Started Using Python on Windows for Scripting and Automation guide. For some advanced scenarios it is advisable to download a specific Python release directly from python.org or to install an alternative, e.g. B. Anaconda, Jython, PyPy, WinPython, IronPython, etc. This is only recommended if you are an accomplished Python programmer and have specific reasons for choosing an alternative implementation.

Install the Windows Subsystem for Linux

WSL allows you to run a GNU / Linux command line environment built right into Windows and your favorite tools like Visual Studio Code, Outlook, etc.

To activate and install WSL (or WSL 2, depending on your needs), follow the steps in the WSL installation documentation. These steps include choosing a Linux distribution (such as Ubuntu).

After installing WSL and a Linux distribution, open the Linux distribution (it's located on the Windows Start menu) and check the version and code name with the command.

It is recommended that you update your Linux distribution regularly; For example, immediately after installation to ensure that you have the latest packages. This update is not processed automatically on Windows. Use the command to update the distribution.

tip

Consider installing the new version of Windows Terminal from the Microsoft Store to enable multiple tabs (quick toggle between multiple Linux command lines, Windows command prompt, PowerShell, Azure CLI, etc.), create custom keyboard shortcuts (shortcut keys to open or closing tabs, for copying and pasting etc.), to use the search function and to set up user-defined designs (color schemes, font styles and sizes, background image / blurring / transparency). additional Information

Setting up Visual Studio Code

By using Visual Studio Code, you can take advantage of IntelliSense, linting, debug support, code snippets, and unit tests. VS Code easily integrates with the Windows subsystem for Linux and offers a built-in terminal for a seamless workflow between the code editor and the command line in addition to supporting Git for version control with general Git commands (add, commit, push, pull), which are integrated directly into the user interface.

  1. Download and install VS Code for Windows. VS Code is also available for Linux. However, since the Windows subsystem for Linux does not support GUI apps, VS Code must be installed on Windows. The integration into the command line and the tools of Linux is easily possible by using the extension "Remote - WSL".

  2. Install the Remote - WSL extension in VS Code. This allows you to use WSL as an integrated development environment while processing compatibility and path mapping automatically. additional Information

Important

If you have already installed VS Code, make sure you have the May release (1.35) or later to install the Remote - WSL extension. It is not recommended to use WSL in VS Code without the Remote - WSL extension as it will not support autocomplete, debug, linting, and so on. Note: This WSL extension is installed under "$ HOME / .vscode-server / extensions".

Create a new project

We will now create a new project directory in the Linux file system (Ubuntu), in which Linux apps and Linux tools with VS Code will then be used.

  1. Close VS Code and open Ubuntu 18.04 (the WSL command line) by typing in the Start menu Enter the following (Windows symbol at the bottom left): "Ubuntu 18.04".

  2. On the Ubuntu command line, navigate to the location where you want to put the project and create a directory for the project:.

tip

When using the Windows Subsystem for Linux (WSL), it is important to note that They are now in two different file systems work: 1) in the Windows file system and 2) in the Linux file system (WSL), in this example Ubuntu. It is therefore important to pay attention to where you install packages and save files. You can install one version of a tool or package on the Windows file system and another version on the Linux file system. Updating the tool in the Windows file system does not affect the tool in the Linux file system and vice versa. With WSL, the local hard disk drives are mounted on the computer under the folder in the Linux distribution. For example, the Windows drive C: is included under. You can access the Windows files through the Ubuntu terminal and use Linux apps and tools on these files and vice versa. It is recommended that you use the Linux file system for Python web development as much of the web tools were originally written for Linux and are deployed in a Linux production environment. This also prevents the file system semantics from being mixed up (e.g., file names are not case-sensitive under Windows). However, WSL can now switch between the Linux and Windows systems so that you can host your files on both systems. additional Information

Install Python, pip, and venv

Ubuntu 18.04 LTS already has Python 3.6 installed, but it does not include some modules that you might get with other Python installations. pip, the standard package manager for Python, and venv, the standard module used to create and manage simple virtual environments, remains to be installed.

  1. Make sure Python3 is already installed by opening the Ubuntu Terminal and typing:. This should return the Python version number. If you need to update your version of Python, please update the Ubuntu version first by typing:. Then update Python using.

  2. Install pip with the following entry:. You can use pip to install and manage additional packages that are not part of the standard Python library.

  3. Install venv with the following entry:.

Create a virtual environment

Using virtual environments is recommended as a best practice for Python development projects. By creating a virtual environment, you can isolate your project tools and avoid version conflicts with tools for other projects. For example, you can manage an older web project that requires the Django 1.2 web framework and then create a new project with Django 2.2. If you update Django globally outside of a virtual environment, you may experience version issues later. With virtual environments, you can not only prevent accidental version conflicts, but also install and manage packages without administrative rights.

  1. Open the Terminal and create in the project folder HelloWorld with the command the virtual environment named .venv.

  2. Activate the virtual environment with the input. If activated successfully, (.venv) before the prompt. This gives you a stand-alone environment in which to write code and install packages. If you no longer want to use the virtual environment, activate it with the command.

tip

It is recommended to create the virtual environment in the directory in which the project is to be saved. Since each project must have a separate directory, each project also has its own virtual environment, so that no unique naming is required. It is recommended to use the name .venv to use. This name is also used by default for some tools (e.g. pipenv) if you install them directly in the project directory. Don't use the name .envas this conflicts with the environment variable definition files. In general, it is not recommended to use names without a period in front of them, as otherwise you will be constantly reminded that the directory exists. It is also recommended to use the GITIGNORE file .venv to add. (See the standard GitHub GITIGNORE template for Python for reference.) For more information on using virtual environments in VS Code, see Using Python environments in VS Code.

Open a window of the "Remote - WSL" extension

In VS Code, the (previously installed) extension "Remote - WSL" treats the Linux subsystem as a remote server. This enables you to use WSL as an integrated development environment. additional Information

  1. Open the project folder in VS Code via the Ubuntu Terminal by typing (“.” In VS Code indicates that the current folder is to be opened).

  2. Windows Defender will display a security warning. Select “Allow Access”. After opening VS Code, you should see the Remote Connection Host indicator in the lower left letting you know that the edit is under WSL: Ubuntu-04/18 he follows.

  3. Close the Ubuntu Terminal. From now on, the WSL terminal integrated in VS Code will be used.

  4. Open the WSL terminal in VS Code by pressing CTRL + ` (Backseat) or selecting view > terminal. This will open a bash command line (WSL) to the project folder path you created in the Ubuntu terminal.

Install the Microsoft Python extension

You must install all VS Code extensions for the Remote - WSL extension. Extensions that have already been installed locally in VS Code are not automatically available. additional Information

  1. Open the VS Code Extensions window by clicking CTRL + SHIFT + X Press (or navigate in the menu to view > Extensions).

  2. Enter in the field above Search for extensions in the Marketplace The following: python.

  3. Find the extension Python (ms-python.python) by Microsoftand choose the green button Install (Install).

  4. After the extension installation is complete, you will have the blue button Reload Required Select (Reload Required). This will reload VS Code and the area WSL: UBUNTU-18.04 - Installed (WSL: UBUNTU-18.04 - Installed) is displayed in the VS Code extensions window, indicating that the Python extension is installed.

Run a simple Python program

Python is an interpreted language and supports different types of interpreters (Python2, Anaconda, PyPy, etc.). In VS Code, the interpreter assigned to your project should be selected by default. If for some reason you'd like to change this interpreter, select the one currently displayed in the blue bar at the bottom of the VS Code window, or open the Command palette (CTRL + SHIFT + P) and enter the command Python: Select Interpreter a. This will bring up a list of the currently installed Python interpreters. Learn more about configuring Python environments.

We will now create a simple Python program as a test, run it and check that the correct Python interpreter is selected.

  1. Open VS Code File Explorer by clicking CTRL + SHIFT + E Press (or in the menu to view > Explorer navigate).

  2. If it isn't already open, open the built-in WSL terminal by pressing CTRL + SHIFT + ` Press and make sure the folder of the Python project HelloWorld is selected.

  3. Create a python file by typing. The file that has just been created should be displayed in the Explorer window under the ".venv" and ".vscode" folders already in the project directory.

  4. Select the file you just created test.py in the Explorer window to open it in VS Code. Since the ".py" extension in the file name in VS Code recognizes that it is a Python file, a Python interpreter is automatically selected and loaded with the previously loaded Python extension, which is then used at the bottom of the VS Code- Window is displayed.

  5. Paste the following Python code in the test.py file, then save the file (Ctrl + S).

  6. To run the Hello World Python program you just created, select the file in the VS Code Explorer window test.py and then right-click the file to display a menu of options. Choose Run Python File in Terminal (Run Python file in Terminal). Alternatively, you can enter in the integrated WSL terminal window to run the "Hello World" program. The Python interpreter outputs "Hello World" in the terminal window.

Congratulations! You can now create and run Python programs. Now let's create the Hello World app using two of the most popular Python web frameworks: Flask and Django.

Tutorial for the "Hello World" app for Flask

Flask is a web application framework for Python. In this short tutorial, you will create a small "hello world" flask app using VS Code and WSL.

  1. Open Ubuntu 18.04 (the WSL command line) by typing in the Start menu Enter the following (Windows symbol at the bottom left): "Ubuntu 18.04".

  2. Create a directory for the project:, then use to enter the directory.

  3. Create a virtual environment to install the project tools:

  4. Open the project HelloWorld flask in VS Code by entering the command.

  5. In VS Code, open the built-in WSL terminal (or bash) by pressing CTRL + SHIFT + ` to press. (The project folder HelloWorld flask should already be selected.) Close the Ubuntu command line as it will now use the WSL terminal built into VS Code.

  6. Activate the virtual environment created in step 3 through the bash terminal in VS Code:. If activation is successful, “(.venv)” should appear in front of the prompt.

  7. Install Flask in the virtual environment by typing. Verify that Flask is installed by typing.

  8. Create a new file for the Python code:

  9. Open the file app.py in the VS Code file explorer (then select the "app.py" file). This enables the Python extension so that an interpreter is selected. By default, it should Python 3.6.8 64-bit ('.venv': venv) are displayed. Notice that the virtual environment has also been recognized.

  10. In the file app.py Add code to import Flask and create an instance of the Flask object:

  11. Paste in app.py also add a function that returns the content, in this case a simple string. Use the Flask decorator app.routeto assign the URL route "/" to this function:

    tip

    You can use multiple decorators for the same function, one per line, depending on how many different routes you want to assign to the same function.

  12. Save the file app.py (CTRL + S).

  13. Run the app in the terminal by typing the following command:

    This will run the Flask development server. By default, the development server searches for app.py. When you run Flask, you should see output similar to the following:

  14. Open the rendered page in your default web browser, and then hold down the click Ctrl key to the URL http://127.0.0.1:5000/ in the terminal. The following message should appear in the browser:

  15. Note that if you go to a URL such as "/" in the debug terminal, you will see a message with the HTTP request:

  16. Quit the app by pressing CTRL + C in the terminal.

tip

If you have a filename other than app.py want to use e.g. B. program.py, define an environment variable named FLASK_APP, and set its value on the selected file. The value of FLASK_APP instead of the standard file app.py used. For more information, see the Flask Command Line Interface documentation.

Congratulations, you have created a Flask web application using Visual Studio Code and the Windows Subsystem for Linux! For a more in-depth tutorial using VS Code and Flask, see the Flask tutorial in Visual Studio Code.

Tutorial for the "Hello World" app for Django

Django is a web application framework for Python. In this short tutorial, you will create a small Hello World Django app using VS Code and WSL.

  1. Open Ubuntu 18.04 (the WSL command line) by typing in the Start menu Enter the following (Windows symbol at the bottom left): "Ubuntu 18.04".

  2. Create a directory for the project:, then use to enter the directory.

  3. Create a virtual environment to install the project tools:

  4. Open the project HelloWorld Django in VS Code by entering the command.

  5. In VS Code, open the built-in WSL terminal (or bash) by pressing CTRL + SHIFT + ` to press. (The project folder HelloWorld Django should already be selected.) Close the Ubuntu command line as it will now use the WSL terminal built into VS Code.

  6. Activate the virtual environment created in step 3 through the bash terminal in VS Code:. If activation is successful, “(.venv)” should appear in front of the prompt.

  7. Use the command to install Django in the virtual environment. Verify that Django is installed by typing.

  8. Then run the following command to build the Django project:

    The command assumes (by using at the end) that the current folder is the project folder, and in that folder it creates:

    • : The Django command line administration program for the project. Use to run administrative commands for the project.

    • A subfolder named that contains the following files:

      • : an empty file indicating to Python that the folder is a Python package.
      • : an entry point for WSGI-compatible web servers to deploy the project. You usually leave this file unchanged because it contains the hooks for production web servers.
      • : Settings for the Django project that you change while developing a web app.
      • : a table of contents for the Django project that you also change during development.
  9. To check the Django project, start the Django development server using the command. The server runs on the standard port 8000. You should see output similar to the following in the terminal window:

    The first time you run the server, a standard SQLite database is created in the file that is intended for development purposes but can be used in the production environment for low volume web apps. In addition, the integrated web server is from Django just intended for local development purposes. However, when deployed to a web host, Django uses the host's web server instead. The module in the Django project takes over the integration into the production server.

    If you want to use a port other than the default 8000, specify the port number on the command line, e.g. B.

  10. on the URL in the terminal output window to open this address in the standard browser. If Django is properly installed and the project is valid, you will see a default page. The server log is also displayed in the VS Code terminal output window.

  11. When done, close the browser window and exit the server in VS Code by pressing as indicated in the terminal output window.

  12. Now, to create a Django app, run the command of the management program in the project folder (where it is located):

    The command creates the folder that contains various code files and a subfolder. Of these code files you often use (contains the functions with which pages are defined in the web app) and (contains the classes for defining the data objects). The data versions are managed in the folder in the Django administration program (see later in this tutorial). The files (app configuration), (for creating an administration interface) and (for tests) are not discussed further here.

  13. Modify according to the following code that creates a single view for the app home page:

  14. Create the file with the following contents. In the file, you specify patterns to direct different URLs to the appropriate views. The following code provides a route to map the app's root url () to the function you just added:

  15. The folder also contains the file in which the URL routing is actually processed. Open and modify them according to the following code (the instruction comments can be left if necessary). This code is used to get the app's file using so that the app's routes are preserved within the app. This separation is useful when a project contains multiple apps.

  16. Save any changed files.

  17. In the VS Code Terminal, run the development server and open it in a browser to see the page that says “Hello, Django”.

Congratulations, you have created a Django web application using Visual Studio Code and the Windows Subsystem for Linux! For a more in-depth tutorial using VS Code and Django, see the Django tutorial in Visual Studio Code.

Additional resources

Is this page helpful?