Python Programming With Microsoft Visual Studio Code For Mac

The Visual Studio Code team has fully embraced the Python programming language, to the point of hiring the original developer of a popular Python extension, taking over the project and hiring even more Python coders to further develop it. Now, Microsoft and Anaconda Inc. Have announced VS Code is. First, install Visual Studio Code and download Mono 4.2.x (for Linux and Mac). Then get the latest Azure Data Lake Tools by going to the VSCode Extension repository or the VSCode Marketplace and searching “ Azure Data Lake Tools ”. Developer Community for Visual Studio Product family. Thank you for taking the time to provide your suggestion. We will do some preliminary checks to make sure we can proceed further.

The most important thing to say is that this is not Visual Studio - not even close - despite the name that attempts to make the connection. This is a brand new cross platform IDE - the real question is why?

Microsoft already has more IDEs than it really needs. There is the excellent Visual Studio - the flagship; then there is Blend, a designer- oriented IDE; and there is WebMatrix 3, which covers some of the same ground as the new Visual Studio Code, but only under Windows.

At the moment the most important thing to say about the latest addition is that it fairly underwhelming. It might be a useful IDE in a few releases time, but at the moment it is just a start on an IDE.

What has made it possible is the work that the .NET languages team did to move from the old compiler infrastructure to the new Roslyn compiler. The entire IDE can be thought of as a locally hosted web app.

'Architecturally, Visual Studio Code combines the best of web, native, and language-specific technologies. Using the GitHub Electron Shell, Code combines web technologies such as JavaScript and Node.js with the speed and flexibility of native apps. Code uses a newer, faster version of the same industrial-strength HTML-based editor that has powered the “Monaco” cloud editor, Internet Explorer's F12 Tools, and other projects.'

The presentation layer is implemented by a customized Google Chrome rendering engine.

Take a look at the video to discover how Microsoft presents VSCode:

What is difficult to find out is exactly what sorts of projects VSCode supports. If you define supports as offering IntelliSense prompting then the list is quite short:

JavaScript, JSON, HTML, CSS, LESS, SASS

If you relax the requirements to syntax coloring then you get:

C++, jade, PHP, Python, XML, Batch, F#, DockerFile, Coffee Script, Java, HandleBars, R, Objective-C, PowerShell, Lua, Visual Basic, Markdown

In fact there are only two languages that get full support and these are C# and TypeScript/Node.js. The target project types are ASP.NET 5 and Node.js.

In fact if you take it down to what types of project can you debug on all platforms then the answer is just Node.js.

Out of the box Code doesn't actually create any projects for you. Projects are defined as what is found in a folder and the project structure is defined by either a suitable json file or an ASP.NET5 solution/project files.

How are you supposed to create a project?

You could use the full Visual Studio to create a project structure and then simply load this into VSCode but this seems self defeating.

The suggested solution is to use Express to generate Node.js projects and yeoman for ASP.NET5. You have to download, install and run these separately. Not a huge problem but I thought IDE stood for INTEGRATED Development Environment. In fact once you start to look at how you might use VSCode you discover that it is not so much an Integrated Development Environment but a selection of tools flying in close formation. The list of additional tools reads like a whos who of FOSS tools - git, Express, gulp, mocha, bower etc. You care supposed to be install and master what ever of these you need to use.

This approach may well appeal to the existing FOSS user, but on the other hand VSCode being from Microsoft isn't likely to appeal much to this audience. What the existing, pampered, Visual Studio user will make of this mess of tools is fairly clear - and this is what makes the use of the name 'Visual Studio' misleading in the extreme.

To add to the injury, if you actually want to run the program you have developed you first have to set up a launch configuration. Also note that, at the moment, debugging of ASP.NET5 isnt' supported on OSX and Linux. So it's not that cross platform at the moment and certainly not a cross platform IDE, more of a cross platform editor with some syntax highlighting/prompting. You can fairly easily debug Node.js applications, but as Node.js is already cross platform, this is hardly an achievement.

I can't see very many programmers rushing to use VSCode as its only real advantage is IntelliSense prompting for JavaScript. What is really important about VSCode, and the reason for the many headlines proclaiming 'Visual Studio Comes to Linux and Mac', which is clearly nonsense, is (to misquote Samuel Johnson):

'Sir, Microsoft doing cross platform is like a dog walking on his hind legs. It is not done well; but you are surprised to find it done at all'

Python Programming With Microsoft Visual Studio Code For Mac

VSCode is the result of a number of deep changes in Microsoft - the creation of the Roslyn compiler, the focus on Cloud services, and the willingness to use and support open source coded.

It will be interesting to see what happens to VSCode in the future, but for now it is a curiosity best left to programmers with time to spare.

More Information

Related Articles

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter,Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.

Scratch 3 Desktop for Raspbian on Raspberry Pi Released
19/08/2019

