在 Visual Studio 中重構 Python 程式代碼

重複使用現有的程式代碼和重新整理程式代碼是開發人員的常見工作。 您可能想要將現有的程式代碼重構為另一個用途,並藉由不撰寫所有新程式代碼來節省時間。 您可能想要清除程式代碼以移除未使用的專案,或重新整理匯入和方法,使其與最新版本保持最新狀態。

Visual Studio 提供數個命令,可協助您自動轉換及清除 Python 原始碼:

必要條件

  • Visual Studio。 若要安裝產品,請遵循安裝 Visual Studio 中的步驟。
  • 使用現有程式代碼存取 Python 程式代碼專案。

重新命名類別、方法或變數

您可以使用 Rename 命令來變更特定識別碼的名稱,包括類別、方法或變數。 Visual Studio 支援更新標識碼的所有實例,或只更新您指出的特定實例。

下列步驟示範如何在程序代碼中使用 Rename 命令。

  1. 在您的程式代碼中,以滑鼠右鍵按下您要重新命名的標識碼,然後選取 [ 重新命名]。 您也可以將插入號放在標識符上,然後從功能表中選取 [編輯>重構>重新命名],或使用鍵盤快捷方式為 F2。

  2. 在 [ 重新命名 ] 對話框中,輸入標識符的新名稱,然後選取 [ 確定]:

    Screenshot that shows how to specify a different name for an identifier in Visual Studio 2019.

  3. 在下一個對話框中,選取程式代碼中應該使用新名稱的檔案和實例。 您可以選取個別實例來預覽特定變更:

    Screenshot that shows how to apply the new name to specific instances of an identifier and preview the changes in Visual Studio 2019.

  4. 選取 [套用] 以變更您的原始程式碼檔。

  1. 在您的程式代碼中,以滑鼠右鍵按下您要重新命名的標識碼,然後選取 [ 重新命名]。 您也可以將插入號放在標識符上,然後從功能表中選取 [編輯>重構>重新命名],或使用鍵盤快捷方式 Ctrl+R。

  2. 在 [ 重新命名 ] 對話框中,輸入標識元的新名稱, 後面接著 Enter

    Screenshot that shows how to specify a different name for an identifier in Visual Studio 2022.

新增匯入語句

如果您的程式代碼中有遺漏定義或支援類型資訊的標識碼,Visual Studio 可協助您修正問題。 當您將插入號放在缺少資訊的標識碼上時,Visual Studio 會在程式碼左邊顯示智慧標記(燈泡)。 標記會列出命令,以新增對應標識符的必要 importfrom ... import 語句。

下列步驟示範如何使用智慧標記,在程式代碼中新增匯入。

  1. 在您的程式代碼中,將插入號放在Visual Studio顯示智慧標記的標識碼上(燈泡)。 在這裡範例中,智慧標記會顯示數學模組的呼叫:

    Screenshot that shows the smart tag for an identifier that needs an import statement added in Visual Studio 2019.

    Screenshot that shows the smart tag for an identifier that needs an import statement added in Visual Studio 2022.

  2. 在智慧標記功能表上,選取 命令,將必要的模組或類型資訊新增至程式碼檔案。 在此範例中,會選取要加入 語句的 import math 命令。

    Visual Studio 為最上層的套件和目前專案和標準程式庫中的模組,提供 import 完成。 Visual Studio 也提供 from ... import 子模組和子套件和模組成員的完成。 完成包括函式、類別或匯出的資料。

  3. 選取選項之後,請確認對檔案進行預期的變更。

    Visual Studio 會在 import 其他匯入之後,於程式代碼檔案頂端新增 語句,如果已匯入相同的模組,則新增至現有的 from ... import 語句。 在此範例中, import math 語句會在其他匯入之後新增至檔案頂端:

    Screenshot that shows the import statement added after running the command from the smart tag in Visual Studio 2019.

    Screenshot that shows the import statement added after running the command from the smart tag in Visual Studio 2022.

Visual Studio 會嘗試篩選出模組中未定義的成員。 例如,當模組匯入另一個模組時,不是匯入模組的子系。 許多模組都會使用 import sys 語句,而不是 from xyz import sys。 即使模組缺少__all__排除模組的成員sys,您也不會看到從其他模組匯入模組的完成sys

同樣地,Visual Studio 會篩選從其他模組或內建命名空間匯入的函式。 如果模組從sys模組匯入settrace函式,理論上您可以從該模組匯入函式。 不過,最好的方法是直接使用 import settrace from sys 語句,因此Visual Studio特別提供該語句。

最後,假設模組通常已排除,但它有其他值,如在模組中指派值指派的名稱一樣。 Visual Studio 仍然會排除匯入。 此行為假設不應該匯出值,因為另一個模組會定義該值。 另一個指派可能是非導出的虛擬值。

移除未使用的匯入

當您撰寫程式代碼時,很容易最後就會有 import 未使用之模組的 語句。 因為 Visual Studio 會分析您的程式代碼,所以查看匯入的名稱是否在語句發生之後使用範圍,即可自動判斷 import 是否需要語句。

下列步驟示範如何移除程序代碼中未使用的匯入。

  1. 在編輯器中的任何位置按下滑鼠右鍵,然後選取 [ 移除匯入]。 您可以從 [所有範圍] 或 [目前範圍] 移除匯入。

    Screenshot that shows how to access the Remove Imports menu in Visual Studio 2019.

    Visual Studio 會判斷程式代碼中是否有未使用的模組,並移除對應的 import 語句。

  2. 選取選項之後,請確認對檔案進行預期的變更。 在此範例中,Visual Studio 會移除三個未使用的模組:binascii、array 和 glob。

    Screenshot that shows the results of using the Remove Imports command to remove unused modules from the code in Visual Studio 2019.

  1. 在您的程式代碼中 import ,將插入號放在Visual Studio顯示智慧標記的語句上(燈泡)。 在此範例中,智慧標記會顯示未使用的模組 binascii、array 和 glob:

    Screenshot that shows how to access options to remove unused imports in Visual Studio 2022.

  2. 選取 [ 移除所有未使用的匯 入] 或 [移除未使用的匯 入] 選項,只移除選取的模組。

  3. 選取選項之後,請確認您對檔案所做的變更。 在此範例中,Visual Studio 會移除三個未使用的模組:binascii、array 和 glob。

    Screenshot that shows the results of using the Remove all unused imports command in Visual Studio 2022.

使用重構命令時的考慮

使用重構命令之前,請先檢閱下列考慮。

  • 執行重構命令之後,您可以使用 [編輯>復原] 命令來還原變更。 Rename 命令提供預覽功能,因此您可以在套用變更之前看到變更。

  • Visual Studio 不會考慮程式代碼中的控制流程。 如果您在程式代碼中出現支援定義之前使用標識符,例如 import 語句,Visual Studio 仍會處理所使用的標識符。 Visual Studio 預期會在呼叫和指派之前尋找標識碼的支持定義。

  • Visual Studio 會忽略所有 from __future__ 匯入語句。 這些語句是在類別定義或using from ... import * 語句內執行的匯入。