-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merging changes synced from https://github.com/MicrosoftDocs/visualst…
…udio-docs-pr (branch live)
- Loading branch information
Showing
16 changed files
with
283 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
84
docs/python/installing-python-support-in-visual-studio.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.