Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to Python Tutorial v2 #5963

Merged
merged 8 commits into from
Jan 26, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 28 additions & 30 deletions docs/python/python-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ Area: python
TOCTitle: Tutorial
ContentId: 77828f36-ae45-4887-b25c-34545edd52d3
PageTitle: Get Started Tutorial for Python in Visual Studio Code
DateApproved: 8/15/2022
DateApproved: 1/20/2023
MetaDescription: A Python hello world tutorial using the Python extension in Visual Studio Code (a great Python IDE like PyCharm, if not the best Python IDE)
MetaSocialImage: images/tutorial/social.png
---
# Getting Started with Python in VS Code

In this tutorial, you use Python 3 to create the simplest Python "Hello World" application in Visual Studio Code. By using the Python extension, you make VS Code into a great lightweight Python IDE (which you may find a productive alternative to PyCharm).
In this tutorial, you will use Python 3 to create the simplest Python "Hello World" application in Visual Studio Code. By using the Python extension, you make VS Code into a great lightweight Python IDE (which you may find a productive alternative to PyCharm).

This tutorial introduces you to VS Code as a Python environment, primarily how to edit, run, and debug code through the following tasks:
This tutorial introduces you to VS Code as a Python environment - primarily how to edit, run, and debug code through the following tasks:

- Write, run, and debug a Python "Hello World" Application
- Learn how to install packages by creating Python virtual environments
Expand Down Expand Up @@ -45,7 +45,7 @@ Along with the Python extension, you need to install a Python interpreter. Which

