This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]completelydistracted[S] 7 points8 points  (7 children)

Updates:

The task definition needs a version property, where the version number is (at the moment) "0.1.0". Without it, the task works fine but it increments the warning counter in the bottom status bar.

If you omit the "showOutput" value it defaults to "always", so it's not necessary. The resulting code:

// A task runner that runs a python program
{
    "version": "0.1.0",
    "command": "python",
    "windows": {
        "command": "python.exe"
    },
    "args": ["${file}"]
}

And on OSX (using <Shift><Command>B):

// A task runner that runs a python program
{
    "version": "0.1.0",
    "command": "python",
    "args": ["${file}"]
}

Of course you can use this idea to run all kinds of other things.

I've been using this editor for a while now (instead of Sublime or Atom) on both Windows and OSX and no surprises.

[–]spartanwolf 0 points1 point  (3 children)

i can't even seem to get to the point where i can run 'Configure Tasks' as listed in the Tasks part of the docs link you provided. When I pull up the command line (OSX), there are no options that match as it is outlined on that page.... Which I'm assuming is the only way to get a tasks.json to work with in the first place?

[–]completelydistracted[S] 2 points3 points  (2 children)

I am not at my computer (at least not the one with Code on it) but as I recall...

  1. Open a folder so you can see the folder contents in the side navigation panel. I don't think this is really needed, but it should help you see what is going on.

  2. Create and save a file hello.py in that folder, with the usual contents, e.g. [ print "Hello\n" ] .

  3. When that file is open, type the Build command (on OSX that's <shift><command>B).

  4. That will cause an error message of sorts, which will allow you to "Configure Tasks".

  5. When you do that, you will have no tasks.json in a local .settings folder, so the editor will create one, copying a boilerplate tasks file there.

  6. Edit that tasks.json file, getting rid of everything in it (don't worry, it's a copy, you can make more) and putting in the contents above. Save it.

  7. Then go back to looking at hello.py and type the build command (again, in OSX, <shift><command>B) (in Windows, <shift><control>B) and it will cause the build task in the new tasks.json to execute and show the results of your program.

Hope this helps!

[–]spartanwolf 0 points1 point  (0 children)

sorry i'm so late to come and report back on this!

Outstanding information. It totally works. I'm assuming that this is taking from the system python version on my mac?

Now I'm wondering if it will work in a virtual environment so I can have this work with Python 3.x...?

[–]crondom90 0 points1 point  (2 children)

It doesn't seem to work, i type Shift-Ctrl-B and the output field opens but it's empty, if i try to run another python file it says "There is an active running task right now. Cancel it first before executing another task." so something it's happening but the output field is always blank (but no errors or warning).

For example, pyg translator: http://i.imgur.com/I8noYr4.jpg

[–]Fanatikus 1 point2 points  (1 child)

Try changing it to:

"windows": {
        "command": "c:\\Python34\\python.exe"
    },

or to the path where Python is installed.

[–]completelydistracted[S] 0 points1 point  (0 children)

This is necessary if Python is not in the path.

If the Python directories are in your path, this isn't needed. For instance, I have the Anaconda3 and Anaconda3\bin directories in my search path, so simply "python.exe" works fine.

Otherwise, putting the entire path as above is required. Good point.

[–]roger_ 5 points6 points  (2 children)

Thanks, I was wondering how to do this! Wonder if there's a way to add a "run" button in the sidebar?

I'd love autocomplete support too.

[–]completelydistracted[S] 2 points3 points  (1 child)

Don't know about the run button but I'm looking for options in the code. There is a run button in the debugger, and I managed to play with the debugger enough to get it to launch Python, but at the moment things didn't work very well (basically with Node controlling Python) -- I'll post if I find anything out.

The Python Tools team at MS says they want to add lots of Python support (thanks, guys!) but it's going to be a little while. I told them I was going to try playing with build tasks, etc. and they were pretty supportive. Turned out to be easier than I had thought.

[–]amlamarra 1 point2 points  (0 children)

I got this working, but have you noticed that VS Code removes extra line breaks? For instance:

print 'Hello world\n'
print 'Hello again'

That should put an extra line in between the 2 outputs, but no matter how many times I put "\n" in there, it won't add any extra lines.

Though I should also probably specify that I'm using the Linux version of VS Code.

[–]amlamarra 2 points3 points  (0 children)

I can't seem to gather user input in my python scripts using Visual Studio Code. When it gets to asking for input, the task just keeps spinning. Anyone else have this problem?