Try   HackMD

Working with VSCode for CS111

This guide assumes that you have worked through lab 0 to set up VSCode.

Organizing your Files

Create a folder on your computer for your CS111 Python work. You can create this within your file explorer, you don't need to do it within VSCode.

We recommend that you make a subfolder for each lecture/assignment within the overall folder. You can create those either within VSCode or just within the folder explorer system on your computer.

Interacting with Your Code

In Pyret, there was one way to interact with code: run it the type expressions in the interactions window.

In Python, you can run code with or without interacting with it (each involves separate steps).

Running your file to completion: You can do this by right-clicking on your code and clicking "Run Python File in Terminal":

You can also use the drop down (down-pointing arrow towards the top right corner, with options for "Run " or "Debug ") and select "Run Python File."

Using either option, VSCode will run the file to completion. For this to be effective, there have to be expressions within your file that actually run your functions or tests and print the results. Python does not automatically show the results of expressions the way Pyret did.

VSCode is picky about switching between the way above (running your file to completion) and the way below (interacting with your file). If your terminal shows >>>, you should exit the terminal (trash button) before running the file to completion, or you'll get invalid syntax errors:

exit_terminal.png

Interacting with your file: If you want to interact with your file as you did with the Pyret interactions window, highlight all of your code (you can do this using CTRL + a on Windows or Command-a on Mac), right click, and select "Run selection/line in Python terminal". Newer versions of VSCode have replaced this with "Run selection in Python REPL". It will look a little different, but it's fine to use this option. If the REPL is giving you issues, please see the pinned Ed post on "Common Python/Pytest issues"

The first time you open a file in VSCode, or if there is no terminal/console open, you might have to do this step twice.

This will evaluate your file line-by-line, and allow you to interact with the code by typing and running expressions.

If you edit your file, you will have to repeat all of the steps above (for either option running the file to completion or interacting with the file).

Testing your code

In Python, testing (writing examples) is done in a separate file from your code. To test a Python file, follow these steps:

  1. Create a new .py file in the same folder as the file you are testing. Make sure it starts with test_ (e.g. test_python_intro.py). Also make sure that there are no files with the same name open in any folders or sub-folders in the VSCode screen. We will call this the "testing file"
  2. At the top of your testing file, write import pytest.
  3. Tell the testing file to pull in the code from your original file by writing from [original file] import *. For example, if the original file is called python_intro.py, write from python_intro import *.
  4. Refer to lab 0 for instructions on configuring and running the tests (starting with the step that reads "Ensure you have a Python file open in VSCode. Next, navigate to the testing panel by clicking on the beaker (in the left sidebar)")