创建新的“Python 应用程序”项目后,名为 PythonApplication1.py 的默认空文件将在 Visual Studio 编辑器中打开。After creating a new "Python Application" project, a default empty file named PythonApplication1.py is open in the Visual Studio editor.

在编辑器中,开始键入 print("Hello, Visual Studio"),注意 Visual Studio IntelliSense 如何在此过程中显示自动完成选项。In the editor, start typing print("Hello, Visual Studio") and notice how Visual Studio IntelliSense displays auto-completion options along the way. 下拉列表中加外边框的选项是按 Tab 键时使用的默认完成选项。The outlined option in the drop-down list is the default completion that's used when you press the Tab key. 涉及到较长的语句或标识符时,最适合使用“完成”。Completions are most helpful when longer statements or identifiers are involved.

VS2010如何调用python文件 vs运行python_Visual

IntelliSense 根据正在使用的语句、正在调用的函数等显示不同的信息。IntelliSense shows different information depending on the statement you're using, the function you're calling, and so forth. 使用 print 函数时,在 print 后面键入 ( 表示函数调用将显示该函数的完整使用情况信息。With the print function, typing ( after print to indicate a function call displays full usage information for that function. IntelliSense 弹出窗口还用粗体显示当前参数(如此处所示的 value):The IntelliSense pop up also shows the current argument in boldface (value as shown here):

VS2010如何调用python文件 vs运行python_Python_02

完成该语句,使其与以下内容匹配:Complete the statement so it matches the following:

print("Hello, Visual Studio")

注意语法着色如何区分 print 语句与 "Hello Visual Studio" 参数。Notice the syntax coloration that differentiates the statement print from the argument "Hello Visual Studio". 另外,暂时删除字符串上的最后一个 ",并注意 Visual Studio 如何在包含语法错误的代码下方显示一条红色下划线。Also, temporarily delete the last " on the string and notice how Visual Studio shows a red underline for code that contains syntax errors. 然后,替换 " 以更正此代码。Then replace the " to correct the code.

VS2010如何调用python文件 vs运行python_Visual_03

提示

由于一个人的开发环境是件非常私人的事情,因此,Visual Studio 允许用户完全控制 Visual Studio 的外观和行为。Because one's development environment is a very personal matter, Visual Studio gives you complete control over Visual Studio's appearance and behavior. 选择“工具” > “选项” 菜单命令,浏览“环境” 和“文本编辑器” 选项卡下面的设置。Select the Tools > Options menu command and explore the settings under the Environment and Text Editor tabs. 默认情况下仅显示部分选项;若要查看每种编程语言的每个选项,请选择对话框底部的“显示所有设置” 。By default you see only a limited number of options; to see every option for every programming language, select Show all settings at the bottom of the dialog box.

按 Ctrl +F5 或选择“调试” > “开始执行(不调试)” 菜单项,运行到目前为止编写的代码。Run the code you've written to this point by pressing Ctrl+F5 or selecting Debug > Start without Debugging menu item. 如果代码中仍然存在错误,Visual Studio 会发出警告。Visual Studio warns you if you still have errors in your code.

运行程序时,会出现一个显示结果的控制台窗口,就像从命令行使用 PythonApplication1.py 运行 Python 解释器一样。When you run the program, a console window appears displaying the results, just as if you'd run a Python interpreter with PythonApplication1.py from the command line. 按键关闭窗口,返回到 Visual Studio 编辑器。Press a key to close the window and return to the Visual Studio editor.

VS2010如何调用python文件 vs运行python_Visual_04

除了完成语句和函数外,IntelliSense 还提供 Python import 和 from 语句的完成。In addition to completions for statements and functions, IntelliSense provide completions for Python import and from statements. 这些完成有助于轻松发现环境中可用的模块以及这些模块的成员。These completions help you easily discover what modules are available in your environment and the members of those modules. 在编辑器中,删除 print 行,开始键入 import。In the editor, delete the print line and start typing import. 键入空格时会显示模块列表:A list of modules appears when you type the space:

VS2010如何调用python文件 vs运行python_ide_05

通过键入或选择 sys 完成行。Complete the line by typing or selecting sys.

在下一行中,键入 from 再次查看模块列表:On the next line, type from to again see a list of modules:

VS2010如何调用python文件 vs运行python_VS2010如何调用python文件_06

选择或键入 math,然后继续键入一个空格和 import,将显示模块成员:Select or type math, then continue typing with a space and import, which displays the module members:

VS2010如何调用python文件 vs运行python_Visual_07

通过导入 sin、cos 和 radians 成员完成,注意自动完成可用于每个成员。Finish by importing the sin, cos, and radians members, noticing the auto-completions available for each. 完成后,代码应如下所示:When you're done, your code should appear as follows:

import sys

from math import cos, radians

提示

完成可处理键入时的子字符串,匹配单词的某些部分、单词开头的字母,甚至是跳过的字符。Completions work with substrings as you type, matching parts of words, letters at the beginning of words, and even skipped characters. 请参阅编辑代码 - 完成,了解详细信息。

再添加一小段代码以输出 360 度的余弦值:Add a little more code to print the cosine values for 360 degrees:

for i in range(360):

print(cos(radians(i)))

使用 Ctrl +F5 或“调试” > “开始执行(不调试)” 再次运行程序。Run the program again with Ctrl+F5 or Debug > Start without Debugging. 完成后,关闭输出窗口。Close the output window when you're done.