Skip to content

Commit

Permalink
Merge pull request #10006 from MicrosoftDocs/main638423213196844723sy…
Browse files Browse the repository at this point in the history
…nc_temp

For protected branch, push strategy should use PR and merge to target branch method to work around git push error
  • Loading branch information
learn-build-service-prod[bot] authored Jan 31, 2024
2 parents 1070bcc + 4f972c1 commit 55aaa9c
Show file tree
Hide file tree
Showing 16 changed files with 283 additions and 206 deletions.
77 changes: 54 additions & 23 deletions docs/python/formatting-python-code.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,85 @@
---
title: Reformat your python code in Visual Studio
description: Automatically format your Python code in Visual Studio, including the code spacing, statements, long line wrapping, and code comments.
ms.date: 08/11/2023
ms.topic: conceptual
author: cwebster-99
ms.author: cowebster
manager: jmartens
ms.subservice: python
ms.topic: conceptual
ms.date: 01/25/2024
manager: jmartens

# CustomerIntent: As a developer, I want to reformat Python code in Visual Studio so I can adjust settings like code spacing and line wrapping.
---

# Automatically reformat Python code in Visual Studio

Visual Studio lets you quickly reformat code to match specific formatter standards.
Visual Studio lets you quickly reformat code to match specific formatter standards. In this article, you explore how to access and enable formatting features.

## Choose a formatter

You can set your source code formatter through **Tools** > **Options** > **Text Editor** > **Python** > **Formatting**. Python Tools in Visual Studio support source code formatting with autopep8, Black formatter, and yapf.

![Python formatting options in Visual Studio](media/options-editor-formatting.png)
:::image type="content" source="media/options-editor-formatting.png" alt-text="Screenshot of the Python formatting options in Visual Studio and the Black formatter option selected." lightbox="media/options-editor-formatting.png":::

[Python support in Visual Studio](installing-python-support-in-visual-studio.md) also adds the useful [**Fill Comment Paragraph**] command to the **Edit** > **Advanced** menu as described in a later section.
[Python support in Visual Studio](installing-python-support-in-visual-studio.md) also adds the **Fill Comment Paragraph** command to the **Edit** > **Advanced** menu as described in a later section.

## Apply format to selection or file

To format a selection:
You can apply formatting settings to all content in a file or only to a specific selection.

To format a selection, use one of the following methods:

- Select **Edit** > **Advanced** > **Format Selection**
- Or, press **Ctrl**+**E** > **F**.
- Select **Edit** > **Advanced** > **Format Selection**.
- Select the keyboard shortcut **Ctrl**+**E**+**F**.

To format the whole file:
To format the whole file, use one of the following methods:

- Select **Edit** > **Advanced** > **Format Document**
- Or, press **Ctrl**+**E** > **D**.
- Select **Edit** > **Advanced** > **Format Document**.
- Select the keyboard shortcut **Ctrl**+**E**+**D**.

## Word wrap

You can enable word wrapping in **Tools** > **Options** > **Text Editor** > **Python** > **Formatting** > **General**.
You can enable word wrapping in **Tools** > **Options** > **Text Editor** > **Python** > **General**. In the **Settings** section, select the **Word wrap** checkbox.

![Enable word wrap for Python](media/word-wrap-setting.png)
:::image type="content" source="media/word-wrap-setting.png" alt-text="Screenshot that shows how to enable word wrapping for Python in Visual Studio." lightbox="media/word-wrap-setting.png":::

## Format comment text

**Edit** > **Advanced** > **Fill Comment Paragraph** (**Ctrl**+**E** > **P**) reflows and formats comment text, combining short lines together and breaking up long ones.
The **Edit** > **Advanced** > **Fill Comment Paragraph** option reflows and formats comment text. You can also access the feature by using the keyboard shortcut **Ctrl**+**E**+**P**.

### Reformat long lines

Use the feature to break up long lines of text, as shown in this example:

```python
# This is a very long long long long long long long long long long long long long long long long long long long comment
```

The text is reformatted as multiple lines:

```python
# This is a very long long long long long long long long long long long long
# long long long long long long long comment
```

### Combine short lines

Use the feature to combine short lines of text, as shown in this example:

```python
# Short line of text
# more text
# text
```

The text is reformatted into a single line:

```python
# Short line of text more text text
```

