Join us and get access to hundreds of tutorials and a community of expert Pythonistas.
Unlock This Lesson
This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.
Running Python Scripts
Learn how to run scripts from VS Code and PyCharm.
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.
Johnny on May 11, 2019
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,
Become a Member to join the conversation.