Python Programming With Microsoft Visual Studio Code For Mac

I am a Python 2 developer and I have everything working fine in Windows 7 visual studio 2015. I was wondering if anyone got the IDE and Visual Studio Code working on a Mac in OS X. If so, maybe you. 3) Visual Studio Code VS Code Visual Studio Code is an open source code editor software developed by Microsoft. This free text editor offers built-in support for TypeScript, JavaScript, and Node.js. Start from an existing Python file. Open the folder or your.py file in Visual Studio Code. Run Device Simulator Express: Open Simulator from the command palette or icon in the editor toolbar. Select the device you want to use. Visual Studio Code is a free editor written by Microsoft. Its first release was in 2015, and boy did that one take off. It has over 28k questions on StackOverflow, is the 2nd most-used Python editor according to the 2019 Jetbrains survey, and the most popular one according to the 2019 StackOverflow survey.

-->

The following is a step-by-step guide for beginners interested in learning Python using Windows 10.

Set up your development environment

For beginners who are new to Python, we recommend you install Python from the Microsoft Store. Installing via the Microsoft Store uses the basic Python3 interpreter, but handles set up of your PATH settings for the current user (avoiding the need for admin access), in addition to providing automatic updates. This is especially helpful if you are in an educational environment or a part of an organization that restricts permissions or administrative access on your machine.

If you are using Python on Windows for web development, we recommend a different set up for your development environment. Rather than installing directly on Windows, we recommend installing and using Python via the Windows Subsystem for Linux. For help, see: Get started using Python for web development on Windows. If you're interested in automating common tasks on your operating system, see our guide: Get started using Python on Windows for scripting and automation. For some advanced scenarios (like needing to access/modify Python's installed files, make copies of binaries, or use Python DLLs directly), you may want to consider downloading a specific Python release directly from python.org or consider installing an alternative, such as Anaconda, Jython, PyPy, WinPython, IronPython, etc. We only recommend this if you are a more advanced Python programmer with a specific reason for choosing an alternative implementation.

Install Python

To install Python using the Microsoft Store:

  1. Go to your Start menu (lower left Windows icon), type 'Microsoft Store', select the link to open the store.

  2. Once the store is open, select Search from the upper-right menu and enter 'Python'. Open 'Python 3.9' from the results under Apps. Select Get.

  3. Once Python has completed the downloading and installation process, open Windows PowerShell using the Start menu (lower left Windows icon). Once PowerShell is open, enter Python --version to confirm that Python3 has installed on your machine.

  4. The Microsoft Store installation of Python includes pip, the standard package manager. Pip allows you to install and manage additional packages that are not part of the Python standard library. To confirm that you also have pip available to install and manage packages, enter pip --version.

Install Visual Studio Code

