How to use Python development on Windows

This article in the original technical team from the city of grapes and starting

Please indicate the source: Grape City's official website , Grape City to provide professional development tools for developers, solutions and services, enabling developers.

 

All along, Microsoft C # programming language is the most significant aspects of Tag, but now Python has grown from a niche language, became the world rankings ranked the forefront of programming languages ​​language.

Python also Web development, web crawler, data analysis, large data processing, machine learning, scientific computing and graphics and other fields have good natural advantages and good performance. Microsoft and from the acquisition of Github, in the open source community into increasing the intensity, so for the support of Python, and provide more contributions to the Python community, Microsoft itself has to assume more responsibility.

So recently, Microsoft set the line Python Tutorial "Develp with Python on Windows", the document content includes Python development environment, install the appropriate development tools and WSL Windows subsystem, as well as how to integrate VS Code and Git development tools and . Down and let us take a look at how to configure Python development environment and the Hello World tutorial.

Setting up the development environment

For newcomers unfamiliar with Python, we recommend installing Python from a Store in the MicrosoftMicrosoft Store will be installed by using the basic Python3 interpreter, but for the current user (avoiding the need for administrator access) set the path settings, and provides automatic updates. If you are in education or environmental organizations restrict administrative access privileges or part, this is particularly useful.

If you use Python for web development on Windows, it is recommended to set up other development environments. It recommended by Windows subsystem for Linux installation and use of Python, rather than installed directly on Windows. For help, see: start using Python for web development on WindowsIf you are interested automate common tasks on the operating system, please refer to the following guidelines: start with Python scripting and automation on WindowsFor certain advanced scenarios (such as the need to access / edit a copy of Python installed files, binary files created or used directly Python Dll), you may want to consider the direct python.org download a specific version of Python, or consider installation of a Zhong alternative The method as Anaconda, Jython, PyPy, WinPython, IronPython like. Only if you are more advanced Python programmers when only recommend using this method, the specific reasons for selecting an alternative implementation.

Installing Python

Use Microsoft Store to install Python:

  1. Go to the "Start" menu (bottom left of the window icon), type "Microsoft Store", select a link to open application store.

  2. After opening the store, select the menu in the upper right of the "Search" and enter "Python". Open the "Python 3.7" from the result under "Applications". Select "Get."

  3. After the process of downloading and installing Python, use the "Start" menu (bottom left of the window icon) to open the Windows PowerShell. Open PowerShell, an input Python --versionto confirm that Python3 installed on the computer.

  4. Python installation of Microsoft Store contains pip, namely standard package manager. Pip allows you to install and manage package does not belong to other Python standard library. To confirm that also has a pip for the installation and management package, enter pip --version.

Install Visual Studio Code

By using VS Code as a text editor / integrated development environment (IDE), you can use IntelliSense  (code completion help)  Linting  (helps to avoid errors in the code), debugging support (to help you find the error in) the run Code), the code fragment (small reusable template) and a code block unit tests (using different types of input interfaces test code).

VS Code also contains a built-in terminal , so you can use the Windows command prompt, PowerShell or any way you like to open a command line Python, creating a seamless workflow between your code editor and the command line.

  1. To install the VS Code, please download the https://code.visualstudio.com Windows of VS Code :.

  2. Python is an interpreted language, to run Python code, you must inform the interpreter VS Code to be used. We recommend to stick with Python 3.7, unless you have a specific reason to choose something else. To select python 3 interpreter, open a command panel (Ctrl + Shift + P), start typing the following command: Select the "interpreter" to search, and select the command. You can also use the "Select Python environment" option on the bottom status bar (if available) (it may have to display the selected interpreter). This command displays VS Code can automatically find the list of available interpreters, including virtual environments. If you do not see the interpreter, see Configure the Python environment .

    Python interpreter selecting the VS Code

  3. To open the terminal in the VS Code, please select "View> Terminal", or use the shortcut Ctrl + '(apostrophe character using reverse). The default terminal is PowerShell.

  4. In the VS Code terminal, simply enter a command to open the Python:python

  5. Enter the following, try to use the Python interpreter print("Hello World")is: The Python return statement "Hello World".

    VS Code The Python command line

Mounting the Git (optional)

如果你计划在 Python 代码上与其他人进行协作, 或在开源站点 (例如 GitHub) 上托管你的项目, VS Code 支持使用 Git 进行版本控制。 VS Code 中的 "源代码管理" 选项卡跟踪所有更改, 并在 UI 中内置内置的 Git 命令 (添加、提交、推送和拉取)。 首先需要安装 Git 才能打开源代码管理面板。

  1. git-scm 网站下载并安装适用于 Windows 的 Git。

  2. 其中包含了一个安装向导, 该向导将询问一系列有关 Git 安装设置的问题。 建议使用所有默认设置, 除非您有特定原因要更改某些内容。

  3. 如果以前从未处理过 Git, GitHub 指南可帮助你入门。

