# Lab 0: VSCode and Python Setup Guide For the first part of the course, we program in Pyret. The programming environment (where you write your programs) is online at [https://code.pyret.org](https://code.pyret.org). You log into it with any Google account. Starting in early April, we will switch to programming in Python. We want to get our Python programming tool (VSCode) installed now, before you're under the deadline pressure of a Python assignment. Unfortunately, getting a Python programming environment installed and set up can be a bit tricky. This is because there are multiple versions of Python and many extensions that we'll need to use with Python: we have to make sure that all the extensions and your code are using a consistent version of Python. **Please don't judge your potential experience in the course by this setup process -- it isn't at all representative (even the prof and TAs often find this painful)**. We've just found in past years that it is better to get the tools ready now before assignments get underway. The TAs will be running several TA hours during the first week of shopping period where you can come in and have them help you through the installation process. Of course, you are welcome to do this on your own as well (and put questions on EdStem). The instructions on this page give everything you'll need. Hopefully, this entire process should take no more than 30 minutes – if you find it taking substantially longer, please come get help! ## Why are we using VSCode? Can we use something else? Despite the popularity of Python, it is very hard to find a good tool in which to teach it, especially in a class that covers testing and explains the underlying information used to run programs. You are welcome to use another Python tool if you'd like, but you'll have to: * use Python version 3.11 (check by running `python3 --version` from terminal/MacOS or command prompt/Windows) * install the packages that we need for yourself (numpy, pytest, matplotlib, and pandas), * make sure you can load csv files that you have downloaded to your computer * adapt lecture presentations to your tool instead of VSCode. **Labs will run in VSCode. The TAs will only help with VSCode installation and configuration.** ## Setting Up Everything At a high-level, there are four steps to the setup: * Install VSCode (the tool where we'll write code) * Install the Miniconda Python manager (this will also install Python) * Configure Miniconda with the Python extensions we'll need * Check that everything is working The rest of this handout walks you through each step in detail. ## Step 1: Install VSCode Follow the instructions on [the official VSCode website](https://code.visualstudio.com/docs/setup/setup-overview) to set it up on your computer. ## Step 2: Install Miniconda/Python Follow the instructions for whichever kind of computer you have **([Windows](#Windows) vs [Mac](#Mac)).** ### Windows Go to [this page](https://docs.conda.io/en/latest/miniconda.html) to install Miniconda. If you don’t know if your computer is 64-bit or 32-bit, it is very likely 64-bit. ![image](https://hackmd.io/_uploads/rkDUiznK6.png) Open the installer and install Miniconda locally (“just for me”). You can use the default settings and just press “next” until you finish the installation. After you’ve run the installer, look for the Miniconda installation on your start menu and click to open it (it may appear as “Anaconda Prompt (Miniconda3)” as shown below) ![image](https://hackmd.io/_uploads/ByDdjzhYp.png) In the command prompt that opens up, type `conda init` and press enter: ![image](https://hackmd.io/_uploads/Bkv5jfnYT.png) Once this is done running, close the window and open a new terminal. To do this, search for ‘Command Prompt’ on Windows in the start menu. Then, type `conda`, and press enter. If informative text appears, similar to the below, then you are set! Go on to [download the VSCode Python extension](#Step-3a-Download-the-VSCode-Python-Extension). ![image](https://hackmd.io/_uploads/rkpZ2GhYa.png) ### Mac Go to [this page](https://docs.conda.io/en/latest/miniconda.html) to install Miniconda. * If you are on an Mac with an M1 or M2 processor download ‘Miniconda3 macOS Apple M1 ARM 64-bit pkg * If you are on an Mac with an Intel processor download ‘Miniconda3 macOS Intel x86 64-bit pkg’ _(you can check what kind of processor you have in ‘About This Mac’)_ ![image](https://hackmd.io/_uploads/rkuHnznYp.png) Once the installer has downloaded, open it and follow the instructions in the installer. **If prompted to select a destination, choose ‘Install for me only’.** If no prompt appears, just continue on. ![image](https://hackmd.io/_uploads/H1xu3GhFa.png) ### For Both Mac and Windows Once you’ve completed all of the steps you should get a success message/window (example below). To be sure you’re all set, you can open ‘Terminal’ (in Launchpad) on MacOS or ‘Command Prompt’ on Windows in the start menu, and type in `conda`. If you get an informative message like the one below, you’re all set! <span style="text-decoration:underline;">Note</span>: It is possible that your installation was a success, but it does not show. For example, if you opened your terminal before installing conda, you may need to open a new terminal. ![image](https://hackmd.io/_uploads/r1rgpGnKT.png) ## Step 3a: Download the VSCode Python Extension **For both Mac/Windows:** Open VSCode (as you would any other application on your laptop). You do not need to open any files/folders at this time. 1. Click on the “Extensions” tab on VSCode (located in the left sidebar, looks like a four-square grid with a piece popping out). 2. Search “Python” in the ‘Search Extensions in MarketPlace’. You’ll see three options popping up at the top. 3. Select the one that includes the word “Pylance” in the description, and install it (the first option in the following screenshot). ![image](https://hackmd.io/_uploads/S1VB6f2KT.png) ## Step 3b: Set up Conda in VSCode In VSCode, open a new terminal (Terminal → New Terminal) and run the following commands **_<span style="text-decoration:underline;">one-by-one</span>_**. If you get an error after any of the commands, resolve them (by posting on Ed or asking a TA) before moving on to the next command. <span style="text-decoration:underline;">Also note that you may be prompted to choose yes/no to Proceed after running some of these commands. Choose yes (just press the y key and then enter/return).</span> <details> <summary>If you are using Windows, click here!</summary> In terminal, make sure that you are running this in the command prompt (“cmd”) and not Powershell, otherwise conda will not be recognized and these commands will not run. To do this press the drop down menu next to the “+” and click on “Command Prompt” (see below). If correct, “cmd” should be seen in the bar on the top right of the terminal. ![image](https://hackmd.io/_uploads/r1nrAGnta.png) </details> <br> ``` conda config --append channels conda-forge conda update -n base -c defaults -c conda-forge conda conda create -n cs111-env python=3.11 conda activate cs111-env conda install -c conda-forge pytest numpy pandas matplotlib ``` This creates a new environment called cs111-env that uses Python version 3.11 and installs all the necessary Python packages. **Troubleshooting:** If you get a CondaSSLError, find the folder on your computer where Miniconda is installed (on Windows, this is usually C:/Users/_[your username]_/Miniconda3), go to the Library folder and then the bin folder, and copy the files libcrypto-1_1-x64.dll and libssl-1_1-x64.dll. Go back to the Miniconda3 folder, go to the DLLs folder, and paste the files: Copy from here: ![image](https://hackmd.io/_uploads/Sk_aNEnF6.png) To here: ![image](https://hackmd.io/_uploads/HJn0NE3t6.png) ## Step 4: Download and open the file to test the setup Create a new folder on your computer, and remember what you named it and where you created it. Download [this file](https://drive.google.com/file/d/1_8P9lqmEy5iLxWCqHa1_-i4zgBl1X-bZ/view?usp=sharing) and save it to that folder. In VSCode, open the folder that you created (File → Open Folder, and then browse to the folder), and then double-click the test_cs111_setup.py file in the left sidebar to open it in the VSCode window. **Make sure to use “Open Folder” and not “Open File”** Check the text at the bottom right corner of the VSCode window. If it does not say **‘cs111-env’: conda** click on the text (the version number) to the right of the word Python and select cs111-env in the Select Interpreter bar (your version will say 3.11 instead of 3.9) **NOTE:** If the correct interpreter doesn't appear, quit Visual Studio Code completely (force quit) and restart it. This will refresh the list of interpreters that appear as below. **WINDOWS:** ![image](https://hackmd.io/_uploads/H1UqrEnta.png) ![image](https://hackmd.io/_uploads/HJl3rEnt6.png) **MAC:** ![image](https://hackmd.io/_uploads/S1u6rV3Y6.png) ![image](https://hackmd.io/_uploads/SJv0BE3YT.png) **Every time that you are working on a CS111 (Python) assignment, make sure the text at the bottom-right corner of the VSCode window says (‘cs111-env’: conda).** ## Step 5: Test your setup The command line/terminal should have `(cs111-env)` before your username, indicating that you are in our cs111 conda virtual environment. Navigate to the testing panel by clicking on the beaker (in the left sidebar): ![image](https://hackmd.io/_uploads/SkoNUEhYT.png) If this is your first time using pytest (likely in 111), you may have to select the correct configuration: **PLEASE click ‘pytest’ and NOT ‘unittest’** ![image](https://hackmd.io/_uploads/SJD8LEnKp.png) **PLEASE click ‘. Root directory.’** ![image](https://hackmd.io/_uploads/S1MtIN2Ya.png) **Troubleshooting:** If pytest is having problems finding your tests, make sure the only file you see in the left panel is test_cs111_setup.py. Also, double-check that you are using the cs111-env environment. If there is a folder called \__pycache\__ anywhere in your workspace (the VSCode term for the collection of directories that you currently have open), you can also try deleting \__pycache\__ to see if it helps (don’t worry, you don’t need it). Once VSCode has found the tests, run them by clicking the “run tests” arrow to the right of test_setup.py _(note: you will not see test_copy or test_nltk on your screen)_ ![image](https://hackmd.io/_uploads/Syp4wN3tp.png) You should see a plot pop up that looks like this: ![image](https://hackmd.io/_uploads/HkzDDVhFa.png) The tests will not finish until you close the plot window. Close the plot now. If the tests all pass (all have green checkmarks), you are good to go! ![image](https://hackmd.io/_uploads/BJV9wNnYp.png) ## Step 6: Update your VSCode settings **VSCode does not automatically save your files unless you tell it to.** To change this, go to File -> Preferences -> Settings on Windows (you can also use Ctrl+,) or Code -> Preferences -> Settings on Mac (alternatively, it might be Code > Settings... > Settings). Auto Save should be the first setting there (or you can use the search bar to search for the Auto Save preference). To have it auto-save, set it to afterDelay: ![image](https://hackmd.io/_uploads/B1JaDE2YT.png) To get a vertical line at 80 characters, in the same settings window, search for "ruler." Click "edit in settings.json" under Editor:Rulers: ![image](https://hackmd.io/_uploads/SkSRD4ntT.png) And change or add the following lines: ![image](https://hackmd.io/_uploads/r1lgdE3Yp.png) _Note:_ if ever you need to install other Python packages in this environment, you may simply use the command ``` conda install -c conda-forge [package name]