Getting Started with PyCharm and esubmit

We recommend using PyCharm for general Python programming in CS 2400. Steps for setting up an MSOE windows laptop:

  1. Open a command prompt and type
            python --version
    
    You should see a version greater than 3.0 listed. If you get a "command not found" error, do the following:
    1. Ensure Python is installed. You can reinstall it from python.org. Download the most recent, 64-bit version.
    2. Locate where Python has been installed. The default location for version 3.9 is
              \Users\your login\AppData\Local\Programs\Python\Python39
      
      If all else fails, you can open a Command prompt and type
              cd \
              dir/s python.exe
      
      There may be multiple versions; use the folder names to determine the best match.
    3. Now, configure your system so executables in the folder containing python.exe are available at the command prompt: start by clicking on the Windows button in the lower left corner and typing "path". Click on "Edit the system environment variables" and then Environment Variables...
    4. Search the bottom box (labeled System variables) for Path and click on Edit...
    5. Click on New followed by Browse... and browse to the folder containing python.exe (that you found above).
    6. Click on OK to close out all of the windows you just opened.
    7. Open a new command prompt and check that the command python now works.
    If this does not work, talk to your instructor.
  2. Open a command prompt and type
            python -m pip install -U mypy
    
    This will install a Python type checker that we will use below.
  3. Download and install the community edition of PyCharm.
  4. Start PyCharm and create a new project. The default for new projects is in \users\your name\PyCharmProjects, but you can change it to a different folder. A good practice is to set up a private git repository for each course you are taking (using GitLab or Bitbucket, perhaps) and create the project inside that repository. Files that are on just one computer should be considered lost!
  5. Copy the following files into the top folder of your new project. Note this will overwrite the main.py they already created, but this is helpful.
  6. Click on the green arrow in the upper right corner to run your program. Note this code is set up to stop reading scores with a blank line, so enter a quiz name followed by a few numbers.

You now have a running Python project using an IDE that's reasonably similar to ones you've used before. But there are some features that we will use in some courses that you may not be familiar with.

Fixing the code

You can set breakpoints in PyCharm and run your code in a debug mode just like IntelliJ. However, entering the data over and over again slows down debugging. The following steps walk you though debugging the provided code and give you tools that can help debug other Python projects.

  1. Click on input1.txt in the Project Browser. The total for these three quizzes is 25.5, so the average is 8.5. Confirm this works by selecting Edit Configurations... from the Run menu, selecting the main configuration, and clicking on the overlapping pages icon. This will create a new configuration called "main (1)". Rename it to something like main input1 and then go to the bottom of the page and check Redirect input from: and browse to the file input1.txt

    Note: since the working directory is the directory that contains input1.txt, it would seem that input1.txt would be sufficient in this box. Apparently it is not.

  2. Click on the Run arrow and confirm the program prints the average 8.5.
  3. Set up a second test using input2.txt. Running your code on this shows an average of 7.0. However, this is incorrect! Open input2.txt in PyCharm and notice the non-negative scores are 8, 7, 0, and 6. The average of these four numbers is 21/4 = 5.25. Let's fix this.
  4. Switch to quiz.py and set a breakpoint at line 20.
  5. With the run configuration still set to the one you created for input2.txt, click on the debugging icon. The program will stop after having computed nonnegatives.
  6. Note you can expand self to examine the attributes. The scores array includes all five numbers, so that seems ok.
  7. Review nonnegatives. Note this array has just three entries; it should have four. See if you can find the error in computing this array.
  8. Once you have fixed how nonnegatives is computed, re-run your program to confirm it is correct. Note you should re-run both cases and maybe test a few additional ones!

Type hints

Review the methods in Quiz. These use Python type hints to document the parameters and return values. Type hints are powerful; they allow a static type checker to catch many mistakes in code. This is especially important in CS 2400 where we will process many different sorts of values. A static type checker can save you hours of debugging.

The first step is to enable a static type checker in PyCharm:

  1. Select Settings... from the File menu.
  2. Click on Plugins and ensure Marketplace is selected.
  3. Enter mypy in the search box.
  4. Select and install "Mypy" by Roberto Leinardi (as opposed to "Mypy (Official)" - Leinardi's version provides better integration).
  5. Restart PyCharm as indicated.
To confirm that static type checking is working, change the return type of average to int and note that the second return is highlighted. Hover your mouse over it to see that this is returning a float (which it should be!) rather than the int. Restore the return type to float.

Note that you can also perform a static type check at the command prompt. Using Windows File Explorer, browse to the folder containing your project, click on the folder box at the top, and enter cmd. This will start a command prompt at that location. Type

        python -m mypy main.py quiz.py
to run the type checker. Assuming your computer is set up to do so, it should print "no issues found in 2 source files". However, it is not critical if this works; the information available in PyCharm should be sufficient in most cases. There are some errors that the PyCharm version will not catch; for example, try changing the line
        num = float(line)
in read_quiz to
        num = line
and note PyCharm does not report the incorrect type of list being passed to the Quiz constructor but that python -m mypy (at the command prompt) does.

Submitting

Demonstrate that you have completed this exercise by submitting your solution (main.py and quiz.py) to esubmit.msoe.edu as exercise1. Most students will have used esubmit before, but if you have not or need a refresher then see this page.