# CS111 guide to working with Python in Ed Workspaces
## Quick reference
* All files must end in `.py`. Filenames have no spaces, no special characters.
* Testing files must start with `test_` and have the two lines loading in pytest and the code file functions at the top.
* To run and interact with a code file, type `python -i [filename]` in the terminal. You must rerun this command every time you want to run/interact with the file after making changes to it.
* To run a testing file, type `pytest [test filename]` in the terminal
* The above two commands will only work in the terminal, not in the Python interpreter. You're in the interpreter if all of your lines start with `>>>`. To exit the interpreter, type `exit()` or press ctrl + d.
## Getting started
Unlike in Pyret, a Python project might consist of many files. In CS111, we'll typically be working with 2 files: a code file and a test file.
Get started by making a new Python workspace in Ed Workspaces, or by forking an existing workspace we give you (as you did in the [software setup](https://hackmd.io/@cs111/software-guide))
### Make a new workspace (start from scratch):

### Fork a workspace that we've given to you
Open it and then press the "fork" button:

If you want to rename a workspace that you've forked, you can click on the name at the top of the screen.

### Making files
Once you have a workspace open, press the new file button to make a file.

**Please follow these guidelines:**
* Do not remove the `/home/` that Ed Workspaces includes by default in the box that pops up. For example, if you want your file to be called `hello.py`, the textbox should read `/home/hello.py`
* Filenames should have no spaces or special characters other than `_` and should end in `.py`
* Filenames should not be the same as a Python library we are using (for example, when working with pandas, do **not** name your file `pandas.py`)
* **Testing** files should start with `test_`, i.e. `test_hello.py`
## Running and interacting with Python files
Ed Workspaces doesn't have a "Run" button like CPO did. In order to run a file, open a **terminal**. A terminal is a computer tool that allows you to type commands that the computer will understand and run. In CS111, we will just be telling the computer to run Python programs. The terminal will open at the bottom of the screen.

In the terminal, type `python -i [filename]`, for example, `python -i hello.py`. The `-i` tells Python that we want to interact with the file after running it. Press enter/return after typing the command.
The `>>>` indicates that we have entered the Python *interpreter* (the interpreter can run inside of the terminal, not the other way around). It acts similarly to the Pyret interactions window -- we can run expressions and see their result.

:::warning
### Re-running a file after making modifications
After making modifications to a file, you will need to exit the interpreter and rerun the Python file.
**If the line you're typing into in your terminal starts with `>>>`, you are in the interpreter and won't be able to run the `python -i [filename]` command.**
To exit the interpreter, either type ***`exit()`*** and press enter/return, or press ***ctrl + d*** (on both Mac and PC).

:::
## Writing and running tests
Tests should be in a separate file that starts with `test_`, e.g. `test_hello.py`. **At the top of each testing file, put the following two lines:** (replace `[filename]` with your code file name without the .py, e.g. `from hello import *` if your code file is called `hello.py`)
```
import pytest
from filename import *
```
This tells Python to load in the testing capabilities and to find all of the functions in your code file.
### Writing a test function
A test function is a function with no inputs/outputs that starts with `test_`. One guideline you can use is to write a separate test function for every `where` block you would have written in Pyret. *Tests* are just Boolean expressions preceded by `assert`. The test will pass if and only if the expression evaluates to true.
```
def test_my_function():
assert my_function(4) == 8
# more examples can go here
```
### Running tests
To run the tests, type `pytest [testing file]`, e.g. `pytest test_hello.py`, in the terminal. If the tests pass, you will see a message similar to the one below:

If some test is failing, you will see an indication of which test is failing:

In this case, the failing test is on line 15 of `test_intro.py` (the pytest output says `test_intro.py:15: AssertionError`).
## Using the debugger
See [lab 9](https://hackmd.io/@cs111/lab9-f25#Part-1-Debugging) on information on how to use the debugger!
## Recommended settings
Press the gear icon to configure your settings

We recommend **turning on** the following settings (the box should be checked). The ones in bold are **important**:
* **Line numbers**
* Highlight active line
* Detect indentation
* **Soft tabs**
* **Smart indent**
* **Overflow ruler**
* Indent guides
We recommend **turning off** the following setting (the box should be unchecked):
* Autocomplete (because it often suggests unhelpful changes)
The others are up to personal preference or unimportant.