[go: up one dir, main page]

0% found this document useful (0 votes)
3 views5 pages

PP Read 1-Python in Visual Studio Code

This tutorial provides a step-by-step guide for setting up Python in Visual Studio Code using the Microsoft Python extension. It covers prerequisites, installation of VS Code and the Python extension, verifying the Python installation, creating a project workspace, selecting a Python interpreter, writing a simple 'Hello World' program, and running Python files. Additionally, it briefly mentions the use of packages for more advanced functionalities like graphical plotting.

Uploaded by

ravaligan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

PP Read 1-Python in Visual Studio Code

This tutorial provides a step-by-step guide for setting up Python in Visual Studio Code using the Microsoft Python extension. It covers prerequisites, installation of VS Code and the Python extension, verifying the Python installation, creating a project workspace, selecting a Python interpreter, writing a simple 'Hello World' program, and running Python files. Additionally, it briefly mentions the use of packages for more advanced functionalities like graphical plotting.

Uploaded by

ravaligan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Hello World Tutorial in VS Code

Working with Python in Visual Studio Code, using the Microsoft Python extension, is simple, fun, and
productive.

The extension makes VS Code an excellent Python editor and works on any operating system with a variety
of Python interpreters.

It leverages all of VS Code's power to provide autocomplete and IntelliSense, linting, debugging, and unit
testing, along with the ability to easily switch between Python environments, including virtual and Conda
environments.

Step 0: Prerequisites
Specifically, this tutorial requires:

VS Code
VS Code Python extension
Python

Step 1: Install Visual Studio Code and the


Python Extension
1.1
If you have not already had VS Code on your computer, install it -------- link: VS Code.

After successful installation, you may see an interface like the one above.
Put the last button on the right-hand side Extensions OR you can use the shortcut key Ctrl + Shift + X to
get the Extensions Market.

(Optional)

Search with the keyword Chinese, download the first extension Chinese.

Congratulations, now you have your own VSCode in Chinese.

1.2
Next, install the Python extension for VS Code from the Visual Studio Marketplace, using the keyword Python

For additional details on installing extensions, see Extension Marketplace.

The Python extension is named Python and it's published by Microsoft.

The extensions set varies from person to person. But I recommend some basic extensions as follows:

Kite
Beautify
Python Indent
Python Indent
Jupyter
Prettier - Code formatter
(optional) latex
(optional) Python Environment Manager

Step 2: Install a Python Interpreter


Along with the Python extension, you need to install a Python interpreter.

Install Python from python.org. You can typically use the Download Python button that appears first on the
page to download the latest version.

Note: The page provides installs of Python 3.7, Python 3.8, Python 3.9, and Python 3.10. Be aware that
Python 3.7 is required in SI100.

For additional information about using Python on Windows, see Using Python on Windows at Python.org

Step 3: Verify the Python Installation


To verify that you've installed Python successfully on your machine, run one of the following commands.
Open a command prompt using a shortcut key Ctrl + R and typing in "cmd". And run the following
command:

py -3 --version

(Python 3.7.x should be on your screen, ignore my python version plz)

Step 4: Start VS Code in a Project


(Workspace) Folder
Using a command prompt or terminal, create an empty folder called "SI100" on your desktop, navigate into
it, and open VS Code ( code ) in that folder ( . ) by entering the following commands:

cd Desktop
mkdir SI100
cd SI100
code .

By starting VS Code in a folder, that folder becomes your "workspace".

VS Code stores settings that are specific to that workspace in .vscode/settings.json , which are separate
from user settings that are stored globally.

Alternately, you can run VS Code through the operating system UI, then use File > Open Folder to open the
project folder. (which means to 打开文件夹)

The path to your workspace is decided by yourself, but managing your different workspace decently is a
good habit.

Step 5: Select a Python Interpreter


Python is an interpreted language, and in order to run Python code and get Python IntelliSense, you must
tell VS Code which interpreter to use.

From within VS Code, select a Python 3 interpreter by opening the Command Palette ( Ctrl+Shift+P ), start
typing the Python: Select Interpreter (Python:选择解释器) command to search, then select the
command.

You can also use the Select Python Environment option on the Status Bar if available (it may already show
a selected interpreter, too):

Step 6: Create a Python Hello World Source


Code File
From the File Explorer toolbar, select the New File button on the SI100 folder:

Name the file test.py , and it automatically opens in the editor:

Type in those codes.

print("Hello world")

When you start typing print , notice how IntelliSense presents auto-completion options. It provides
completions for methods available on object types.

By using the .py file extension, you tell VS Code to interpret this file as a Python program, so that it evaluates the
contents with the Python extension and the selected interpreter.

Note: the File Explorer toolbar also allows you to create folders within your workspace to better
organize your code. You can use the New folder button to quickly create a folder.
Step 7: Run Your First Python File
It's simple to run test.py with Python. Just click the Run Python File in Terminal play button on the top-
right side of the editor.

Step 8: Install and Use Packages


Let's now run an example that's a little more interesting.

In Python, packages are how you obtain any number of useful code libraries.

For this example, you use the matplotlib and NumPy packages to create a graphical plot as is commonly
done with data science, which is in the syllabus.

This part may be updated in a few weeks.

About
Author: Kyran Fang

Typesetting: Kyran Fang, teafrogsf

Date: 02/17/2022

You might also like