If you want to use a different port than the default 8000, specify the port number on the command line, such as python3 manage.py runserver 5000. If it worked, you should see (.venv) before the command prompt. Work fast with our official CLI. Set up your development environment. You now have a self-contained environment ready for writing code and installing packages. It also avoids mixing file system semantics (like Windows being case-insensitive regarding file names). 4. The migrations folder is used by Django's administrative utility to manage database versions as discussed later in this tutorial. Create a new file for your Python code: touch app.py. For some advanced scenarios, you may want to consider downloading a specific Python release directly from python.org or consider installing an alternative, such as Anaconda, Jython, PyPy, WinPython, IronPython, etc. If you're interested in automating common tasks on your operating system, see our guide: Get started using Python on Windows for scripting and automation. In this brief tutorial, you'll create a small "Hello World" Flask app using VS Code and WSL. Open Ubuntu 18.04 (your WSL command line) by going to your Start menu (lower left Windows icon) and typing: "Ubuntu 18.04". Open your HelloWorld-Flask project in VS Code by entering the command: code . Since each project should have its own separate directory, each will have its own virtual environment, so there is not a need for unique naming. The WSL, in its current incarnation, is a translation layer that converts Linux system calls into Windows system calls. If you have a reason to change it, select the interpreter currently displayed in blue bar on the bottom of your VS Code window or open the Command Palette (Ctrl+Shift+P) and enter the command Python: Select Interpreter. Visual Studio Code Remote Development: Open any folder in WSL, in a Docker container, or on a remote machine using SSH and take advantage of VS Code's full feature set. Install venv by entering: sudo apt install python3-venv. For more information, see Flask's Command Line Interface documentation. We do not recommend using WSL in VS Code without the Remote-WSL extension as you will lose support for auto-complete, debugging, linting, etc. We also recommend adding .venv to your .gitignore file. To update your distribution, use the command: sudo apt update && sudo apt upgrade. In the terminal, run the app by entering the following command: This runs the Flask development server. Install Flask in the virtual environment by entering: python3 -m pip install flask. VS Code Remote Development extension "stable" releases are tied directly to VS Code releases. If it worked, you should see (.venv) before the command prompt. Learn more. Most web apps are also deployed on Linux, so this will ensure you have consistency between your development and production environments. In addition to preventing accidental versioning conflicts, virtual environments let you install and manage packages without administrative privileges. This repository is for providing feedback on the Visual Studio Remote Development extension pack and its related extensions. That said, WSL now supports jumping between the Linux and Windows files systems, so you can host your files on either one. Linux runs on top of the WSL. Create a directory for your project: mkdir HelloWorld-Flask, then cd HelloWorld-Flask to enter the directory. Here's a PSA for those wanting to install MySQL Server 8.0 under the new Windows Subsystem for Linux - WSL 2, with an Ubuntu distribution. And with VS Code’s Remote development extension, the whole thing is very easy to set up. We recommend creating the virtual environment inside the directory in which you plan to have your project. Pip allows you to install and manage additional packages that are not part of the Python standard library. Some tools (like pipenv) also default to this name if you install into your project directory. Learn more about configuring Python environments. localhost accesss made WSL so much easier to use compared to the traditional development vm. Go to the end of the file using arrow key. You can install one version of a tool or package in the Windows file system and a completely different version in the Linux file system. But what exactly is this mad science? Our suggestion is to use the name .venv to follow the Python convention. Paste this Python code into your test.py file and then save the file (Ctrl+S): To run the Python "Hello World" program that we just created, select the test.py file in the VS Code Explorer window, then right-click the file to display a menu of options. Install and use Python directly on Windows for these cases. tells VS Code to open the current folder). You will need to install any VS Code extensions for your Remote - WSL. Now let's try creating a Hello World app with two of the most popular Python web frameworks: Flask and Django. You don't want to use .env as that conflicts with environment variable definition files. For more information about working with virtual environments in VS Code, see Using Python environments in VS Code. Create a python file by entering: touch test.py. Select the test.py file that you just created in your Explorer window to open it in VS Code. A newer version means several bugfixes. Congratulations, you've created a Django web application using VS Code and Windows Subsystem for Linux! Ctrl+click the http://127.0.0.1:8000/ URL in the terminal output window to open your default browser to that address. Pengwin is the first Linux distribution created specifically for WSL. When you're finished with your virtual environment, enter the following command to deactivate it: deactivate. Select Run Python File in Terminal. We recommend updating your Linux distribution regularly, including immediately after you install, to ensure you have the most recent packages. You can access your Windows files from the Ubuntu terminal and use Linux apps and tools on those files and vice-versa. A subfolder named web_project, which contains the following files: To verify the Django project, start Django's development server using the command python3 manage.py runserver. The server runs on the default port 8000, and you should see output like the following output in the terminal window: When you run the server the first time, it creates a default SQLite database in the file db.sqlite3, which is intended for development purposes, but can be used in production for low-volume web apps. Create a directory for your project: mkdir HelloWorld-Django, then cd HelloWorld-Django to enter the directory. A Security Alert will pop-up from Windows Defender, select "Allow access". Release highlights can be found in VS Code release notes which will include a link to detailed extension release notes. If you have a question, connect with the community using any of these social platforms: See our CONTRIBUTING guide for more details. Press i to go into edit mode. Windows doesn't automatically handle this update. Verify that it's installed by entering: python3 -m flask --version. For a more in-depth tutorial using VS Code and Flask, see Flask Tutorial in Visual Studio Code. Learn more. If you're new to Python, see our guide: Get started using Python on Windows for beginners. The wsgi.py module in the Django project takes care of hooking into the production servers. Flask is a web application framework for Python. If Django is installed correctly and the project is valid, you'll see a default page. This allows you to use WSL as your integrated development environment. You can reference our summary of tips for remote related issues and our extension guide to help the extension author get started. In your Ubuntu command line, navigate to where you want to put your project, and create a directory for it: mkdir HelloWorld. This will display a list of the Python interpreters that you currently have installed. If you need to update your version of Python, first update your Ubuntu version by entering: sudo apt update && sudo apt upgrade, then update Python using sudo apt upgrade python3. Open your project folder in VS Code from your Ubuntu terminal by entering: code . Let's create and run a simple Python program as a test and ensure that we have the correct Python interpreter selected. This allows you to use WSL as your integrated development environment and will handle compatibility and pathing for you. Flask's development server then uses the value of FLASK_APP instead of the default file app.py. Once you have installed WSL and a Linux distribution, open the Linux distribution (it can be found in your Windows start menu) and check the version and codename using the command: lsb_release -dc. Confirm that Python3 is already installed by opening your Ubuntu terminal and entering: python3 --version. When you run Flask, you should see output similar to the following: Open your default web browser to the rendered page, Ctrl+Click the http://127.0.0.1:5000/ URL in the terminal. Learn about updates coming soon with WSL 2! If nothing happens, download the GitHub extension for Visual Studio and try again. Use Flask's app.route decorator to map the URL route "/" to that function: You can use multiple decorators on the same function, one per line, depending on how many different routes you want to map to the same function. I'm trying to change it to something else. To enable and install WSL (or WSL 2 depending on your needs), follow the steps in the WSL install documentation. Learn more. VS Code should default to the interpreter associated with your project. Learn more. The following is a step-by-step guide to get you started using Python for web development on Windows, using the Windows Subsystem for Linux (WSL). 2:45 - Deploying WSL In The Enterprise, Hayden Barnes and Sohini Roy. Ubuntu 18.04 LTS comes with Python 3.6 already installed, but it does not come with some of the modules that you may expect to get with other Python installations. WSL lets you run a GNU/Linux command line environment integrated directly with Windows and your favorite tools, like Visual Studio Code, Outlook, etc. Modify hello/views.py to match the following code, which creates a single view for the app's home page: Create a file, hello/urls.py, with the contents below. Now, to create a Django app, run the administrative utility's startapp command in your project folder (where manage.py resides): The command creates a folder called hello that contains a number of code files and one subfolder. Purchases through the Microsoft Store employ developers to add features, test builds, provide user support, and patch security bugs. I found just stopping the Windows version was enough to enable the WSL Ubuntu version to run rather than having to uninstall the Windows version completely.