📘 Tutorial: Running Python Scripts in VS Code¶
Follow these simple steps to download, open, and interactively run Python scripts (.py files) using Visual Studio Code (VS Code).
What is a Python Script?¶
A Python script is a simple text file ending in .py containing lines of Python code. Unlike notebooks that run inside divided blocks or web browsers, a Python script allows you to write, edit, and run code line-by-line directly in VS Code's editor, sending results to an interactive terminal (REPL—Read-Eval-Print Loop) at the bottom of your screen.
1. Open Your Course Workspace in VS Code¶
- Launch Visual Studio Code.
- Open your course project folder:
- Click File → Open Folder... from the top menu.
- Select your course workspace folder (e.g.,
data-analysis). - Click Open. (If VS Code asks "Do you trust the authors of the files in this folder?", click Yes, I trust the authors to avoid Restricted Mode).
- You will see your project files listed in the Explorer sidebar on the left.
2. Verify Your Python Environment¶
Make sure VS Code is using the .venv-da environment set up during onboarding:
(Note: The setup script automatically configures workspace settings and registers the kernel, so VS Code automatically selects .venv-da whenever you open your course folder).
- Open the Command Palette:
- Press F1 (or click the search bar at the very top of VS Code).
- Alternatively:
- On macOS: Press Cmd+Shift+P
- On Windows: Press Ctrl+Shift+P
- In the top search bar, type
Python Select Interpreterand click on Python: Select Interpreter. - In the dropdown list, click on the environment labeled
.venv-da. - Success Check: Look at the bottom-right status bar of VS Code. You should see
Python 3.12... ('.venv-da': venv)displayed.
3. Download a Course Python Script¶
- Open the course materials repository on GitHub:
- Click on the script you wish to open (for example,
01_getting_started.pyor02_data_types_and_structures.py). - Click the Download raw file button (the downward arrow icon at the top right of the code view).
- Save or move the downloaded
.pyfile into your course workspace folder.
4. Open and Run Code Interactively (Smart Send)¶
- In the VS Code Explorer sidebar on the left, click on the script file (e.g.,
01_getting_started.py) to open it in the editor. - Place your cursor on the first line of code you want to run (or highlight multiple lines).
- Execute the code using Smart Send: press Shift+Enter.
- Success Check:
- An Interactive Python Terminal will automatically open at the bottom of your screen.
- Your code line will appear in the terminal, followed immediately by the evaluated result or output.
- The cursor in your editor will automatically advance to the next runnable line.
5. Working with Your Data and Variables¶
- Variables stay in memory: Once you run a line that defines a variable (like
df = pd.read_csv(...)), that variable remains loaded in your Python session. You can inspect it anytime by typing its name in the terminal below or selecting it and pressing Shift+Enter. - Saving your work: Save your modifications anytime using:
- On macOS: Press Cmd+S
- On Windows: Press Ctrl+S
- Restarting fresh: If your code encounters an error and you want to start clean, click the trash can icon at the top-right of the terminal panel to close the terminal, then press Shift+Enter again on line 1 to launch a fresh session.
6. Official Resources & References¶
For documentation, syntax guides, and cheat sheets for the tools used in this course:
- Course Coding Resources: Full listing of official sites, tutorials, and cheat sheets.
- Visual Studio Code Official Website: Official VS Code homepage and documentation.
- Python Official Documentation: Python standard library and language tutorial.
- pandas Documentation: Tabular data structures and manipulation guide.
- Altair Documentation: Declarative statistical charting gallery and examples.
- statsmodels Documentation: Statistical modeling and linear regression guides.