當您開發 Python 專案時,您可能會發現自己切換至命令視窗以執行特定文稿或模組、執行 pip 命令,或使用其他工具搭配您的程式碼。 若要改善工作流程,您可以將自定義命令新增至Visual Studio中的 Python 專案選單。 自定義 Python 命令可以在控制台視窗中或 Visual Studio 輸出 視窗中執行。 您也可以使用正規表示式來指示 Visual Studio 如何剖析命令輸出中的錯誤和警告。
先決條件
- 在 Windows 上安裝 Visual Studio 並支援 Python 工作負載。 如需詳細資訊,請參閱 在Visual Studio中安裝 Python 支援。
不支援 Visual Studio for Mac。 如需詳細資訊,請參閱 Visual Studio for Mac 發生什麼事? Windows、Mac 和 Linux 上的 Visual Studio Code 透過可用的擴充功能與 Python 搭配運作。
探索自定義命令
根據預設,Python 項目選單包含兩個命令:執行 PyLint,以及 執行 Mypy:
您定義的任何自定義 Python 命令都會出現在這個相同的功能表中。 自定義命令可以參考 Python 檔案、Python 模組、內嵌 Python 程式代碼、任意可執行檔或 pip 命令。 您也可以指定命令的執行方式和位置。
您可以透過數種方式新增自訂命令:
在 Python 專案檔中定義自定義命令(.pyproj)。 這些命令會套用至該特定專案。
在目標檔案中定義自定義命令(.targets)。 您可以輕鬆地匯入此檔案中的命令,以將它們與多個專案搭配使用。
在 Visual Studio 中,從定義自定義 Python 命令的專案範本建立 Python 專案。
Visual Studio 中的特定 Python 專案範本會使用目標檔案來新增自定義命令。 Bottle Web 專案和 Flask Web 專案範本會新增兩個命令,啟動伺服器,啟動偵錯伺服器。 Django Web 專案模板會新增下列命令及其他更多:
重載專案以存取自定義命令
在 Visual Studio 中開啟專案時,如果您在編輯器中對對應的專案檔進行變更,則必須重載專案以套用變更。 同樣地,在 Python 專案檔中定義自定義 Python 命令之後,您必須重載 Python 專案,命令才能出現在 Python 項目選單上。 當您修改目標檔案中定義的自定義命令時,您必須針對匯入該目標檔案的任何專案重建完整的 Visual Studio 方案。
常見的方法是直接在 Visual Studio 中變更 Python 專案檔:
在 Visual Studio 中開啟 Python 專案。 (當您在 Visual Studio 中開啟專案時,專案預設會 載入。)
在 [方案總管]中,以滑鼠右鍵點選 Python 專案,然後選取 [卸載專案]。
Visual Studio 會卸除專案,並在編輯器中開啟對應的 Python 專案檔 (.pyproj)。
如果項目檔未開啟,請再次以滑鼠右鍵按兩下 Python 專案,然後選取 [編輯項目檔] :
在 Visual Studio 編輯器中變更項目檔,並儲存您的工作。
在 [方案總管]中,右鍵點擊已卸載的專案,然後選取 [重載專案] 。 如果您嘗試重載專案而不儲存對專案檔的變更,Visual Studio 會提示您完成動作。
當您開發自定義命令時,卸載-編輯-儲存-重新載入的流程可能會變得令人厭煩。 更有效率的工作流程牽涉到在 Visual Studio 中同時載入專案,並在個別編輯器中開啟 Python 項目檔。 您可以使用任何編輯器,例如另一個Visual Studio實例、Visual Studio Code、記事本等等。 在編輯器中儲存變更並切換回 Visual Studio 之後,Visual Studio 會偵測開啟專案的專案檔變更,並提示您採取動作:
選取 [重載] 或 [重載所有],Visual Studio 會立即將專案檔變更套用至開啟的專案。
使用項目檔新增自定義命令
下列程式示範如何在 Python 專案檔中新增定義來建立自定義命令(.pyproj),並在 Visual Studio 中重載您的專案。 自訂命令會使用 python.exe
命令直接執行專案的啟動檔案,這基本上與在主要 Visual Studio 工具列上使用 [除錯]>[啟動但不除錯] 選項相同。
在 Visual Studio 中,使用 Python 應用程式 範本,建立名為 Python-CustomCommands 的新 Python 專案。 如需指示,請參閱 快速入門:從範本建立 Python 專案。
Visual Studio 會建立 Python 專案,並將其載入您的會話。 您可以透過項目檔來設定專案(.pyproj)。 此檔案只有在專案開啟時才會顯示在 Visual Studio 中,但 卸載。 專案也有應用程式程式代碼的 Python 檔案 (.py)。
在編輯器中開啟 Python_CustomCommands.py 應用程式檔,然後新增下列程式代碼:
print("Hello custom commands")
在 [方案總管]中,以滑鼠右鍵按兩下 Python 專案,選取 [Python],並注意操作功能表上的命令。 目前,操作功能表上的唯一命令是 執行 PyLint,執行 Mypy。 當您定義自定義命令時,這些命令也會出現在此功能表上。
在 Visual Studio 工作階段之外啟動個別的編輯器,並在編輯器中開啟 Python 專案檔 (Python-CustomCommands.pyproj)。 (請務必開啟項目檔(.pyproj),而不是 Python 應用程式檔(.py)。
在專案檔中,找出位於檔案結尾的結尾
</Project>
元素,並在結尾元素之前立即新增以下的 XML:<PropertyGroup> <PythonCommands> $(PythonCommands); </PythonCommands> </PropertyGroup>
儲存您的項目檔變更,然後切換回 Visual Studio。 Visual Studio 會偵測您的專案檔變更,並提示您採取動作。 在提示中,選取 重載,以更新專案檔變更至您開啟的專案。
在 [方案總管]中,以滑鼠右鍵按兩下 Python 專案,選取 [Python],然後檢查操作功能表上的命令。
操作功能表仍然只會顯示 執行 PyLint 命令和 執行 Mypy 命令。 您剛新增至項目檔的程式代碼只會復寫包含 PyLint 命令的預設
<PythonCommands>
屬性群組。 在下一個步驟中,您會為自定義命令新增更多程序代碼。切換至您要更新項目檔的編輯器。 在
<Project>
元素內新增下列<Target>
項目定義。 您可以在先前所述的<PropertyGroup>
定義之前或之後放置<Target>
定義。此
<Target>
項目會定義自定義命令,以在控制台視窗中使用python.exe
命令來執行專案的啟動檔案(StartupFile 屬性識別)。 屬性定義ExecuteIn="consolepause"
使用一個主控台,該主控台會等候您選擇一個按鍵來關閉視窗。<Target Name="Example_RunStartupFile" Label="Run startup file" Returns="@(Commands)"> <CreatePythonCommandItem TargetType="script" Target="$(StartupFile)" Arguments="" WorkingDirectory="$(MSBuildProjectDirectory)" ExecuteIn="consolepause"> <Output TaskParameter="Command" ItemName="Commands" /> </CreatePythonCommandItem> </Target>
以下列 XML 取代
<PythonCommands>
屬性群組(步驟 5中新增)。 此語法定義了<Target>
元素的Name
屬性,該屬性會將自定義命令新增至 Python 內容功能表。 命令具有選單標籤,[執行啟動檔案]。<PythonCommands> $(PythonCommands); Example_RunStartupFile </PythonCommands>
提示
如果您希望自訂命令出現在右鍵選單中且位於
$(PythonCommands)
令牌中定義的預設命令之前,請將命令的<Target>
語法放在該令牌之前。儲存您的項目檔變更,然後切換回 Visual Studio。 當出現提示時,重載您的專案。
在 [方案總管]中,以滑鼠右鍵按兩下 Python 專案,選取 [Python],然後重新檢查操作功能表上的命令。
現在,自定義 執行啟動檔案 命令位於功能表上。 如果您沒有看到自定義命令,請確認您已將
<Target>
元素的Name
屬性值新增至<PythonCommands>
元素,如步驟 9 中所述。 另請參閱本文稍後的 疑難解答 一節中所列的考慮。選擇 [執行啟動檔案] 命令。 主控台視窗隨即開啟並顯示 Hello 自訂命令的文字, 後面接著 按任意鍵繼續。 確認輸出並關閉主控台視窗。
注意
自定義命令文本會在 Python 專案的啟動環境中執行。
使用項目檔切換至編輯器。 在
<Target>
元素定義中(步驟 8中新增),將ExecuteIn
屬性的值變更為output
。<CreatePythonCommandItem ... ExecuteIn="output"> ... </CreatePythonCommandItem>
儲存變更、切換回 Visual Studio,然後重載您的專案。
再次在 [Python] 快捷選單中選取 [執行啟動檔案] 自定義命令。 現在程序輸出會出現在 Visual Studio [輸出] 視窗中,而不是控制台視窗:
若要新增更多自定義命令,請遵循下列相同程式:
為專案檔中的自定義命令定義適當的
<Target>
元素。將
<Target>
項目的Name
屬性值新增至<PythonCommands>
屬性群組。將變更儲存至項目檔。
在 Visual Studio 中重載您的專案。
使用專案屬性
若要參考 <Target>
項目屬性或項目屬性值中的環境變數,請使用 $()
標記中的屬性名稱,例如 $(StartupFile)
和 $(MSBuildProjectDirectory)
。 如需詳細資訊,請參閱 MSBuild 屬性。
如果您叫用類似 ($StartupFile)
的命令,該命令會使用 StartupFile 屬性等項目屬性,而且命令會因為令牌未定義而失敗,Visual Studio 會停用命令,直到您重載項目為止。 如果您對修改屬性定義的項目進行變更,您的變更不會重新整理相關命令的狀態。 在此情況下,您仍然需要重載專案。
了解 <目標的> 元素結構
您可以使用 <Target>
項目來定義自訂命令的詳細資料。
<Target>
元素的一般形式會顯示在下列偽代碼中:
<Target Name="Name1" Label="Display Name" Returns="@(Commands)">
<CreatePythonCommandItem Target="filename, module name, or code"
TargetType="executable/script/module/code/pip"
Arguments="..."
ExecuteIn="console/consolepause/output/repl[:Display name]/none"
WorkingDirectory="..."
ErrorRegex="..."
WarningRegex="..."
RequiredPackages="...;..."
Environment="...">
<!-- Output always appears in this form, with these exact attributes -->
<Output TaskParameter="Command" ItemName="Commands" />
</CreatePythonCommandItem>
</Target>
目標屬性
下表列出 <Target>
項目屬性。
屬性 | 必填 | 描述 |
---|---|---|
Name |
是的 | Visual Studio 專案中命令的標識碼。 這個名稱必須新增至 <PythonCommands> 屬性群組,命令才能出現在 Python 操作功能表上。 |
Label |
是的 | 出現在 Python 操作功能表中的 UI 顯示名稱。 |
Returns |
是的 | 傳回的信息,必須包含 @(Commands) 標記,這個標記用於將目標識別為命令。 |
CreatePythonCommandItem 屬性
<Target>
元素包含 <CreatePythonCommandItem>
和 <Output>
元素,定義了自定義命令的詳細行為。 下表列出可用的 <CreatePythonCommandItem>
項目屬性。 所有屬性值都不區分大小寫。
Attribute |
必填 | 描述 |
---|---|---|
TargetType |
是的 | 指定 Target 屬性包含的內容,以及值與 Arguments 屬性的使用方式:- executable :執行 Target 屬性中名為的可執行檔,並附加 Arguments 屬性中的值,就像直接在命令列上輸入一樣。 值必須只包含不含自變數的程式名稱。 - script :使用 Target 屬性中的檔名執行 python.exe 命令,後面接著 Arguments 屬性中的值。 - module :執行 python -m 命令,後面接著 Target 屬性中的模組名稱,後面接著 Arguments 屬性中的值。 - code :執行包含在 Target 屬性中的內嵌程序代碼。 忽略 Arguments 屬性值。 - pip :使用 Target 屬性中的 命令執行 pip,後面接著 Arguments 屬性中的值。 如果 ExecuteIn 屬性設定為 output ,pip 會假設要求是執行 install 命令,並使用 Target 屬性作為套件名稱。 |
Target |
是的 | 根據 TargetType 屬性的值,指定要使用的檔名、模組名稱、程式代碼或 pip 命令。 |
Arguments |
選擇性 | 提供要與 Target 屬性一起使用的參數字串(如果有的話)。 - 當 TargetType 屬性值 script 時,Arguments 值會提供給 Python 程式,而不是 python.exe 命令。 - 當 TargetType 屬性值 code 時,會忽略 Arguments 值。 |
ExecuteIn |
是的 | 指定要在其中執行命令的環境: - console :( 預設值) 使用 Arguments 值執行 Target 屬性,就像直接在命令行上輸入一樣。 當 Target 屬性正在執行時,命令視窗隨即出現並自動關閉。 - consolepause :與 console 相同的行為,但在關閉視窗之前,先等候按下按鍵。 - output :執行 Target 屬性,並在 Visual Studio [輸出] 視窗中顯示結果。 如果 TargetType 屬性是 pip ,Visual Studio 會使用 Target 屬性做為套件名稱,並附加 Arguments 屬性值。 - repl :在 Python 互動式視窗中執行 Target 屬性。 選用的顯示名稱會用於視窗的標題。 - none :與 console 相同的行為。 |
WorkingDirectory |
選擇性 | 識別要在其中執行命令的資料夾。 |
ErrorRegex WarningRegEx |
選用 | 只有當 ExecuteIn 屬性設定為 output 時,才會使用 。 這兩個屬性值都會指定 Visual Studio 用來剖析命令輸出的正規表示式,並在 [錯誤清單] 視窗中顯示錯誤和警告。 如果未指定這些屬性,命令不會影響 錯誤清單 視窗。 如需了解 Visual Studio 的預期詳細資訊,請參閱 具名擷取群組。 |
RequiredPackages |
選擇性 | 使用與 requirements.txt 檔案相同的格式來提供命令的套件需求清單(pip.readthedocs.io)。 例如,執行 PyLint 命令會指定格式 pylint>=1.0.0 。 執行命令之前,Visual Studio 會確認清單中已安裝所有套件。 Visual Studio 會使用 pip 來安裝任何遺漏的套件。 |
Environment |
可選擇的 | 識別執行命令之前要定義的環境變數字串。 每個變數都會使用表單 \<NAME>=\<VALUE> ,並以分號分隔多個變數。 具有多個值的變數必須包含在單引號或雙引號中,如 'NAME=VALUE1;VALUE2' 所示。 |
正則表達式的具名擷取群組
當 Visual Studio 從自定義命令輸出剖析錯誤和警告時,它會預期在 ErrorRegex
和 WarningRegex
屬性值中使用下列具名群組的正規表示式:
-
(?<message>...)
:錯誤的文字。 -
(?<code>...)
:錯誤碼值。 -
(?<filename>...)
:報告錯誤的檔名。 -
(?<line>...)
:錯誤已報告的檔案中該位置的行號。 -
(?<column>...)
:報告錯誤之檔案中位置的欄號。
例如,PyLint 會產生下列形式的警告:
************* Module hello
C: 1, 0: Missing module docstring (missing-docstring)
若要讓 Visual Studio 從這些警告中擷取正確的資訊,並在 [錯誤清單] 視窗中顯示它們,Run Pylint 命令的 WarningRegex
屬性值如下所示:
^(?<filename>.+?)\((?<line>\d+),(?<column>\d+)\): warning (?<msg_id>.+?): (?<message>.+?)$]]
注意
WarningRegex
屬性值中的 msg_id
語法實際上應該 code
,如 問題 3680中所述。
使用目標檔案來匯入自定義命令
如果您在 Python 專案檔中定義自訂命令,則命令僅適用於該特定專案。 當您想要建立自定義命令並在多個專案中使用這些命令時,您可以使用目標檔案中所有 <Target>
項目來定義 <PythonCommands>
屬性群組(.targets),然後將該檔案匯入 Python 專案中。
目標檔案使用相同的格式和語法來定義 Python 專案檔所述的自定義命令(.pyproj)。 要設定的常見元素包括
<PythonCommands>
、<Target>
、<CreatePythonCommandItem>
和<Output>
:<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <PythonCommands> $(PythonCommands); <!-- Additional command names --> </PythonCommands> </PropertyGroup> <Target Name="..." Label="..." Returns="@(Commands)"> <!-- CreatePythonCommandItem and Output elements... --> </Target> <!-- Any number of additional Target elements--> </Project>
若要將目標檔案導入專案,請在專案檔案中的
<Project>
元素內的任何位置新增<Import Project="(path)">
元素。例如,如果您的專案檔名為 CustomCommands.targets,且位於 Python 專案中的 targets 資料夾中,請將下列代碼新增至專案檔:
<Import Project="targets/CustomCommands.targets"/>
如果您的專案檔匯入目標檔案,而且您在 Visual Studio 中開啟專案時對目標檔案進行變更,則必須 重建 包含您專案的 Visual Studio 解決方案,而不只是您的專案。
範例命令
下列各節提供範例程式代碼,可讓您用來定義 Python 專案的自定義命令。
執行 PyLint (模組目標)
下列程式代碼會出現在 Microsoft.PythonTools.targets 檔案中:
<PropertyGroup>
<PythonCommands>$(PythonCommands);PythonRunPyLintCommand</PythonCommands>
<PyLintWarningRegex>
<![CDATA[^(?<filename>.+?)\((?<line>\d+),(?<column>\d+)\): warning (?<msg_id>.+?): (?<message>.+?)$]]>
</PyLintWarningRegex>
</PropertyGroup>
<Target Name="PythonRunPyLintCommand"
Label="resource:Microsoft.PythonTools.Common;Microsoft.PythonTools.Common.Strings;RunPyLintLabel"
Returns="@(Commands)">
<CreatePythonCommandItem Target="pylint.lint"
TargetType="module"
Arguments=""--msg-template={abspath}({line},{column}): warning {msg_id}: {msg} [{C}:{symbol}]" -r n @(Compile, ' ')"
WorkingDirectory="$(MSBuildProjectDirectory)"
ExecuteIn="output"
RequiredPackages="pylint>=1.0.0"
WarningRegex="$(PyLintWarningRegex)">
<Output TaskParameter="Command" ItemName="Commands" />
</CreatePythonCommandItem>
</Target>
使用特定套件執行 pip 安裝 (pip 目標)
下列命令會在 Visual Studio [輸出] 視窗中執行 pip install my-package
命令。 當您開發套件並測試其安裝時,可能會使用如下的命令。
<Target>
元素包含封裝名稱,而不是 install
命令,這是在使用 ExecuteIn="output"
屬性定義時被假設的。
<PropertyGroup>
<PythonCommands>$(PythonCommands);InstallMyPackage</PythonCommands>
</PropertyGroup>
<Target Name="InstallMyPackage" Label="pip install my-package" Returns="@(Commands)">
<CreatePythonCommandItem Target="my-package" TargetType="pip" Arguments=""
WorkingDirectory="$(MSBuildProjectDirectory)" ExecuteIn="output">
<Output TaskParameter="Command" ItemName="Commands" />
</CreatePythonCommandItem>
</Target>
顯示已過時的 pip 套件(pip 目標)
下列命令會使用 list
函式執行 pip,以識別過期的 pip 套件:
<PropertyGroup>
<PythonCommands>$(PythonCommands);ShowOutdatedPackages</PythonCommands>
</PropertyGroup>
<Target Name="ShowOutdatedPackages" Label="Show outdated pip packages" Returns="@(Commands)">
<CreatePythonCommandItem Target="list" TargetType="pip" Arguments="-o --format columns"
WorkingDirectory="$(MSBuildProjectDirectory)" ExecuteIn="consolepause">
<Output TaskParameter="Command" ItemName="Commands" />
</CreatePythonCommandItem>
</Target>
使用 consolepause 執行可執行檔
下列命令會執行 where
函式,以顯示從專案資料夾開始的 Python 檔案位置:
<PropertyGroup>
<PythonCommands>$(PythonCommands);ShowAllPythonFilesInProject</PythonCommands>
</PropertyGroup>
<Target Name="ShowAllPythonFilesInProject" Label="Show Python files in project" Returns="@(Commands)">
<CreatePythonCommandItem Target="where" TargetType="executable" Arguments="/r . *.py"
WorkingDirectory="$(MSBuildProjectDirectory)" ExecuteIn="output">
<Output TaskParameter="Command" ItemName="Commands" />
</CreatePythonCommandItem>
</Target>
執行伺服器並執行偵錯伺服器命令
若要探索如何定義 web 專案的 Start server 和 Start debug server 命令,請檢查 GitHub 上的 Microsoft.PythonTools.Web.targets 存放庫。
安裝套件以進行開發
下列程式代碼會執行 pip 來安裝套件:
<PropertyGroup>
<PythonCommands>PipInstallDevCommand;$(PythonCommands);</PythonCommands>
</PropertyGroup>
<Target Name="PipInstallDevCommand" Label="Install package for development" Returns="@(Commands)">
<CreatePythonCommandItem Target="pip" TargetType="module" Arguments="install --editable $(ProjectDir)"
WorkingDirectory="$(WorkingDirectory)" ExecuteIn="Repl:Install package for development">
<Output TaskParameter="Command" ItemName="Commands" />
</CreatePythonCommandItem>
</Target>
來自 fxthomas/Example.pyproj.xml (GitHub),使用許可。
產生 Windows 安裝程式
下列文稿會產生 Windows 安裝程式:
<PropertyGroup>
<PythonCommands>$(PythonCommands);BdistWinInstCommand;</PythonCommands>
</PropertyGroup>
<Target Name="BdistWinInstCommand" Label="Generate Windows Installer" Returns="@(Commands)">
<CreatePythonCommandItem Target="$(ProjectDir)setup.py" TargetType="script"
Arguments="bdist_wininst --user-access-control=force --title "$(InstallerTitle)" --dist-dir="$(DistributionOutputDir)""
WorkingDirectory="$(WorkingDirectory)" RequiredPackages="setuptools"
ExecuteIn="Repl:Generate Windows Installer">
<Output TaskParameter="Command" ItemName="Commands" />
</CreatePythonCommandItem>
</Target>
從 fxthomas/Example.pyproj.xml (GitHub)經許可使用。
產生 Python 轉輪套件
下列腳本會產生 Python wheel 套件:
<PropertyGroup>
<PythonCommands>$(PythonCommands);BdistWheelCommand;</PythonCommands>
</PropertyGroup>
<Target Name="BdistWheelCommand" Label="Generate Wheel Package" Returns="@(Commands)">
<CreatePythonCommandItem Target="$(ProjectDir)setup.py" TargetType="script"
Arguments="bdist_wheel --dist-dir="$(DistributionOutputDir)""
WorkingDirectory="$(WorkingDirectory)" RequiredPackages="wheel;setuptools"
ExecuteIn="Repl:Generate Wheel Package">
<Output TaskParameter="Command" ItemName="Commands" />
</CreatePythonCommandItem>
</Target>
從 fxthomas/Example.pyproj.xml (GitHub),經許可使用。
針對自定義命令進行疑難解答
請檢閱下列各節,以識別與使用自定義命令相關的可能問題。
未載入項目檔
此錯誤訊息表示您在項目檔中有語法錯誤。 訊息包含特定錯誤,並附有行號和字元位置。
命令執行後主控台視窗關閉
如果主控台視窗執行命令後立即關閉,請使用 ExecuteIn="consolepause"
屬性定義,而不是 ExecuteIn="console"
。
選單缺少命令
如果您在 Python 上下文選單沒有看到自定義命令,請檢查下列項目:
- 確認命令包含在
<PythonCommands>
屬性群組中。 - 確認已定義命令清單的命令名稱符合
<Target>
專案中指定的名稱。
以下是範例。 在下列 XML 代碼段中,<PythonCommands>
屬性群組中的 Example
名稱不符合 <Target>
元素定義中的 ExampleCommand
名稱。 Visual Studio 找不到名為 Example
的命令,因此不會顯示任何命令。 請在指令清單中使用 ExampleCommand
,或將目標的名稱變更為只 Example
。
<PropertyGroup>
<PythonCommands>$(PythonCommands);Example</PythonCommands>
</PropertyGroup>
<Target Name="ExampleCommand" Label="Example Command" Returns="@(Commands)">
<!-- ... -->
</Target>
執行命令時發生錯誤,無法取得命令目標
此錯誤訊息指出 <Target>
或 <CreatePythonCommandItem>
元素的內容不正確。
以下是此錯誤的一些可能原因:
- 必要的
<Target>
元素屬性是空的。 - 必要的
TargetType
屬性是空的,或包含無法辨識的值。 - 必要的
ExecuteIn
屬性是空的,或包含無法辨識的值。 - 指定
ErrorRegex
或WarningRegex
屬性,而不需設定ExecuteIn="output"
屬性定義。 - 元素中存在無法辨識的屬性。 例如,屬性參考可能會拼錯為
Argumnets
,而不是Arguments
。
如果您參考未定義的屬性,屬性值可以是空的。 如果您使用令牌 $(StartupFile)
但專案中未定義啟動檔案,令牌會解析為空字串。 在這種情況下,您可能想要定義預設值。 例如,執行伺服器 和 執行偵錯伺服器 在 Bottle、Flask 和 Django 專案範本中定義的命令預設會使用 manage.py 檔案(如果未在專案屬性中指定伺服器啟動檔案)。
Visual Studio 停止回應,當機
如果 Visual Studio 在執行自訂命令時停止回應並當機,您可能嘗試使用 ExecuteIn="output"
屬性定義來執行控制台命令。 在這種情況下,當 Visual Studio 嘗試剖析輸出時,可能會當機。 若要避免這種情況,請改用 ExecuteIn="console"
屬性定義。 如需詳細資訊,請參閱 問題 3681。
命令無法辨識為可操作的程式或批處理檔
當您設定 TargetType="executable"
屬性定義時,Target
屬性中的值必須只 程式名稱,而不需要任何自變數,例如 python
或 python.exe
。 在此情況下,請將任何參數移動到 Arguments
屬性。