有关某些 Python 基础知识的 Hello World 教程

根据其 creator Guido van Rossum, Python 是一种 "高级编程语言", 其核心设计理念全部与代码可读性和语法相关, 使程序员能够在几行代码中表达概念。 "

Python 是一种解释型语言。 与编译的语言不同, 你编写的代码需要转换为机器代码才能由计算机处理器运行, Python 代码直接传递给解释器并直接运行。 只需键入代码并运行代码。 试试吧!

  1. 打开 PowerShell 命令行后, 输入python以运行 Python 3 解释器。 (某些指令更喜欢使用命令pypython3, 它们也应该有效。) 你将知道, 你会成功, 因为将显示一个 > > > 提示, 其中三个符号为三个。

  2. 可以通过几种内置方法修改 Python 中的字符串。 使用以下方式创建变量: variable = 'Hello World!'。 对于新行, 请按 Enter。

  3. 用以下内容打印变量print(variable):。 这会显示文本 "Hello World!"。

  4. 使用: len(variable)查找字符串变量的长度和使用的字符数。 这会显示使用了12个字符。 (请注意, 该空格在总长度中被计为一个字符。)

  5. 将字符串变量转换为大写字母: variable.upper()。 现在将字符串变量转换为小写字母: variable.lower()

  6. 计算在字符串变量中使用字母 "l" 的次数: variable.count("l")

  7. 搜索字符串变量中的特定字符, 让我们查找感叹号, 使用: variable.find("!")。 这会显示感叹号位于字符串的第11个位置字符中。

  8. 将感叹号替换为问号: variable.replace("!", "?")

  9. 若要退出 Python, 可以输入exit()、 quit()或, 然后选择 Ctrl + z。

This tutorial PowerShell Screenshot

希望使用 Python 的某些内置字符串修改方法时要开心。 现在, 请尝试创建 Python 程序文件并使用 VS Code 运行该文件。

使用 Python 与 VS Code Hello World 教程

VS Code 团队已结合了有关 Python 的精彩入门教程, 介绍如何使用 python 创建 Hello World 程序、运行程序文件、配置和运行调试器, 以及安装程序包 (例如matplotlibnumpy在虚拟环境中创建图形绘图。

 

1. 打开 PowerShell 并创建名为 "hello" 的空文件夹, 导航到此文件夹, 然后在 VS Code 中打开它:

 

mkdir hello
cd hello
code .

 

2. VS Code 打开后, 在左侧的资源管理器窗口中显示新的 " hello " 文件夹, 通过按Ctrl + ' (使用反撇号) 或选择 "查看 > ",在VSCode的底部面板中打开命令行窗口。终端。 通过在文件夹中开始 VS Code, 该文件夹将成为你的 "工作区"。 VS Code 存储特定于 vscode/settings 中的工作区的设置, 它们不同于全局存储的用户设置。

 

 

3. 继续 VS Code 文档中的教程:创建 Python Hello World 源代码文件

 

使用 Pygame 创建简单游戏

Run the sample games Pygame

Pygame 是一种流行的 Python 包, 用于编写游戏-鼓励学生学习编程, 同时创建有趣的东西。Pygame 在新窗口中显示图形, 因此它将无法在 WSL 的命令行方法下运行。 但是, 如果您通过本教程中所述的 Microsoft Store 安装了 Python, 它将正常工作。

  1. 安装 Python 后, 通过键入python -m pip install -U pygame --user从命令行 (或 VS Code 内的终端) 安装 pygame。

  2. 通过运行示例游戏来测试安装:python -m pygame.examples.aliens

  3. 一切正常, 游戏就会打开一个窗口。 完成播放后, 关闭窗口。

下面介绍了如何开始编写自己的游戏。

打开 PowerShell (或 Windows 命令提示符) 并创建一个名为 "弹跳" 的空文件夹。 导航到此文件夹并创建一个名为 "bounce.py" 的文件。 在 VS Code 中打开文件夹:

mkdir bounce
cd bounce
new-item bounce.py
code .

2. 使用 "VS Code", 输入以下 Python 代码 (或复制并粘贴):

import sys, pygame

pygame.init()

size = width, height = 640, 480
dx = 1
dy = 1
x= 163
y = 120
black = (0,0,0)
white = (255,255,255)

screen = pygame.display.set_mode(size)

while 1:

    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

    x += dx
    y += dy

    if x < 0 or x > width:   
        dx = -dx

    if y < 0 or y > height:
        dy = -dy

    screen.fill(black)

    pygame.draw.circle(screen, white, (x,y), 8)

    pygame.display.flip()

将其另存bounce.py为:。

从 PowerShell 终端, 通过输入以下内容来运行python bounce.py它:。

Pygame run under a big problem

请尝试调整某些数字, 以查看它们对弹跳球的影响。

Read about by pygame in pygame.org writing details of the game.

 

Guess you like

Origin www.cnblogs.com/powertoolsteam/p/11248091.html