To run your python code press RUN a symbol at the top right corner of your editor or right-click and choose run options. Run Python Snippet in VSCode If you choose “ Run selection/Line in Python terminal “, Vscode runs only that part in a terminal.
- After clicking to run the Python file in the terminal, Visual Studio Code will automatically write the path of the Python application and the Python file, again just like we did manually in the previous article. Now, everything is just happening inside the same application for our convenience!
- In Visual Studio, select File New Project (Ctrl + Shift + N), which brings up the New Project dialog. Here you browse templates across different languages, then select one for your project and specify where Visual Studio places files. To view Python templates, select Installed Python on.
Tutorial
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.
Visual Studio Code Python Tutorial
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.
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.
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.
Is there any keyboard shortcut to run code from Visual Studio Code?
Is there any keyboard shortcut to run code from Visual Studio Code? |
Jun-04-2018, 07:33 PM (This post was last modified: Jun-04-2018, 07:46 PM by python300.) I know you can right click in the editor and select Run Python File in Terminal. Or I can right click the name of the python file from left panel and select Run Python File in Terminal. But what i am trying to find is if there is a keyboard shortcut to run the code? My bad. I was missing code runner extension and so was unable to see the Run triangle symbol at the top of code editor. Once I installed Code Runner, I can see the Run Code symbol (triangle) and om highlighting it, I see the shortcut Ctrl + Alt + N Jun-04-2018, 09:39 PM (This post was last modified: Jun-04-2018, 09:39 PM by python300.) I have installed Visual Studio Code and added extensions - Python, Code Runner. With Code Runner, now I can see the Run Code symbol (triangle) and on highlighting it, I see the shortcut Ctrl + Alt + N But when I try to use it to run the code that asks for user input, I can't find a way to provide the input. When I try to enter user input, I get error message 'Cannot edit in read-only editor' I think this is because I am missing some configuration part for Code Runner like setting up PATH or some other Workspace settings. Please assist me in identifying what all configuration will I need to do and how? [UPDATE]: I did select 'Add Python 3.6 to PATH' while installing Python. I have attached screenshots for reference. Note: Even now when I right click and select 'Run Python File in Terminal' for the same program, I can enter user input fine and get the expected output. Jun-04-2018, 11:35 PM Install Code Runner,get a button and shortcut Ctrl+Alt+N. Can also build own task that run Python. Aug-07-2018, 09:12 PM Finally I got answer to my second question (which I wanted to post as a new question) from SO. Answer: You can provide input by telling code runner to use the terminal. To do this, there is a setting called code-runner.runInTerminal, set to false by default, that you can set to true. In the lower left hand corner of the screen on 'VS code' you'll see the icon which will take you to settings when clicked. Thank you to the helper who answered it there. I wanted to post that answer here so if someone like me comes here searching for the same thing, he/she will find help here. |
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
Autocompletion for Python in Visual Studio | user3481 | 1 | 418 | Dec-10-2020, 12:03 PM Last Post: snippsat | |
Installing the Visual Studio Code on Linux - how to do that!? | apollo | 4 | 1,298 | Oct-05-2019, 09:48 PM Last Post: apollo |