Python 3 Code Runner

All examples use Python 3.5 or later (unless noted) and assume you're running Linux or a unix-based OS.

All examples can be found on this Jupyter notebook

  • I have Python 3.8 selected. But code chooses version 3.7.2. I have rebooted. It still chooses 3.7.2. So whatever solution does this it is not simply using the command pallet. I can find the right version by copying what Code says and running it on the command line. But Code, does not run that command that it says it is supposed to.
  • Python is an interpreted language, and in order to run Python code and get Python IntelliSense, you must tell VS Code which interpreter to use. From within VS Code, select a Python 3 interpreter by opening the Command Palette ( ⇧⌘P (Windows, Linux Ctrl+Shift+P ) ), start typing the Python: Select Interpreter command to search, then select.
  • Write and run Python code using our online compiler (interpreter). You can use Python Shell like IDLE, and take inputs from the user in our Python compiler.
  • The Python extension then provides shortcuts to run Python code in the currently selected interpreter (Python: Select Interpreter in the Command Palette): In the text editor: right-click anywhere in the editor and select Run Python File in Terminal. If invoked on a selection, only that selection is run.

This tool allows you to run any Python demo code online and helps you to test any python code from your browser without any configuration. This tool provides you any Python version from Python 2.7, Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 3.6, Python 3.7, Python 3.8 and runs your Python code in our sandbox environment.

call() example

Use run() instead on Python v3.5+

With suprocess.call() you pass an array of commands and parameters.

subprocess.call() returns the return code of the called process.

subprocess.call() does not raise an exception if the underlying process errors!

call() example using shell=True

Use run() instead on Python v3.5+

If shell=True, the command string is interpreted as a raw shell command.

Using shell=True may expose you to code injection if you use user input to build the command string.

call() example, capture stdout and stderr

If you are on Python 3.5+, use subprocess.run() instead as it's safer.

call() example, force exception if process causes error

Use subprocess.check_call()

Run command and capture output

Using universal_newlines=True converts the output to a string instead of a byte array.

  • Python version 2.7 -> 3.4

  • Python version 3.5+

Run raw string as a shell command line

Don't do this if your string uses user input, as they may inject arbitrary code!

This is similar to the example above, with shell=True

Run python script in python
  • Python version 2.7 -> 3.4

  • Python version 3.5+

run() example: run command and get return code

run() behaves mostly the same way as call() and you should use it instead of call() for version 3.5 onwards.

subprocess.run() does not raise an exception if the underlying process errors!

run() example: run command, force exception if underlying process errors

Use check=True to force the Python method to throw an exception if the underlying process encounters errors:

run() example: using shell=True

As in the call() example, shell=True, the command string is interpreted as a raw shell command.

Again, Using shell=True may expose you to code injection if you use user input to build the command string.

run() example: store output and error message in string

If the underlying process returns a nonzero exit code, you will not get an exception; the error message can be accessed via the stderr attribute in the CompletedProcess object.

  • case 1: process return 0 exit code

  • case 2: process returns nonzero exit code

  • case 3: other OS-level errors

    this case will throw an exception no matter what. For example, if you call an executable that doesn't exist. This throws an exception because it wasn't that the subprocess had an error - it never got created in the first place.

Popen example: run command and get return code

subprocess.Popen() is used for more complex examples where you need. See Popen() vs call() vs run()

This causes the python program to block until the subprocess returns.

Popen example: Store the output and error messages in a string

Popen example: Redirect output to file

Popen example: Redirect output and errors to the same file

Popen example: Run command in the background

By default, calls to Popen() spawn a subprocess in the background and don't wait for it to terminate (unless you use wait() on the Popen object).

Pipe commands together

Use Popen:

Wait for command to terminate, asynchronously

Use asyncio and await.

Method asyncio.create_subprocess_exec() works much the same way as Popen() but calling wait() and communicate() on the returned objects does not block the processor, so the Python interpreter can be used in other things while the external subprocess doesn't return.

Python 3.6+ is needed here

call() vs run()

As of Python version 3.5,run() should be used instead of call().

Python 3 Code Runner
  • run() returns a CompletedProcess object instead of the process return code.

    • A CompletedProcess object has attributes like args, returncode, etc. subprocess.CompletedProcess
  • other functions like check_call() and check_output() can all be replaced with run().

Popen vs run() and call()

call() and run() are convenience functions and should be used for simpler cases.

Popen() is much more powerful and handles all cases, not just simple ones.

Felipepython3

Related content

Python 3 Code Runner 2

Please enable JavaScript to view the comments powered by Disqus.