Visual Studio Code Mac Python



I want to install pip for python 2.7 on my Mac. I think this is the python located in /usr/bin/python. Unfortunately I have already installed Anaconda, which installs python 3.6.3, and changes things so that the command python xxx.py automatically runs xxx.py using python 3.6.3. Visual Studio Code supports the following categories in linting: Hint, Error, Information, Warning By default the extension maps pylint “convention” to “Hint”, and so on. These mappings can be altered either in the User or Workspace settings files as follows: 'python. 1 day ago  We are pleased to announce that the March 2021 release of the Python Extension for Visual Studio Code is now available. This release largely focused on bug fixes and stabilization work in our Python, Pylance and Jupyter extensions, and includes a preview of improved Jedi language server support for our Python Insiders. Download Visual Studio Community, Professional, and Enterprise. Try Visual Studio IDE, Code or Mac for free today.

Visual Studio Code has the ability to debug mixed Python with C++ extensions. In this blog post, I give an example of how to get it working.

I’m going to do the example from scratch in five steps:

  1. Make virtual environment. Chances are that, if you’re doing this kind of thing, you’ll be wanting to use a virtual environment too.
  2. Write code. My toy example is a C++ extension that just adds two numbers together.
  3. Set up VS Code project. Important to point the interpreter to virtual environment.
  4. Create launch.json. Configure the debugger so it can both run on python launch and attach to C++.
  5. Run two debuggers at once. How to pause the debugger and switch from python to C++ (with screenshots).

Step 1. Make virtual environment

Activating the environment changes the prompt as a reminder

I’ll be working from within the virtual environment from here on.

Step 2. Write example code

Write the C++ extension.

Inside myadd.cpp:

Write the python we’d want to use that calls the C++

Inside myscript.py, I write very simple code that imports myadd and uses it to add 5 and 6 together.

Write the setup script.

Inside setup.py:

Run the setup script.

Now myadd is available as a module in this virtual environment.
We can check that it works by running the script.

Step 3: Basic setup of VS Code project

Fire up VS Code while in the virtual environment

The first step is to choose the virtual environment’s Python interpreter as our default Python interpreter. Hit ctrl + shift + p, and choose ./bin/python (or wherever you put your virtual environment).

click images to make bigger

This will create the settings.json, which will look something like this:

Step 4: Create launch.json

The launch.json file is used to configure the debugger in VS Code. In this file, we need to tell VS Code that there are two ways we want to run the debugger: (1) for the Python script, on launching the file; and (2) for the C++ portion, we want to attach that to a Python debugger process that’s already going.

The following code goes in .vscode/launch.json, or you can create it from within the VS Code gui by clicking on the debugger icon (the little bug) and clicking on the blue link that says “create a launch.json file”.

Step 5: Run two debuggers at once

Mac

Add a debug breakpoint in the Python script before it drops into myadd(), and add another one in myadd.cpp at the line z = x+y.

First, we’ll start the Python debugger. While focused on myscript.py, click the debugger icon, then click on the little green right-arrow icon next to “Python: Current file”.

click images to make bigger

The debugger will work through myscript.py and stop at the first breakpoint.

While the debugger is paused, we will need to start the second debugger and attach it to the Python debugger process.

First we identify the Python process. Open a new terminal, run ps aux | grep python, and look for the process that has the token. In my case, it’s process number 4482.

Go back to VS Code, and focus on the file myadd.cpp. Now use the drop-down to choose “(gdb) Attach”, then click the green play button.

The debugger will then ask you to choose which process to attach the debugger to. Type in the process number from before, and it will find the Python process.

Visual Studio Code Mac Python

[note: a Stackoverflow question about issues at this step]

In the terminal, VS Code will tell you that superuser access is required to attach to a process. Type in Y and enter the root password.

Python In Visual Studio

Now when you click continue in the debugging process (blue play button), the debugger will drop into the cpp file and you will be able to do the usual debugging things.