본문 바로가기

카테고리 없음

Python Programming With Microsoft Visual Studio Code For Mac



Get a handle on working with Python 2 and 3 Be able to use the in-built Python modules for their own projects Beginners and children will be able to create their own projects and advance to more complicated concepts Use Microsoft Visual Studio Code-a powerful IDLE for practical programming. Download this app from Microsoft Store for Windows 10, Windows 8.1. See screenshots, read the latest customer reviews, and compare ratings for Python Programs.

Mar 07, 2017  There has no option to create C++ project in this version and some other community members reported this suggestion to the Visual Studio Product Team, please check this: Support C++ in Visual Studio for Mac and you can vote it, then waiting for the feedback from the Visual Studio. Visual Studio for Mac enables the creation of.NET Core solutions, providing the back-end services to your client solutions. Code, debug, and test these cloud services simultaneously with your client solutions for increased productivity. Visual studio for mac c++.

Active1 year, 1 month ago

I cannot seem to get Python3 to work when debugging it continues to use Python2. I've tried a few things but still am unable to get it work. I verified Python3s path is /usr/local/bin/python3Everytime I run a script to by

I still get 2.7.10python 3 is installed and work correctly.

I have this in my launch.json and settings.json (type and request are both grayed out for some reason in the launch.json)

Suraj Rao

25.3k8 gold badges65 silver badges76 bronze badges
SeanJohnSeanJohn

4 Answers

To select a specific python interpreter, invoke the Python: Select Interpreter command from the Command Palette (⇧⌘P).

This will update your .vscode/settings.json file:

You should then be able to debug using python 3 interpreter or any other python interpreter is available on your machine.

Microsoft Visual Studio Community

kimbaudikimbaudi

3,2785 gold badges28 silver badges42 bronze badges

This is not an answer, but my rep won't allow a comment:For me your proposed solution worked just fine. Here some troubleshooting:

  • Did you change the original launch.json entry? Then you probably need to reload it, which should happen automatically, but restarting VSCode will do that for sure.
  • Also, I would recommend using the 'Add Configuration' button, then choose 'python', and edit the name in the new entry (from 'Python' to 'Python3') as not to clash with the old python entry, as well as change the 'pythonPath' key's value from '${config:python.pythonPath}' to '/usr/local/bin/python3'

To use this config, select in the Debug dropdown box.

TascheTasche

if you use 'code runner',you can try to open user settings with command palette.

it's work for me.

V-rund Puro-hit

4,3768 gold badges21 silver badges47 bronze badges
謝騰緯謝騰緯

What worked for me is adding the following to User Settings:

kimbaudi

3,2785 gold badges28 silver badges42 bronze badges

Lech MigdalLech Migdal

1,9352 gold badges22 silver badges41 bronze badges

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

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

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.
  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.

Microsoft Visual Basic

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

Programming with microsoft visual basic nested if statement

Eco StropheEco Strophe

In the latest version (1.36) of VS Code (Python): No option for asio4all in fl studio 12 mac producer.

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:

Microsoft Visual Studio Express

  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:

Programming With Microsoft Visual Basic 2012 Sixth Edition Diane Zak

  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.

LyX2394LyX2394

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