Setting Up Visual Studio Code For Python On macOS

I recently decided to learn Python language for one of my project. Currently I am in the process of setting up my macOS system for Python dev environment. I have installed the latest version of Python 3.x on my MacBook Pro. As I am already using VS Code IDE, I’m thinking of using it for Python programming. Here, I will share with you the steps I took for setting up Visual Studio Code for Python on macOS.

Tools & Technologies Used

  • macOS Mojave (10.14.5)
  • Visual Studio Code (1.36.0)
  • Python (3.7.3)

Steps For Setting Up VS Code For Python

Prerequisites

  1. Install the latest 3.x version of Python. You can find the installation steps here.
  2. If VS Code is not already available on your mac, you can download and install it from https://code.visualstudio.com/. Now go for the next step of installing the Python extension for VS Code.
  3. Launch VS Code and go to extension tab. Search for python in the extensions market place. You can find several Python extensions in the market place. Install the extension named as Python which is maintained by Microsoft. Most probably it will be on the top of the list.
    Python extension for VS Code

Choosing The Python Interpreter

  1. Launch VS Code.
  2. Open a folder for Python workspace by going to Files >> Open … >> Select or create a folder if the finder window and open it. This will be your Python workspace.
  3. Now, open the Command Palette, by pressing Shift, Command and P keys.
  4. In the Command Palette, type in and select Python: Select Interpreter.
  5. From the list of interpreters, select the appropriate Python 3.x interpreter. This action will create folder .vscode inside your workspace. Inside this .vscode folder, you can find settings.json file which stores the settings specific to the workspace. In our case you can find the Python interpreter path.
    Python Settings in VS Code workspace
  6. At the bottom left cornet of VS Code window, you can see the Python version as well.
    Python version at the bottom of VS Code window

Now Visual Studio Code is ready for interpret and run Python code. It is time to create a Python source code in VS Code and check it.

Create Your First Python Code In VS Code

  1. In the Explorer panel, press the New File icon against the workspace folder. Enter the name of the file with extension .py. I have named it as first.py.
    First Python source code file in VS Code
  2. In the newly created file, enter some python code. For example, I used the code below, where a string value is assigned to a variable and the variable is passed as a parameter to print function. While typing the source code, you can see the intellisense kicking in showing you the autocomplete and function param value functionality. This is a indication, that the settings we have done before were correct.
    message = “Hello Python World!..”
    print(message)

    my first python code
  3. To run this code, right-click somewhere in the editor and select Run Python File in Terminal.
  4. This action will open the terminal panel at the bottom and run the python file against the selected interpreter. The result will be like this:
    run python file in terminal

Configuring The Debugger

We have completed the setups required to run the Python source code. Now we will go over the configurations required for debugging Python

  1. In VS Code, press the debug icon in the sidebar.
  2. Then press the configure icon.
    Setting up python debug configuration
  3. From the Command Palette, select Python File.
    Select Python FIle From debug config
  4. This action will create a configuration file called launch.json under the .vscode folder in the workspace. By default this file will have the most common Python debug configuration settings.
    launch.json file

A basic Python debugger configuration is now complete. VS Code is now ready for debugging Python code. You can edit the launch.json file and modify or add configuration settings. Read more about debugger configuration here. Now we will see how to use the debugger

Using The Python Debugger

  1. Open the Python file.
  2. Set a breakpoint on the line having the print comment, by pressing the left gutter just before the line number.
  3. Press the debug icon at the sidebar to open the debug panel.
  4. Press the debug green arrow at the top of the debug panel.
    Starting the python debugger
  5. The debugger will start running and stop at the breakpoint. By this time, you will notice some changes in the editor. A debug tool bar will appear on top, which you can use for continue, step over, step into, step out, restart and stop the debugging. In the debug panel you can see the variables and their values. A Python Debug Console appears at the bottom. The status bar changes color to orange.
  6. If you hover the mouse pointer on a variable in the editor window, it will show the value stored in it.
    Debugging Python code in VS CODE
  7. Press continue icon in the debug tool bar to finish debugging and get the result in the terminal console.

Conclusion

We have covered setting up a python workspace in Visual Studio Code on macOS along with basic debugging configurations.

Next Step


2 thoughts on “Setting Up Visual Studio Code For Python On macOS”

  1. Please disregard my previous message. Despite the warning after doing the system install and after updating /etc/paths, I was able to complete this tutorial exactly as you have it. Sorry.

    Reply
  2. This no longer works as of VS Code 1.48.2 and Python 3.85. Microsoft advises on the Python plugin install page that Python itself needs to be installed through Homebrew rather than the package installer from the website (as you show in your Python install guide). So unfortunately, this guide no longer works from the point you say “right-click somewhere in the editor.”

    Reply

Leave your thoughts...

This site uses Akismet to reduce spam. Learn how your comment data is processed.