Running into issues? Check out the PyCharm Bugs handout, post on Ed, or come chat with course staff in person!
In CS106A, you will be editing and running your code using an application called PyCharm. PyCharm is what is known as an IDE, or Integrated Development Environment, which means that it allows us to edit our code, browse our files, run our programs and debug them, all in the same place. This handout will walk you through installing 1) the language Python and 2) the IDE PyCharm on your own computer.
In order to be able to run Python programs on your computer, you need
to install a Python Interpreter. An interpreter is a program
that is capable of reading a .py
file that you have
written, and translating the Python code in that file to instructions
that your computer can easily execute. Begin by downloading Python. Select the link corresponding to
your operating system and download the latest version of Python 3:
After clicking the link above:
Windows Downloads (If you're using a relatively new Windows computer, use this link)
After clicking the link above:
Windows 32-bit installer (If you're using an older Windows computer, download this file)
Note:
Macs come with a version of Python installed, but this is an older version of Python. CS106A requires the use of the newest version of Python, so even if you think you already have Python installed, please follow the steps above.
Note:
When you open the downloaded file, before installing, there should be an option that says "Add Python VERSION_NUMBER in PATH." Make sure to check this box. Then, continue installing normally.
To get started, download and install the community version of PyCharm. You may have to scroll down on the download page to see the community version.
.dmg
dropdown and select either Apple
or Intel based on what you saw in step 2.
.dmg
file and drag PyCharm into your Applications folder..exe
file and install
PyCharm, using all the default options.
At the end of the PyCharm installation process, open PyCharm, and you'll see a welcome screen:
When you work on assignments in CS106A, we'll have you download a PyCharm project, which is a folder
containing Python code files (.py
files) and sometimes other files your program will
need, like images or text files. You'll open up the entire project folder using PyCharm, then edit
the files in PyCharm.
To get set up for HW2B and gain familiarity with the PyCharm environment, download the project below and open it in PyCharm as follows:
image-grid
folder. Note: Whenever you open
projects in PyCharm,
open the folder containing the files you want to edit, as opposed to just
the files themselves. Sometimes,
Windows computers will create another nested folder when you extract a folder. Select
the innermost image-grid
folder to open.
Nice, you've just opened a project in PyCharm! Upon clicking "Open," you should be greeted by a window that looks like this:
We now want to select our Python interpreter - there are several ways to set this. You might see this pop-up below, and you can go ahead and click the option indicated by the red arrow below.
If you don't see that, you can follow the steps below:
At the bottom right hand corner, you can click where the arrow is pointing
You can either click the Python version listed or if you don't have that, click "Add Local Interpreter" and then "Add Local Interpreter". You can then click "Select Existing" and everything should populate, and you can just click "Ok".
What you see might look different, but just try different things to select some version of Python as your interpreter. You just want to make the bottom right-hand corner look something like this instead of it saying "No Interpreter"
On the left side of your screen, the "Project" tab should be open. This is where you can see files
in the image-grid
project folder you currently have open. If you don't see this tab
on the left, click the folder icon in the top-left or do Command + 1
.
To expand the image-grid
project folder you've opened, hit the >
next to
the image-grid
folder. You should see some Python (.py
) files and some
image files inside. The Python files contain Python code!
Double-click on image-grid.py
, and you should see a new panel appear. Now, you can view
the contents of this Python file. This is the file editor window. Here, you can view and edit code.
You'll write some code to replace the pass
statements and complete HW2B, for now we'll
leave these functions empty.
You might see a pop-up about "No Python interpreter configured for the project." Select the version of Python you just downloaded (in this case, "Use Python 3.12"), and you should be all set.
Now, click 'Terminal' in the bottom-left corner.
This will pull up a new terminal panel in your PyCharm window:
In PyCharm, we use the terminal to run our programs (unlike in the Experimental Server, where we can just hit the "Run" button). To run a program, we type something into the terminal and hit enter. First, let's test that you installed the latest version of Python correctly.
Type the following into your terminal:
python3 --version
Here's how it looks to type something in your terminal. Note that you should not be typing anything in the panel on the right, your file editor window.
After hitting enter, you should see output that looks like this:
Next, we'll have you install the "Pillow" library, which you need for the image-processing code you're going to write. Type the following into your terminal and hit enter:
python3 -m pip install Pillow
Note that if you are using Windows, your command might be py
instead of python
This will take a couple of seconds, but if everything worked correctly, you should see something like "Successfully installed Pillow" near the end of the text that gets printed out. You should see something like this:
If you already had Pillow installed, you'll see "Requirement already satisfied" which is also a sign of success.
Nearly there! Finally, to actually run the code in image-grid.py
, we have to tell the
terminal that we want to run a Python program, and then we tell it which
program to run, e.g. image-grid.py
. Sometimes, we'll give even more information after
this, like the name of an image or the "mode" we want to run the program in.
Enter the following into your terminal (you can replace "Alice" with your name if you like) to make
the image-grid.py
program say hello to you!
python3 image-grid.py -hello Alice
Congratulations! You're now done with the PyCharm setup process. If you ran into any bugs along the way, check out this PyCharm Bugs handout.
Also note that you've just opened the HW2B project and you're ready to start working on the assignment by writing code in the file editor window on the right. See the HW2B assignment handout on the course website for next steps, and if everything's working, you can start with part b on that handout (HW2B handout will be published at the end of week 2).