| Reformat | Example 1 |
| :------- | :-------------------------------------------------------------------------------------------------------------------------------- |
| Before | `# This is a very long long long long long long long long long long long long long long long long long long long comment` |
| After | `# This is a very long long long long long long long long long long long long`<br/>`# long long long long long long long comment` |
## Related content

| Reformat | Example 2 |
| :------- | :------------------------------ |
| Before | `# foo`<br/>`# bar`<br/>`# baz` |
| After | `# foo bar baz` |
- [Edit Python code](editing-python-code-in-visual-studio.md)
- [Refactor Python code](refactoring-python-code.md)
- [Lint Python code](linting-python-code.md)
29 changes: 19 additions & 10 deletions docs/python/includes/project-from-existing.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
---
ms.date: 01/25/2024
ms.topic: include
---

Follow these steps to create a project from existing files.

> [!IMPORTANT]
> The following process doesn't move or copy any original source files.
> If you want to work with a copy of your files, first duplicate the folder and then create the project.
1. Launch Visual Studio and select **File** > **New** > **Project**.

1. In the **New Project** dialog, search for "Python", select the **From Existing Python code** template, give the project a name and location, and select **OK**.
1. In the **Create a new project** dialog, search for _python_, and select the **From Existing Python code** template. Enter a project name and location, choose the solution to contain the project, and select **Create**.

1. In the wizard that appears, set the path to your existing code, set a filter for file types, and specify any search paths that your project requires, then select **Next**. If you don't know what search paths are, leave that field blank.
1. In the **Create New Project from Existing Python Code** wizard, set the folder path to your existing code, set a filter for file types, and specify any search paths that your project requires, then select **Next**. If you don't know the search paths, leave the field blank.

:::image type="content" source="../media/projects-from-existing-1.png" alt-text="Screenshot of a New Project creation from Existing Code window, step 1.":::
:::image type="content" source="../media/projects-from-existing-1.png" alt-text="Screenshot of a New Project creation from Existing Code, including choosing the folder path, file type filters, and search paths." lightbox="../media/projects-from-existing-1.png":::

1. In the next dialog, select the startup file for your project and select **Next**. If necessary, select an environment; otherwise accept the defaults.
1. On the next page, select the startup file for your project. Visual Studio selects the default global Python interpreter and version. You can change the environment by using the dropdown menu. When you're ready, select **Next**.

> [!Note]
> The dialog shows only files in the root folder; if the file you want is in a subfolder, leave the startup file blank and set it later in **Solution Explorer** (described next).
> The dialog shows only files in the root folder. If the file you want is in a subfolder, leave the startup file blank. You can set the startup file later in **Solution Explorer**, as described in a later step.
:::image type="content" source="../media/projects-from-existing-2.png" alt-text="Screenshot of New Project Creation from Existing Code window, including choosing the startup file and Python environment." lightbox="../media/projects-from-existing-2.png":::

:::image type="content" source="../media/projects-from-existing-2.png" alt-text="Screenshot of New Project Creation from Existing Code window, step 2.":::
1. Select the location where you want to save the project file (a _.pyproj_ file on disk). If applicable, you can also include autodetection of virtual environments and customize the project for different web frameworks. If you're unsure of these options, leave the fields set to the defaults.

1. Select the location in which to save the project file (which is a `.pyproj` file on disk). If applicable, you can also include auto-detection of virtual environments and customize the project for different web frameworks. If you're unsure of these options, leave them set to the defaults.
:::image type="content" source="../media/projects-from-existing-3.png" alt-text="Screenshot of a New Project creation from Existing Code window, including the save location for the project file and other optional settings." lightbox="../media/projects-from-existing-3.png" :::

:::image type="content" source="../media/projects-from-existing-3.png" alt-text="Screenshot of a New Project creation from Existing Code window, step 3.":::
1. Select **Finish**.

1. Select **Finish** and Visual Studio creates the project and opens it in **Solution Explorer**. If you want to move the `.pyproj` file elsewhere, select it in **Solution Explorer** and choose **File** > **Save As**. This action updates file references in the project but doesn't move any code files.
Visual Studio creates the project and opens it in **Solution Explorer**. If you want to move the _.pyproj_ file to a different location, select the file in **Solution Explorer**, and then select **File** > **Save As** on the toolbar. This action updates file references in the project, but it doesn't move any code files.