Install [Python from python.org](https://www.python.org/downloads/). You can typically use the **Download Python** button that appears first on the page to download the latest version.

>**Note**: If you don't have admin access, an additional option for installing Python on Windows is to use the Microsoft Store. The Microsoft Store provides installs of [Python 3.7](https://www.microsoft.com/p/python-37/9nj46sx7x90p), [Python 3.8](https://www.microsoft.com/p/python-38/9mssztt1n39l), [Python 3.9](https://www.microsoft.com/p/python-39/9p7qfqmjrfp7), and [Python 3.10](https://www.microsoft.com/p/python-310/9pjpw5ldxlz5).
>**Note**: If you don't have admin access, an additional option for installing Python on Windows is to use the Microsoft Store. The Microsoft Store provides installs of [supported Python versions](https://apps.microsoft.com/store/search?hl=en-us&gl=us&publisher=Python%20Software%20Foundation).
gregvanl marked this conversation as resolved.
Show resolved Hide resolved

For additional information about using Python on Windows, see [Using Python on Windows at Python.org](https://docs.python.org/3.9/using/windows.html)

Expand Down Expand Up @@ -85,7 +85,9 @@ If the installation was successful, the output window should show the version of

> **Note** You can use the `py -0` command in the VS Code integrated terminal to view the versions of python installed on your machine. The default interpreter is identified by an asterisk (*).

## Start VS Code in a project (workspace) folder
## Start VS Code in a workspace folder

By starting VS Code in a folder, that folder becomes your "workspace". VS Code stores settings that are specific to that workspace in `.vscode/settings.json`, which are separate from user settings that are stored globally.

Using a command prompt or terminal, create an empty folder called "hello", navigate into it, and open VS Code (`code`) in that folder (`.`) by entering the following commands:

Expand All @@ -97,13 +99,12 @@ code .

>**Note**: If you're using an Anaconda distribution, be sure to use an Anaconda command prompt.

By starting VS Code in a folder, that folder becomes your "workspace". VS Code stores settings that are specific to that workspace in `.vscode/settings.json`, which are separate from user settings that are stored globally.

Alternately, you can run VS Code through the operating system UI, then use **File > Open Folder** to open the project folder.

## Select a Python interpreter

Python is an interpreted language, and in order to run Python code and get Python IntelliSense, you must tell VS Code which interpreter to use.
Python is an interpreted language. Thus, in order to run Python code and get Python IntelliSense, you must tell VS Code which interpreter to use.

From within VS Code, select a Python 3 interpreter by opening the **Command Palette** (`kb(workbench.action.showCommands)`), start typing the **Python: Select Interpreter** command to search, then select the command. You can also use the **Select Python Environment** option on the Status Bar if available (it may already show a selected interpreter, too):

Expand Down Expand Up @@ -252,36 +253,33 @@ A best practice among Python developers is to avoid installing packages into a g

> **Note**: For additional information about virtual environments, see [Environments](/docs/python/environments.md#creating-environments).

1. Create and activate the virtual environment
1. Create a Virtual Environment using the Create Environment Command

**Virtual environment creation for Windows**
From within VS Code, you can create non-global environments, using Venv or Anaconda, by opening the Command Palette (Ctrl+Shift+P), start typing the `Python: Create Environment` command to search, and then select the command. You can also trigger the `Python: Create Environment' command through the Getting Started with Python page.
gregvanl marked this conversation as resolved.
Show resolved Hide resolved

```bat
py -3 -m venv .venv
.venv\scripts\activate
```
The command presents a list of environment types, Venv or Conda.
cwebster-99 marked this conversation as resolved.
Show resolved Hide resolved

If the activate command generates the message "Activate.ps1 is not digitally signed. You cannot run this script on the
current system.", then you need to temporarily change the PowerShell execution policy to allow scripts to
run (see [About Execution Policies](https://go.microsoft.com/fwlink/?LinkID=135170) in the PowerShell documentation):
![Create Environment dropdown](images/environments/create_environment_dropdown.png)

```powershell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
```
If you are creating an environment using **Venv**, the command presents a list of interpreters that can be used for your project.
cwebster-99 marked this conversation as resolved.
Show resolved Hide resolved

**Virtual environment creation for macOS/Linux**
![Virtual environment interpreter selection](images/environments/interpreters-list.png)

```bash
python3 -m venv .venv
source .venv/bin/activate
```
>**Note**: When you create a new virtual environment, you should be prompted by VS Code to set it as the default for your workspace folder. If selected, the environment will automatically be activated when you open a new terminal.
If you are creating an environment using **Conda**, the command presents a list of Python versions that can be used for your project.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is a tutorial ,we can be opinionated and ask them to make a choice. For simplicity sake (and because this is a general script tutorial), I think we could suggest users to just choose venv and then for the rest of tutorial, assume that is what they're using

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to reflect this suggestion

cwebster-99 marked this conversation as resolved.
Show resolved Hide resolved

![Conda environment Python version selection](images/environments/conda_environment_python_versions.png)
cwebster-99 marked this conversation as resolved.
Show resolved Hide resolved

After selecting the desired interpreter or Python version, a notification will show the progress of the environment creation and the environment folder will appear in your workspace.
cwebster-99 marked this conversation as resolved.
Show resolved Hide resolved

![Create environment status notification](images/environments/create_environment_prompt_status.png)

The command will also install necessary packages outlined in a requirements/dependencies file, such as requirements.txt, pyproject.toml or environment.yml, located in the project folder.
gregvanl marked this conversation as resolved.
Show resolved Hide resolved

![Virtual environment dialog](images/tutorial/virtual-env-dialog.png)
> **Note**: If you want to create an environment manually, or run into error in the environment creation process, visit the [Environments](/docs/python/environments.md#create-a-virtual-environment-in-the-terminal) page.

1. Ensure your new environment is selected by using the **Python: Select Interpreter** command from the **Command Palette**.

1. Select your new environment by using the **Python: Select Interpreter** command from the **Command Palette**.
![Select an Interpreter](images/tutorial/interpreter-venv.png)
![Select an Interpreter](images/tutorial/interpreter-venv.png)
1. Install the packages

```bash
Expand Down Expand Up @@ -324,5 +322,5 @@ There is then much more to explore with Python in Visual Studio Code:
- [Debugging](/docs/python/debugging.md) - Learn to debug Python both locally and remotely.
- [Testing](/docs/python/testing.md) - Configure test environments and discover, run, and debug tests.
- [Settings reference](/docs/python/settings-reference.md) - Explore the full range of Python-related settings in VS Code.
- [Deploy Python to Azure App Service using containers](https://learn.microsoft.com/azure/developer/python/tutorial-deploy-containers-01)
- [Deploy Python to Azure App Service on Linux](https://learn.microsoft.com/azure/developer/python/configure-python-web-app-local-environment)
- [Deploy Python to Azure App Service](https://learn.microsoft.com/en-us/azure/developer/python/tutorial-containerize-deploy-python-web-app-azure-01)
gregvanl marked this conversation as resolved.
Show resolved Hide resolved
- [Deploy Python to Container Apps](https://learn.microsoft.com/en-us/azure/developer/python/tutorial-deploy-python-web-app-azure-container-apps-01)