The Raspberry Pi Foundation has released an offline implementation of Scratch 3.0 than runs under Raspbian Buster and performs best on the 2GB Raspberry Pi 4.


Git Adds Switch And Restore
21/08/2019

The latest version of Git, the distributed version control system, has been released with around 500 changes, including two new sub-commands that are designed to provide an experimental alternative to [ ... ]


More News


Comments


Python Programming With Microsoft Visual Studio Code For Mac

or email your comment to: comments@i-programmer.info

Active4 days ago

Visual Studio Code was recently released and I liked the look of it and the features it offered, so I figured I would give it a go.

I downloaded the application from the downloads page fired it up, messed around a bit with some of the features ... and then realized I had no idea how to actually execute any of my Python code!

I really like the look and feel/usability/features of Visual Studio Code, but I can't seem to find out how to run my Python code, a real killer because that's what I program primarily in.

Does anyone know if there is a way to execute Python code in Visual Studio Code?

RPiAwesomenessRPiAwesomeness
1,8244 gold badges24 silver badges42 bronze badges

21 Answers

You can add a custom task to do this. Here is a basic custom task for Python.

You add this to tasks.json and press CTRL + SHIFT + B to run it.

FentonFenton
167k47 gold badges304 silver badges332 bronze badges

There is a much easier way to run Python, no any configuration needed:

  1. Install the Code Runner Extension
  2. Open the Python code file in Text Editor, then use shortcut Ctrl+Alt+N, or press F1 and then select/type Run Code, the code will run and the output will be shown in the Output Window.

If you want to add Python path, you could Go to File->Preference->Settings, and add Python path like below:

Jun HanJun Han
7,0116 gold badges20 silver badges25 bronze badges

Here is how to Configure Task Runner in Visual Studio Code to run a py file.

In your console press Ctrl+Shift+P (Windows) or Cmd+Shift+P (Apple) and this brings up a search box where you search for 'Configure Task Runner'

EDIT: If this is the first time you open the 'Task: Configure Task Runner', you need to select 'other' at the bottom of the next selection list.

This will bring up the properties which you can then change to suit your preference. In this case you want to change the following properties;

  1. Change the Command property from 'tsc' (TypeScript) to 'Python'
  2. Change showOutput from 'silent' to 'Always'
  3. Change args (Arguments) from ['Helloworld.ts'] to ['${file}'] (filename)
  4. Delete the last property problemMatcher
  5. Save the changes made

You can now open your py file and run it nicely with the shortcut Ctrl+Shift+B (Windows) or Cmd+Shift+B (Apple)

Enjoy!

python_starterpython_starter

All these answers are obsolete now.

