Running Python Code From an IDE or Text Editor
Learn how to run scripts from VS Code and PyCharm.
00:00 Next, running code from an IDE or text editor. Visual Studio Code—Windows. In Windows Explorer, you can navigate to your file, and if you’ve got VS Code installed, right-clicking and then Open with will allow you to open it in VS Code.
00:17 You can go to Terminal and then Run Active File, and you should see the program running in the window.
00:30 Visual Studio Code on Mac. In Finder, you can navigate to your Python script, and once you get there, you can double-tap or right-click to open up the context menu and go to Open With and then pick Visual Studio Code.
00:50 Then you can go to Terminal and Run Active File, and you can see there’s a problem. An error has been generated with unexpected tokens, and this is because VS Code in this case needs to be told how to interpret the script. That’s done with the line being typed onscreen now, which you may remember from the Ubuntu example earlier in the series.
01:17
With that in place, go to Terminal, and Run Active File allows it to run as intended and you can see Hello World!
. PyCharm—and while you’ll see this on a Mac, it’s the same in Windows.
01:32 Once you’ve found the file, you can right-click or double-tap and go to Open With > PyCharm. And once PyCharm launches and opens up your file, it’s as easy as double-tapping or right-clicking, and then picking Run.
01:55 A terminal window appears at the bottom, and you can see the effect of the script running there.
Johnny on May 11, 2019
Hi Warren!
Your laptop is fine. Sounds like you’re using Linux or MacOS. In order to make a script executable, you have to set the execute permission on the file itself as well as tell your shell where to find the python interpreter that can execute your script (#!/usr/bin/env python3). These two pieces will allow you to execute your script right away. Darren covered setting the execute bit in the prior video (at the 2:33 mark for linux). For macOS, Darren demonstrates how to run it from IDLE but you would follow the same directions (setting the execute bit on the script and adding the first line aka the shebang line) to get it to run.
Following the above, you’ll be able to run it in VS Code be selecting Terminal->Run Active File. If you would like to run it more automatically and without the shebang line, you can set the python interpreter that you want to use for your project by selecting View->Command Palette and select Python: Select Interpreter. Pick the version of python you want you use. The bottom left corner will display that version of python. Once you set the version of python to use for your script, you can just go into the code portion of the screen, right click and select Run Python File in Terminal. This will open an Integrated Terminal and run the script. In this case, you are not making the script executable but rather running the version of python with the script as the argument to the python interpreter.
Hope this helps, Johnny
Become a Member to join the conversation.
Warren Ross on April 9, 2019
Excellent tutorial. I would like to understand why VS Code needs a line at the top to explain how to run python3. Is there a setting that can make this more automatic? Also, for some reason there is a permissions issue also from my laptop. I figured out how to fix it by changing the file permissions but it would be nice to know if where to change setting in VS Code so I don’t have to change permissions on each file.