By using VS Code as your text editor / integrated development environment (IDE), you can take advantage of IntelliSense (a code completion aid), Linting (helps avoid making errors in your code), Debug support (helps you find errors in your code after you run it), Code snippets (templates for small reusable code blocks), and Unit testing (testing your code's interface with different types of input).

VS Code also contains a built-in terminal that enables you to open a Python command line with Windows Command prompt, PowerShell, or whatever you prefer, establishing a seamless workflow between your code editor and command line.

  1. To install VS Code, download VS Code for Windows: https://code.visualstudio.com.

  2. Once VS Code has been installed, you must also install the Python extension. To install the Python extension, you can select the VS Code Marketplace link or open VS Code and search for Python in the extensions menu (Ctrl+Shift+X).

  3. Python is an interpreted language, and in order to run Python code, you must tell VS Code which interpreter to use. We recommend sticking with Python 3.7 unless you have a specific reason for choosing something different. Once you've installed the Python extension, select a Python 3 interpreter by opening the Command Palette (Ctrl+Shift+P), start typing the command Python: Select Interpreter to search, then select the command. You can also use the Select Python Environment option on the bottom Status Bar if available (it may already show a selected interpreter). The command presents a list of available interpreters that VS Code can find automatically, including virtual environments. If you don't see the desired interpreter, see Configuring Python environments.

  4. To open the terminal in VS Code, select View > Terminal, or alternatively use the shortcut Ctrl+` (using the backtick character). The default terminal is PowerShell.

  5. Inside your VS Code terminal, open Python by simply entering the command: python

  6. Try the Python interpreter out by entering: print('Hello World'). Python will return your statement 'Hello World'.

Install Git (optional)

If you plan to collaborate with others on your Python code, or host your project on an open-source site (like GitHub), VS Code supports version control with Git. The Source Control tab in VS Code tracks all of your changes and has common Git commands (add, commit, push, pull) built right into the UI. You first need to install Git to power the Source Control panel.

  1. Download and install Git for Windows from the git-scm website.

  2. An Install Wizard is included that will ask you a series of questions about settings for your Git installation. We recommend using all of the default settings, unless you have a specific reason for changing something.

  3. If you've never worked with Git before, GitHub Guides can help you get started.

Hello World tutorial for some Python basics

Python, according to its creator Guido van Rossum, is a “high-level programming language, and its core design philosophy is all about code readability and a syntax which allows programmers to express concepts in a few lines of code.”

Python is an interpreted language. In contrast to compiled languages, in which the code you write needs to be translated into machine code in order to be run by your computer's processor, Python code is passed straight to an interpreter and run directly. You just type in your code and run it. Let's try it!

  1. With your PowerShell command line open, enter python to run the Python 3 interpreter. (Some instructions prefer to use the command py or python3, these should also work). You will know that you're successful because a >>> prompt with three greater-than symbols will display.

  2. There are several built-in methods that allow you to make modifications to strings in Python. Create a variable, with: variable = 'Hello World!'. Press Enter for a new line.

  3. Print your variable with: print(variable). This will display the text 'Hello World!'.

  4. Find out the length, how many characters are used, of your string variable with: len(variable). This will display that there are 12 characters used. (Note that the blank space it counted as a character in the total length.)

  5. Convert your string variable to upper-case letters: variable.upper(). Now convert your string variable to lower-case letters: variable.lower().

  6. Count how many times the letter 'l' is used in your string variable: variable.count('l').

  7. Search for a specific character in your string variable, let's find the exclamation point, with: variable.find('!'). This will display that the exclamation point is found in the 11th position character of the string.

  8. Replace the exclamation point with a question mark: variable.replace('!', '?').

  9. To exit Python, you can enter exit(), quit(), or select Ctrl-Z.

Hope you had fun using some of Python's built-in string modification methods. Now try creating a Python program file and running it with VS Code.

Hello World tutorial for using Python with VS Code

The VS Code team has put together a great Getting Started with Python tutorial walking through how to create a Hello World program with Python, run the program file, configure and run the debugger, and install packages like matplotlib and numpy to create a graphical plot inside a virtual environment.

  1. Open PowerShell and create an empty folder called 'hello', navigate into this folder, and open it in VS Code:

  2. Once VS Code opens, displaying your new hello folder in the left-side Explorer window, open a command line window in the bottom panel of VS Code by pressing Ctrl+` (using the backtick character) or selecting View > Terminal. 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.

  3. Continue the tutorial in the VS Code docs: Create a Python Hello World source code file.

Create a simple game with Pygame

Pygame is a popular Python package for writing games - encouraging students to learn programming while creating something fun. Pygame displays graphics in a new window, and so it will not work under the command-line-only approach of WSL. However, if you installed Python via the Microsoft Store as detailed in this tutorial, it will work fine.

  1. Once you have Python installed, install pygame from the command line (or the terminal from within VS Code) by typing python -m pip install -U pygame --user.

  2. Test the installation by running a sample game : python -m pygame.examples.aliens

  3. All being well, the game will open a window. Close the window when you are done playing.

Here's how to start writing your own game.

  1. Open PowerShell (or Windows Command Prompt) and create an empty folder called 'bounce'. Navigate to this folder and create a file named 'bounce.py'. Open the folder in VS Code:

  2. Using VS Code, enter the following Python code (or copy and paste it):

  3. Save it as: bounce.py.

  4. From the PowerShell terminal, run it by entering: python bounce.py.

Try adjusting some of the numbers to see what effect they have on your bouncing ball.

Read more about writing games with pygame at pygame.org.

Resources for continued learning

We recommend the following resources to support you in continuing to learn about Python development on Windows.

Online courses for learning Python

  • Introduction to Python on Microsoft Learn: Try the interactive Microsoft Learn platform and earn experience points for completing this module covering the basics on how to write basic Python code, declare variables, and work with console input and output. The interactive sandbox environment makes this a great place to start for folks who don't have their Python development environment set up yet.

  • Python on Pluralsight: 8 Courses, 29 Hours: The Python learning path on Pluralsight offers online courses covering a variety of topics related to Python, including a tool to measure your skill and find your gaps.

  • LearnPython.org Tutorials: Get started on learning Python without needing to install or set anything up with these free interactive Python tutorials from the folks at DataCamp.

  • The Python.org Tutorials: Introduces the reader informally to the basic concepts and features of the Python language and system.

  • Learning Python on Lynda.com: A basic introduction to Python.

Working with Python in VS Code

  • Editing Python in VS Code: Learn more about how to take advantage of VS Code's autocomplete and IntelliSense support for Python, including how to customize their behavior... or just turn them off.

  • Linting Python: Linting is the process of running a program that will analyse code for potential errors. Learn about the different forms of linting support VS Code provides for Python and how to set it up.

  • Debugging Python: Debugging is the process of identifying and removing errors from a computer program. This article covers how to initialize and configure debugging for Python with VS Code, how to set and validate breakpoints, attach a local script, perform debugging for different app types or on a remote computer, and some basic troubleshooting.

  • Unit testing Python: Covers some background explaining what unit testing means, an example walkthrough, enabling a test framework, creating and running your tests, debugging tests, and test configuration settings.

Make without limit! Device Simulator Express, a Microsoft Garage project, allows you to code microcontrollers without the hardware on hand! You can program your Adafruit Circuit Playground Express (CPX), your BBC micro:bit or the Adafruit CLUE! Test and debug your code on the device simulator and see the sameresult when you plug in your actual microcontroller. Curious about the output of the device, the serialmonitor allows you to observe the device output.

Table of Contents

  • Adafruit Circuit Playground Express (CPX) Simulator
  • BBC micro:bit Simulator
  • Adafruit CLUE Simulator
  • How to use

Devices we support

Prerequisites

The following dependencies are required to install before launching Device Simulator Express.
You will be prompted to install the Python dependencies during the first use.

Python Programming With Microsoft Visual Studio Code For Mac
  • Python 3.7+: Make sure you've added Python and pip to your PATH in your environment variables. (1)
  • Python VS Code extension: This will be installed automatically from the marketplace when you install Device Simulator Express.

Adafruit Circuit Playground Express (CPX) Simulator

Features

  • IntelliSense and syntax highlighting for CircuitPython code for the CPX library
  • Template file generation
  • Integrated Python Debugging for the Simulator
  • Serial monitor (available on Windows and Mac only)
  • Output panel for the simulator
  • Deploy CircuitPython code to the physical device.
  • Simulation of the CPX device, including:
    • Green LED
    • Red LED
    • Push Buttons A and B
    • Slider Switch
    • Speaker: Play .wav file
    • 10 NeoPixels
    • Light sensor
    • Motion sensors
    • Acceleration detection
    • Device shake detection
    • Temperature sensor
    • 7 Capacitive Touch sensors

The simulator supports most of the sensors on CPX except IR transmitter & Receiver, Sound Sensor (microphone), Speaker (Play Tone) and the 'tap' on Motion Sensor.The code related to these sensors can still run on the actual CPX board and be deployed using Device Simulator Express.
As we only support CPX library now, other libraries (i.e. simpleio) can’t run on the simulator. But they will work on the actual device!

Useful Links

  • Tutorials and Example Code for Adafruit CPX:
  • Format Adafruit CPX device:

Keyboard Shortcuts

In Device Simulator Express, you can use keyboard to interact with the device:

  • Push Button: A for Button A, B for Button B, C for Buttons A & B
  • Capacitive Touch Sensor: Shift + 1 ~ 7 for GPIO pins A1 - A7
  • Slider Switch: Shift + S
  • Refresh the simulator: Shift + R
  • Run the simulator: Shift + F

BBC micro:bit Simulator

Features

  • IntelliSense and syntax highlighting for MicroPython code for the micro:bit library
  • Template file generation
  • Integrated Python Debugging for the Simulator
  • Deploy MicroPython code to the physical device
  • Serial monitor (available on Windows and Mac only)
  • Simulation of the micro:bit device, including:
    • 25 LEDs
    • Push Buttons A and B
    • Light sensor
    • Motion sensors
    • Acceleration detection including gesture detection
    • Temperature sensor

Useful Links

  • Tutorials and Example Code for BBC micro:bit:

Keyboard Shortcuts

  • Push Button: A for Button A, B for Button B, C for Buttons A & B
  • Refresh the simulator: Shift + R
  • Run the simulator: Shift + F

Adafruit CLUE Simulator

Features

  • IntelliSense and syntax highlighting for CircuitPython code for the following drivers and libraries:
    • adafruit_clue
    • adafruit_slideshow
    • adafruit_display_shapes
    • adafruit_display_text
    • adafruit_bitmap_font
    • adafruit_fancyled
    • neopixel
    • displayio
  • Template file generation
  • Integrated Python Debugging for the Simulator
  • Deploy CircuitPython code to the physical device
  • Serial monitor (available on Windows and Mac only)
  • Simulation of the CLUE device, including:
    • 240x240 color screen
    • Push Buttons A and B
    • Sensors for:
      • Temperature
      • Light
      • Color
      • Acceleration
      • Humidity
      • Pressure
      • Proximity
      • Gestures
      • Gyro
      • Magnetic Field

Useful Links

  • Tutorials and Example Code for Adafruit CLUE:

Keyboard Shortcuts

  • Push Button: A for Button A, B for Button B, C for Buttons A & B
  • Refresh the simulator: Shift + R
  • Run the simulator: Shift + F

How to use

To use Device Simulator Express, install the extension from the marketplace and reload VS Code.

To access many of the commands, you need to open the command palette. This can be done with Ctrl + Shift + P for Windows and Linux / Cmd + Shift + P for Mac. It can also be accessed from the toolbar by going to View -> Command Palette.

I. Take a look at the 'Device Simulator Express: Getting Started' Command.

  1. Type in 'Device Simulator Express: Getting Started' in the command palette (Ctrl + Shift + P / Cmd + Shift + P to open the command palette).
  2. Choose the the device you want to play with from the dropdown.
  3. Read, copy and learn some of the things you can do with the simulator!

II. Start with the 'Device Simulator Express: New File' Command.

  1. Type in 'Device Simulator Express: New File' in the command palette (Ctrl + Shift + P / Cmd + Shift + P to open the command palette).
  2. Select the device you want to use.
  3. Name and save your file somewhere, and we’re good to go!
  4. Start with some examples: you can find examples files and tutorials inside the comments at the top of the file.

III. Start from an existing Python file.

  1. Open the folder or your .py file in Visual Studio Code.
  2. Run Device Simulator Express: Open Simulator from the command palette or icon in the editor toolbar.
  3. Select the device you want to use.

IV. Run your code on the simulator.

  1. Run Run Simulator from the command palette or use the Play button on the simulator webview.

V. Deploy your code to the physical device

Before deploying the Python code to your CPX device, you need to format your device by following these tutorials:

  • For the CPX:

    • Download the firmware with the .uf2 file (link: https://learn.adafruit.com/adafruit-circuit-playground-express/circuitpython-quickstart).
    • Download the lastest versions of the cpx libraries (link: https://learn.adafruit.com/welcome-to-circuitpython/circuitpython-libraries).
  • For the micro:bit:

    • Download the firmware with the .hex file (link: https://microbit.org/get-started/user-guide/firmware/).
  • For the CLUE:

    • Download the latest versions of the cpx libraries and follow the instructions here (link:https://learn.adafruit.com/adafruit-clue/circuitpython).
  1. Plug in your device (make sure it’s formatted properly already).
  2. Run the command 'Device Simulator Express: Deploy to Device'.

VI. Use the Serial Monitor for your device (available on Windows and Mac only)

  1. Plug in your device (make sure it’s formatted properly already).
  2. Run the command 'Device Simulator Express: Open Serial Monitor'.
  3. Select your baud rate for the serial port.
  4. The print() statements in your code will show in the output console.

VII. Debug your project on the simulator

Python Programming With Microsoft Visual Studio Code For Mac Osx

  1. Add breakpoints in your code
  2. Press F5 to enter the debugging mode, and you can start debugging line by line!

Commands

Device Simulator Express provides several commands in the Command Palette (F1 or Ctrl + Shift + P / Cmd + Shift + P for Mac OS) for working with *.py files:

  • Device Simulator Express: Getting Started: Opens a page in VS Code that helps users get started with the extension. Here, users can browse through code that they can use to play with the simulators.
  • Device Simulator Express: Run Simulator: Runs Python code on the simulator.
  • Device Simulator Express: New File: Opens an unsaved .py file with template code, also opens the simulator for the selected device.
  • Device Simulator Express: Open Simulator: Opens the simulator in the simulator window for the selected device
  • Device Simulator Express: Deploy to Device: Copies the current file to the selected device.
  • Device Simulator Express: Open Serial Monitor: Opens the serial monitor in the integrated output window.
  • Device Simulator Express: Close Serial Monitor: Stops the serial monitor and releases the serial port.
  • Device Simulator Express: Change Baud Rate: Changes the baud rate of the selected serial port. For Adafruit CPX, the default baud rate is 115200.
  • Device Simulator Express: Select Serial Port: Changes the current serial port.

Contribute

Provide feedback

To add a review for our extension, please do so on the Visual Studio Marketplace

Python Programming With Microsoft Visual Studio Code For Mac

To report issues, provide feedback or requests, please use this link: Provide Feedback.
We would love to hear from you about your experience to keep improving our project.

Privacy and Telemetry Notice

Data Collection

The software may collect information about you and your use of the software and send it to Microsoft. Microsoft may use this information to provide services and improve our products and services. You may turn off the telemetry as described in the repository. There are also some features in the software that may enable you and Microsoft to collect data from users of your applications. If you use these features, you must comply with applicable law, including providing appropriate notices to users of your applications together with a copy of Microsoft's privacy statement. Our privacy statement is located at https://go.microsoft.com/fwlink/?LinkID=824704. You can learn more about data collection and use in the help documentation and our privacy statement. Your use of the software operates as your consent to these practices.

Disable Telemetry

The Microsoft Device Simulator Express Extension for Visual Studio Code collects usagedata and sends it to Microsoft to help improve our products andservices. Read ourprivacy statement tolearn more. This extension respects the telemetry.enableTelemetrysetting which you can learn more about athttps://code.visualstudio.com/docs/supporting/faq#_how-to-disable-telemetry-reporting.

To disable telemetry, follow these steps:

  1. Open File (Open Code on macOS)
  2. Select Preferences
  3. Select Settings
  4. Search for telemetry
  5. Uncheck the Telemetry: Enable Telemetry setting

Third Party Notice

A ThirdPartyNotices.txt file is provided in the extension's source code listing the appropriate third-party notices.

Python Programming With Microsoft Visual Studio Code For Mac Download

Troubleshooting Tips

  • The first time you install the extension, you'll need to execute the run command at least once in order to access auto-completion.
  • While running a code file, if you get an error saying it can't find the file, make sure you've clicked on a valid Python code file before running it.
  • To open the output panel again after closing it go to VS Code menu: View -> Output.
  • If you try to deploy to the CPX while it's plugged in but you still get an error saying it cannot find the board, make sure your device is formatted correctly and that its name matches CIRCUITPY.
  • If you can't get the Simulator communication working while debugging, try to open your Settings and check the port used under 'Device Simulator Express: Debugger Server Port'. You can either change it (usually ports above 5000 should work) or try to free it, then start debugging again.
  • When you are using the serial monitor, if you get some unusual error messages, unplug the device and reload the VS Code windows.
  • If you're using Ubuntu and having some problems with setting up the environment, try reviewing this article's 'Step 1' section on how to set up Python 3 on Ubuntu 16.04. Then, ensure that you've run sudo apt-get install -y python3-venv to allow for virtual environment creation.

License

Notes

Python Programming With Microsoft Visual Studio Code For Mac 64-bit

  1. Make sure that when you type python in a terminal, the command is recognized and you have the correct version. The easiest way to do it is to select the 'Add to PATH' option directly when you install Python. Otherwise you can search how to insert it manually.
  2. You can choose to see the prompt or not by changing the extension configurations.