Currently you have to:

  1. install Python language extension (and python, obviously)
  2. open folder (important!), open any python file inside that folder
  3. switch to debug 'tab'(?) and click on the gearbox (with hint 'Configure of Fix 'launch.json')
  4. save opened launch.json file (it's placed in .vscode subdir in the folder opened on step #2)
  5. finally, click green triangle or hit F5

No additional extensions or manual launch.json editing is required now.

vlad2135vlad2135

To extend @vlad2135's answer (read his first); that is how you set up python debugging in VSCode with Don Jayamanne's great python extension (Which is a pretty full featured IDE for python these days, and arguably one of VS code's best language extensions IMO).

Basically when you click the gear icon, it creates a launch.json in your .vscode directory in your workspace. You can also make this yourself, but it's probably just simpler to let VSCode do the heavy lifting. Here's an example file:

You'll notice something cool after you generate it. It automatically created a bunch of configurations(most of mine are cut off, just scroll to see them all) with different settings and extra features for different libraries or environments (like django). The one you'll probably end up using the most is python; which is a plain (in my case C)Python debugger, and easiest to work with settings wise. I'll make a short walkthrough of the json attributes for this one, since the others use the pretty much same configuration with only different interpreter paths and one or two different other features there.

  • name: The name of the configuration. A useful example of why you would change it is if you have two python configurations which use the same type of config, but different arguments. It's what shows up in the box you see on the top left (my box says 'python' since I'm using the default python config).
  • type: Interpreter type. You generally don't want to change this one.
  • request: How you want to run your code, and you generally don't want to change this one either. Default value is 'launch', but changing it to 'attach' allows the debugger to attach to an already running python process. Instead of changing it, add a configuration of type attach and use that.
  • stopOnEntry: Python debuggers like to have an invisible break-point when you start the program so you can see the entry-point file and where your first line of active code is. It drives some C#/Java programmers like me insane. false if you don't want it, true otherwise.
  • pythonPath: The path to your install of python. The default value gets the extension level default in the user/workspace settings. Change it here if you want to have different pythons for different debug processes. Change it in workspace settings if you want to change it for all debug processes set to the default config in a project. Change it in user setting to change where the extension finds python across all projects. (4/12/17 The following was fixed in extension version 0.6.1). Ironically enough, this gets auto-generated wrong. It auto-generates to '${config.python.pythonPath}' which is deprecated in the newer VSCode versions. It might still work, but you should use '${config:python.pythonPath}' instead for your default first python on your path or VS settings. (4/6/17 Edit: This should be fixed in the next release. The team commited the fix a few days ago.)
  • program: The initial file that you debugger starts up when you hit run. '${workspaceRoot}' is the root folder you opened up as your workspace (When you go over to the file icon, the base open folder.) Another neat trick if you want to get your program running quickly, or you have multiple entry points to your program is to set this to '${file}' which will start debugging at the file you have open and in focus in the moment you hit debug.
  • cwd: The current working directory folder of the project you're running. Usually you'll just want to leave this '${workspaceRoot}'.
  • debugOptions: Some debugger flags. The ones in the picture are default flags, you can find more flags in the python debugger pages, I'm sure.
  • args: This isn't actually a default configuration setting, but a useful one nonetheless (and probably what the OP was asking about). These are the command line arguments that you pass in to your program. The debugger passes these in as though they you had typed: python file.py [args] into your terminal; passing each json string in the list to the program in order.

You can go here for more information on the VSCode file variables you can use to configure your debuggers and paths.

You can go here for the extension's own documentation on launch options, with both optional and required attributes.

You can click the 'Add Configuration' button at the bottom right if you don't see the config template already in the file. It'll give you a list to auto generate a configuration for most of the common debug processes out there.

Now, as per vlad's answer, you may add any breakpoints you need as per normal visual debuggers, choose which run configuration you want in the top left dropdown menu and you can tap the green arrow to the left to the configuration name to start your program.

Pro tip: Different people on your team use different IDE's and they probably don't need your configurations files. VSCode nearly always puts it's IDE files in one place (by design for this purpose; I assume), launch or otherwise so make sure to add .vscode/ to your .gitignore if this is your first time generating a VSCode file(This process will create the folder in your workspace if you don't have it already)!

RMSDRMSD

There is a Run Python File in Terminal command available in the Python for VS Code extension.

kenwarnerkenwarner
19.4k24 gold badges114 silver badges162 bronze badges

As stated in Visualstudio Code Documentation, just right-click anywhere in the editor and select Run Python File in Terminal.

AndreaBAndreaB

So there're 4 ways to run Python in VSCode so far:

  1. Via an integrated terminal (c'mon it's integrated! So technically you run it from within the VSCode ;)
    • No need to install any extension.
    • No need to create and configure anything (assuming that you already have python in your $PATH).
    • ⌃Space (open terminal) and python my_file.py (run file).
  2. Via custom Task (accepted @Fenton's answer):
    • No need to install any extension.
    • Default VSCode's way of doing things.
    • Beware not to copy-paste the answer because its problemMatcher.pattern.regexp is broken and it hangs the editor. It's better to either delete problemMatcher or change the regexp to at least ^s+(.*)$.
  3. Via Code Runner extension (@JanHan's answer):

    • Need to configure code-runner.executorMap in User Settings (add path to your python).
    • Very helpful extention especially if you run not only Python in VSCode.
  4. Via Microsoft's official Python extension (@vlad2135's answer):
    • Need to create launch.js (a couple of clicks in VSCode's Debug tab).
    • The extension is a must-have for those who wants to use VSCode as a primary IDE for Python.
Nikolay KulachenkoNikolay Kulachenko

You no longer need any additional extensions. You can simply switch the output of the debugger to the integrated terminal.

Ctrl+Shift+D, then select Integrated Terminal/Console from the dropdown at the top.

Nathan Tuggy
2,2049 gold badges25 silver badges35 bronze badges
NickDNickD
  1. Install the Python extension(Python should be installed in your system). To install the Python Extension press Ctrl+Shift+X and then type 'python' and enter. Install the extension.

  2. Open the file containing python code. Yes! .py file.

  3. Now to run the .py code, simply right click on the editor screen and hit 'Run Python File in the Terminal'. That's it!

Now this is the additional step Actually I got irritated of clicking again and again so I setup the Keyboard Shortcut.

  1. Hit that Settings-type-looking-like icon on bottom-left side -> Keyboard Shortcuts -> type 'Run Python File in the Terminal'. Now you will see that + sign, go choose your shortcut. You're Done!
Paul Roub
33.3k8 gold badges62 silver badges77 bronze badges
Saloni TayalSaloni Tayal
Visual

If you are using the latest version of vs code (version 1.21.1). The task.json format has changed, see here. So the answer by @Fenton and @python_starter may no longer be valid.

Before you start configuring vs code for running your python file.

  • Make sure that you have installed Python and added its executable to your system PATH.
  • You must set the folder where your python source file resides as your working folder (go to File -> Open Folder to set your working folder).

Now you can configure the task. The following steps will help you run your python file correctly:

  1. use Ctrl+Shift+P and input task, you will see a list of options, select Tasks: Configure Task.

Microsoft Visual Studio Download

  1. You will then be prompted create task.json from template, choose this option, and you will be prompted to choose from a list of options. Choose Others.
  1. Then in the opened task.json file, use the following settings:

    In the above settings, you can give a meaningful label to this task. For example, run python.

  2. Go to the Tasks menu and click Run Task. You will be prompted to choose the task. Just choose the newly created run this script task. You will see the result in the TERMINAL tab.

For a more complete tutorial about task configuration, go to vs code official documentation.

jdhaojdhao
5,8752 gold badges36 silver badges61 bronze badges

Super simple:
Press F5 key and the code will run. If a breakpoint is set, pressing F5 will stop at the breakpoint and run the code in Debug mode.

user2371563user2371563

Here's the current (September 2018) extensions for running python:

Official python extension: This is a must install.

Code Runner: Increadibly useful for all sorts of languages, not just python. Would highly reccomend installing.

AREPL: Real-time python scratchpad that displays your variables in a side window. I'm the creator of this so obviously I think it's great but I can't give a unbiased opinion ¯_(ツ)_/¯

Wolf: Real-time python scratchpad that displays results inline

And of course if you use the integrated terminal you can run python in there and not have to install any extensions.

AlmenonAlmenon

A simple and direct Python extension would save both time and efforts.Linting, debugging, code completion are the available features once installation is done. After this, to run the code proper Python installation path needs to be configured in order to run the code. General settings are available in User scope and Workspace can be configured for Python language– 'python.pythonPath': 'c:/python27/python.exe' With above steps at least the basic Python programs can be executed.

w1n5rxw1n5rx

From Extension install Code Runner. After that you can use the shortcuts to run your source code in Visual Studio Code.

First: To run code:

  • use shortcut Ctrl+Alt+N
  • or press F1 and then select/type Run Code,
  • or right click the Text Editor and then click Run Code in editor context menu
  • or click Run Code button in editor title menu
  • or click Run Code button in context menu of file explorer.

Second: To stop the running code:

  • use shortcut Ctrl+Alt+M
  • or press F1 and then select/type Stop Code Run
  • or right click the Output Channel and then click Stop Code Run in context menu
wjandrea
3,4444 gold badges15 silver badges34 bronze badges
Eco StropheEco Strophe

In the latest version (1.36) of VS Code (Python):

Press F5 then hit Enter to run your code in the integrated terminal.

CTRL+A then hit SHIFT+Enter to run your code in interactive IPython Shell.

MI AlamMI Alam

If you are running a code and want to take input via running your program in the terminal. best thing to do is to run it in terminal directly by just right click and choose 'Run python file in terminal'.

Fustock leonvaesrFustock leonvaesr

in order to launch the current file with respective venv i added this to launch.json

in the bin folder resides the source .../venv/bin/activate script regularly sourced when running from regular terminal

qrtLsqrtLs

I had installed python via Anaconda. By starting VS code via anaconda I was able to run python programs. However, I couldn't find any shortcut way (hotkey) to directly run .py files.

(using the latest version as of Feb 21st 2019 with the Python extension which came with VS Code.Link: https://marketplace.visualstudio.com/items?itemName=ms-python.python )

Following worked:

  1. Right clicking and selecting 'Run python file in terminal' worked for me.
  2. CTRL + A then SHIFT + ENTER (on windows)

The below is similar to what @jdhao did.

This is what I did to get the hotkey:

  1. CTRL + SHIFT + B //run build task
  2. It gives option to configure
  3. I clicked on it to get more options. I clicked on Other config
  4. A 'tasks.json' file opened

I made the code look like this:

After saving it, the file changed to this:

  1. After saving the file 'tasks.json', go to your python code and pressCTRL + SHIFT + B.
  2. Then click on Run task -> Run Python File //this is the label thatyou gave.

Now every time that you press CTRL + SHIFT + B, the python file will automatically run and show you the output :)

HemangHemang

If you have a project consisting of multiple python files and you want to start running/debugging with the main program independent of which file is current you create the following launch configuration (change MyMain.py to your main file)

rioV8rioV8
4,9902 gold badges5 silver badges13 bronze badges

I use Python 3.7 (32 bit). To run a program in Visual Studio Code, I right-click on the program and select 'Run Current File in Python Interactive Window'. If you do not have Jupyter, you may be asked to install it.

Programming With Microsoft Visual Basic 2012 Sixth Edition Diane Zak

LyX2394LyX2394

Not the answer you're looking for? Browse other questions tagged pythonvisual-studio-code or ask your own question.

Comments are closed.