Tutorial
- Visual Studio Code And Python Free
- Visual Studio Code And Python Interview
- Visual Studio Code And Python Django
- Visual Studio Code And Python Code
- Visual Studio Code Python Linter
From the Debug view, select the Configuration drop-down, then Add Configuration, and select Python: Visual Studio Code will create a debug configuration file under the current folder called.vscode/launch.json, which allows you to setup specific Python configurations as well as settings for debugging specific apps, like Django and Flask.
- Python in Visual Studio Code Working with Python in Visual Studio Code, using the Microsoft Python extension, is simple, fun, and productive. The extension makes VS Code an excellent Python editor, and works on any operating system with a variety of Python interpreters.
- Python debug configurations in Visual Studio Code The Python extension supports debugging of a number of types of Python applications. For a short walkthrough of basic debugging, see Tutorial - Configure and run the debugger. Also see the Flask tutorial.
- Apr 09, 2020 Python is an incredibly popular language with strong support in Visual Studio Code. By installing the Python extension, you’ll get Python intellisense, auto-completion, and other useful miscellaneous shortcuts.
- Visual Studio supports Python version 2.7, as well as version 3.5 and greater. While it is possible to use Visual Studio to edit code written in other versions of Python, those versions are not officially supported and features such as IntelliSense and debugging might not work.
Introduction
Python is one of the most popular and easy to learn languages, which is why it is often one of the first languages you learn. Let’s see how to work with and run Python inside of Visual Studio Code.
In this tutorial you’ll install the Python extension then use intellisense and shortcuts to run your Python code.
Prerequisites
- Python installed on your machine and a local development environment set up. You can complete both of these with our tutorial How To Install and Set Up a Local Programming Environment for Python 3.
- Visual Studio Code installed on your machine by visiting the official download page.
Step 1 — Running Python From the Built-in Terminal
With Python installed and your local programming environment set up, open Visual Studio Code.
Inside of Visual Studio Code, open the directory you’re working in by going to File -> Open and selecting the directory. After that, you’ll see your folder open in the explorer window on the left.
With the directory open, you can create your first Python file (.py
extension) with some code to print 'Hello World'
.
Save the file.
Now that you have your Hello World
code ready, we can run it by using the built-in terminal in Visual Studio Code. If if is not open already, you can open it by going to View -> Terminal or use the shortcut, CTRL + ~
.
The terminal that you just opened will automatically start in the current directory that you are editing in Visual Studio Code. This is exactly why we created and opened a directory before getting started. We can prove this by running the following command:
This command will print the path to the current directory. From there, you can verify that your Python file is also inside of the current directory by running the following command to print a list of files in the directory:
Now, you can run your Python file with the following command:
After running, you’ll see Hello World
printed out in the console.
Step 2 — Installing the Python Extension
We can streamline the process of working with Python in Visual Studio by installing the Python extension created by Microsoft. To install the extension, open up the extension menu on the left (the icon looks like a square inside of a square) and search Python.
It will be the first one that pops up, and you can click on it to view the extension details and click Install.
After installing, you might need to reload, so go ahead and do that.
After you restart, you can now take advantage of the Python extension’s features:
- IntelliSense
- Auto-completion
- Shortcuts for running Python Files
- Additional info on hovering Python variables, functions, and so on.
To start working with IntelliSense, create an empty array called list
.
Then following type list.
followed by a period and notice that some information pops up. The extension is providing you all the functions and properties of a list that you can use.
If you want to use one of those functions, you can press ENTER
or TAB
to auto-complete that function name. This means that don’t have to memorize every function in Python because the extension will give you hints as to what is available. Notice also that it shows you a brief description of what the function does and what parameters it takes.
You can also get intellisense when importing modules in Python. Notice if you type random
, intellisense pops up to complete the name of the module as well as providing some background info on what it does.
If you then start to use the random
module, you’ll continue to get intellisense for functions that you can access with that module.
Visual Studio Code And Python Free
Lastly, you can hover on existing variables, module imports, and so on, for additional information whenever you need it.
Step 3 — Using Shortcuts to Run Python Code
If you want to do more in your Python file, here’s a short snippet for the Bubble Sort algorithm. It calls the bubble_sort
function and prints out the result. You can copy this code into your file:
With this new piece of code, let’s explore a new way to run our Python file. The typical first workflow for working with Python files is to save your file and then run that Python file in the terminal. With the Python extension, there are a few shortcuts to help with this process.
Inside of any Python file, you can right click in the editor and choose Run Python File In Terminal. This command will do each of the individual steps that we talked about before.
Visual Studio Code And Python Interview
After using the shortcut, you can see the bubble_sort
output in your console.
You also have a shortcut to open the Python REPL where you can quickly type Python code directly into your console and see the output. Open the command palette using the shortcut CMD
+ SHIFT
+ P
on Mac or CTRL
+ SHIFT
+ P
on Windows and select Python Start REPL.
After typing in a print
command, you will see Hello World
immediately displayed in the console.
Conclusion
Python is an incredibly popular language with strong support in Visual Studio Code. By installing the Python extension, you’ll get Python intellisense, auto-completion, and other useful miscellaneous shortcuts.
-->By default, installing the Python development workload in Visual Studio 2017 and later also installs Python 3 (64-bit). You can optionally choose to install 32-bit and 64-bit versions of Python 2 and Python 3, along with Miniconda (Visual Studio 2019) or Anaconda 2/Anaconda 3 (Visual Studio 2017), as described in Installation.
Alternately, you can install standard python interpreters from the Add Environment dialog. Select the Add Environment command in the Python Environments window or the Python toolbar, select the Python installation tab, indicate which interpreters to install, and select Install.
You can also manually install any of the interpreters listed in the table below outside of the Visual Studio installer. For example, if you installed Anaconda 3 before installing Visual Studio, you don't need to install it again through the Visual Studio installer. You can also install an interpreter manually if, for example, a newer version of available that doesn't yet appear in the Visual Studio installer.
Note
Visual Studio supports Python version 2.7, as well as version 3.5 to 3.7. While it is possible to use Visual Studio to edit code written in other versions of Python, those versions are not officially supported and features such as IntelliSense and debugging might not work.
For Visual Studio 2015 and earlier, you must manually install one of the interpreters.
Visual Studio (all versions) automatically detects each installed Python interpreter and its environment by checking the registry according to PEP 514 - Python registration in the Windows registry. Python installations are typically found under HKEY_LOCAL_MACHINESOFTWAREPython (32-bit) and HKEY_LOCAL_MACHINESOFTWAREWOW6432NodePython (64-bit), then within nodes for the distribution such as PythonCore (CPython) and ContinuumAnalytics (Anaconda).
If Visual Studio does not detect an installed environment, see Manually identify an existing environment.
Visual Studio shows all known environments in the Python Environments window, and automatically detects updates to existing interpreters.
Interpreter | Description |
---|---|
CPython | The 'native' and most commonly-used interpreter, available in 32-bit and 64-bit versions (32-bit recommended). Includes the latest language features, maximum Python package compatibility, full debugging support, and interop with IPython. See also: Should I use Python 2 or Python 3?. Note that Visual Studio 2015 and earlier do not support Python 3.6+ and can give errors like Unsupported python version 3.6. Use Python 3.5 or earlier instead. |
IronPython | A .NET implementation of Python, available in 32-bit and 64-bit versions, providing C#/F#/Visual Basic interop, access to .NET APIs, standard Python debugging (but not C++ mixed-mode debugging), and mixed IronPython/C# debugging. IronPython, however, does not support virtual environments. |
Anaconda | An open data science platform powered by Python, and includes the latest version of CPython and most of the difficult-to-install packages. We recommend it if you can't otherwise decide. |
PyPy | A high-performance tracing JIT implementation of Python that's good for long-running programs and situations where you identify performance issues but cannot find other resolutions. Works with Visual Studio but with limited support for advanced debugging features. |
Jython | An implementation of Python on the Java Virtual Machine (JVM). Similar to IronPython, code running in Jython can interact with Java classes and libraries, but may not be able to use many libraries intended for CPython. Works with Visual Studio but with limited support for advanced debugging features. |
Developers that want to provide new forms of detection for Python environments, see PTVS Environment Detection (github.com).
Visual Studio Code And Python Django
Visual Studio Code And Python Code
Move an interpreter
Visual Studio Code Python Linter
If you move an existing interpreter to a new location using the file system, Visual Studio doesn't automatically detect the change.
If you originally specified the location of the interpreter through the Python Environments window, then edit its environment using the Configure tab in that window to identify the new location. See Manually identify an existing environment.
If you installed the interpreter using an installer program, then use the following steps to reinstall the interpreter in the new location:
- Restore the Python interpreter to its original location.
- Uninstall the interpreter using its installer, which clears the registry entries.
- Reinstall the interpreter at the desired location.
- Restart Visual Studio, which should auto-detect the new location in place of the old location.
Following this process ensures that the registry entries that identify the interpreter's location, which Visual Studio uses, are properly updated. Using an installer also handles any other side effects that may exist.