1. To set a different startup file, locate the file in **Solution Explorer**, right-click on the file, and select **Set as Startup File**.
1. To set a different startup file, locate the file in **Solution Explorer**, right-click the file, and select **Set as Startup File**.
84 changes: 49 additions & 35 deletions docs/python/installing-python-support-in-visual-studio.md
Original file line number Diff line number Diff line change
@@ -1,74 +1,88 @@
---
title: Install Python Tools for Visual Studio (PTVS)
description: Install the Python Tools for Visual Studio (PTVS) in Visual Studio on Windows and check your installation in the Python Interactive window.
ms.date: 6/5/2023
ms.topic: how-to
author: cwebster-99
ms.author: cowebster
manager: jmartens
ms.subservice: python
ms.topic: how-to
ms.date: 01/25/2024
manager: jmartens

# CustomerIntent: As a developer, I want to install Python support in Visual Studio so that I can access Python features in the product.

---

# Install Python support in Visual Studio

Python support is available only on Visual Studio for Windows. On Mac and Linux, Python support is available through [Visual Studio Code](https://code.visualstudio.com/docs/python/python-tutorial).

## Download & install the Python workload
## Prerequisites

- Visual Studio on Windows. To install the product, follow the steps in [Install Visual Studio](../install/install-visual-studio.md).

1. Download and run the latest Visual Studio installer for Windows. Python support is present in the release 15.2 and later. If you have Visual Studio installed already, open Visual Studio and run the installer by selecting **Tools** > **Get Tools and Features**.
## Download and install the Python workload

> [!div class="nextstepaction"]
> [Install Visual Studio Community](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Community&rel=15&rid=34347&utm_source=docs&utm_medium=clickbutton&utm_campaign=python_gettingstarted)
Complete the following steps to download and install the Python workload.

>[!Tip]
> The Community edition is for individual developers, classroom learning, academic research, and open source development. For other uses, install [Visual Studio Professional](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Professional&rel=15&rid=34347&utm_source=docs&utm_medium=clickbutton&utm_campaign=python_gettingstarted) or [Visual Studio Enterprise](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Enterprise&rel=15&rid=34347&utm_source=docs&utm_medium=clickbutton&utm_campaign=python_gettingstarted).
1. Download and run the latest Visual Studio Installer for Windows. Python support is present in release 15.2 and later. If you have Visual Studio installed already, open Visual Studio and run the installer by selecting **Tools** > **Get Tools and Features**.

1. The installer provides a list of workloads that are groups of related options for specific development areas.
For Python, select the **Python development** workload and select **Install**:
> [!div class="nextstepaction"]
> [Install Visual Studio Community](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Community&rel=15&rid=34347&utm_source=docs&utm_medium=clickbutton&utm_campaign=python_gettingstarted)
![Screenshot of the Python development workload selected in the Visual Studio installer.](media/installation-python-workload.png)
> [!Tip]
> The Community edition is for individual developers, classroom learning, academic research, and open source development. For other uses, install [Visual Studio Professional](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Professional&rel=15&rid=34347&utm_source=docs&utm_medium=clickbutton&utm_campaign=python_gettingstarted) or [Visual Studio Enterprise](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Enterprise&rel=15&rid=34347&utm_source=docs&utm_medium=clickbutton&utm_campaign=python_gettingstarted).
| Python&nbsp;installation&nbsp;options| Description |
1. The installer provides a list of workloads that are groups of related options for specific development areas. For Python, select the **Python development** workload and select **Install**:

:::image type="content" source="media/installation-python-workload.png" alt-text="Screenshot of the Python development workload selected in the Visual Studio installer." lightbox="media/installation-python-workload.png":::

| Python installation options| Description |
| --- | --- |
| Python distributions | Choose any combination of the available options, such as 32-bit and 64-bit variants of the Python 2, Python 3, Miniconda, Anaconda2, and Anaconda3 distributions that you plan to work with. Each includes the distribution's interpreter, runtime, and libraries. Anaconda, specifically, is an open data science platform that includes a wide range of pre-installed packages. Visual Studio automatically detects existing Python installations. See [The Python Environments window](managing-python-environments-in-visual-studio.md#the-python-environments-window). Also, if a newer version of Python is available than what's shown in the installer, you can install that version separately and Visual Studio will detect it. |
| **Cookiecutter template support** | Installs the Cookiecutter graphical UI to discover templates, input template options, and create projects and files. See [Use the Cookiecutter extension](using-python-cookiecutter-templates.md). |
| **Python web support** | Installs tools for web development including HTML, CSS, and JavaScript editing support, along with templates for projects using the Bottle, Flask, and Django frameworks. See [Python web project templates](python-web-application-project-templates.md). |
| **Python native development tools** | Installs the C++ compiler and other necessary components to develop native extensions for Python. See [Create a C++ extension for Python](working-with-c-cpp-python-in-visual-studio.md). Also install the **Desktop development with C++** workload for full C++ support. |
| **Python distributions** | Choose any combination of Python distribution that you plan to work with. Common options include 32-bit and 64-bit variants of Python 2, Python 3, Miniconda, Anaconda 2, and Anaconda 3. Each option includes the distribution's interpreter, runtime, and libraries. Anaconda, specifically, is an open data science platform that includes a wide range of preinstalled packages. Visual Studio automatically detects existing Python installations. For more information, see [The Python Environments window](managing-python-environments-in-visual-studio.md#the-python-environments-window). Also, if a newer version of Python is available than the version shown in the installer, you can install the new version separately and Visual Studio detects it. |
| **Cookiecutter template support** | Install the Cookiecutter graphical UI to discover templates, input template options, and create projects and files. For more information, see [Use the Cookiecutter extension](using-python-cookiecutter-templates.md). |
| **Python web support** | Install tools for web development including HTML, CSS, and JavaScript editing support, along with templates for projects using the Bottle, Flask, and Django frameworks. For more information, see [Python web project templates](python-web-application-project-templates.md). |
| **Python native development tools** | Install the C++ compiler and other necessary components to develop native extensions for Python. For more information, see [Create a C++ extension for Python](working-with-c-cpp-python-in-visual-studio.md). Also install the **Desktop development with C++** workload for full C++ support. |

By default, the Python workload gets installed for all users on a computer under:
By default, the Python workload installs for all users on a computer under:

:::moniker range=">=vs-2022"

```txt
*%ProgramFiles%\Microsoft Visual Studio\\<VS_version>\\<VS_edition>Common7\IDE\Extensions\Microsoft\Python*
```
_%ProgramFiles%\Microsoft Visual Studio\\<VS_version>\\<VS_edition>Common7\IDE\Extensions\Microsoft\Python_

where `<VS_version>` is 2022 and `<VS_edition>` is Community, Professional, or Enterprise.
where _\<VS_version>_ is 2022 and _\<VS_edition>_ is Community, Professional, or Enterprise.

:::moniker-end
:::moniker range="<=vs-2019"

```txt
*%ProgramFiles(x86)%\Microsoft Visual Studio\\<VS_version>\\<VS_edition>Common7\IDE\Extensions\Microsoft\Python*
```
_%ProgramFiles(x86)%\Microsoft Visual Studio\\<VS_version>\\<VS_edition>Common7\IDE\Extensions\Microsoft\Python_

where `<VS_version>` is 2019 or 2017 and `<VS_edition>` is Community, Professional, or Enterprise.
where _\<VS_version>_ is 2019 or 2017 and _\<VS_edition>_ is Community, Professional, or Enterprise.

:::moniker-end

## Test your install

To quickly test Python support:
Quickly check your installation of Python support:

1. Launch Visual Studio.

1. Select Alt + I to open the **Python Interactive** window.

1. In the window, enter the statement `2+2`.

The statement output `4` displays in the window. If you don't see the correct output, recheck your steps.

::: moniker range="<=vs-2019"

:::image type="content" source="media/installation-interactive-test.png" alt-text="Screenshot that shows a test of Python support through the interactive window." lightbox="media/installation-interactive-test.png":::

::: moniker-end

1. Launch Visual Studio
2. Press **Alt**+**I** to open the **Python Interactive** window and enter `2+2`. If you don't see the output of **4**, recheck your steps.
::: moniker range=">=vs-2022"

::: moniker range="<=vs-2019"
![Screenshot of testing Python through the interactive window.](media/installation-interactive-test.png)
::: moniker-end
:::image type="content" source="media/vs-2022/python-interactive.png" alt-text="Screenshot that shows a test of Python support through the Visual Studio 2022 interactive window." lightbox="media/vs-2022/python-interactive.png":::

::: moniker range=">=vs-2022"
![Screenshot of testing Python through the Visual Studio 2022 interactive window.](media/vs-2022/python-interactive.png)
::: moniker-end
::: moniker-end

## Related content

Expand Down
Loading

0 comments on commit 55aaa9c

Please sign in to comment.