Programming Assignment 0
Introduction
This assignment gets you started with the basic tools you will need to complete all of your programming assignments. This project will
- ensure that you have correctly installed Python 3
- give you practice using a text editor to write Python programs, and
- give you practice running Python programs and using command line features.
Problem Description
You are a CS 3642 student who needs to install Python, configure it for command line use, and learn how to use a programmer's text editor to create and edit Python source code.
Solution Description
Part 1: Python
If you are unfamiliar with Unix command shells, see Matt Might's Survival guide for Unix newbies.
- Download and install Python 3 on your computer and configure your sytem so you can run Python from the command line. See the instructions on the class web site's Resources page.
- Download and install a programmer's text editor. You may end up trying out several over the course of the semester before you settle on one. See the Text Editors guide on the class web site's Resources page.
- Create a directory for your CS 3642 coursework somewhere on your hard disk. We suggest
cs3642. Note: avoid putting spaces in file and directory names, since doing so complicates the use of some command line tools. - Create a
pa0subdirectory of your CS 3642 coursework directory for your PA0 solution.
Here are the steps to accomplish the tasks described above. You can copy and paste these commands, but I recommend that you type them to build muscle memory.
- On Unix/BASH you can create both of these directories at once with
mkdir -p cs3642/pa0
Windows uses the same commands as Unix for directory navigation and creation,
cdandmkdir, but Windows'smkdircommand doesn't have the-poption, so you'll have to .
- On the command line, go to the
pa0directory you just created and enter:
python3 --version > pa0-output.txt
>redirects the output of a program, in this case to thepa0-output.txtfile. Important note: if the line above doesn't write your Python version to thepa0-output.txtfile then replace the>with2>and try again. Some versions of Python, such as the one installed by Anaconda and miniconda, write the Python version tostderrinstead ofstdout.>redirectsstdoutand2>redirectsstderr. For more information, see the GNU Bash documentation on redirection.
- Open your text editor, create a file in your newly created
pa0directory namednimbly_bimbly.pyand save the following Python program in the file (this text box contains file contents, not commands to type at the shell prompt):
print("\u004D\u0065\u006F\u0077 " * 9)
print("...")
print("\u004D\u0065\u006F\u0077\u0021")
- In your OS command shell,
cdto yourpa0directory run the program you created above:
python3 nimbly_bimbly.py
- Run the program again, redirecting its output to the turn-in file:
Don't forget the extra
>in>>.>>appends to an existing file, a single>overwrites an existing file.
python3 nimbly_bimbly.py >> pa0-output.txt`
At this point your pa0-output.txt file should contain your Python version and the output from nimbly_bimbly.py.
Part 2: Libraries
AI software development, like most software development, relies on many packages outside the Python standard library. In this part you will:
- create a virtual environment to keep project-specific dependencies separated from each other, and
- install a few essential and popular libraries for AI.
Follow these steps. If you're not sure what's going on, see the Background section below.
- Create a virtual environment. In your
pa0directory, execute:
python3 -m venv venv
- Activate your virtual environment.
On a Unix-like OS (e.g., Linux or macOS):
source venv/bin/activate
or in Windows PowerShell:
venv\bin\activate.ps1
- Install essential and popular AI libraries. First, download this
requirements.txtfile to yourpa0directory. Then:
python3 -m pip install -r requirements.txt
- List the installed packages in your venv and add it to the assignment output report.
python3 -m pip freeze >> pa0-output.txt
You'll notice that the output above differs from the requirements.txt file.
You're done! You can now turn in your pa0-output.txt file.
Background
Packages
There are two meanings for "package" in Python:
- Subdirectories into which modules are organized. See Python's module documentation for details.
- A distribution of 3rd-party software, e.g., Python modules and supporting files, native code, etc.
Here we discuss the second meaning.
Installing Packages
The pip3 command downloads and installs packages.
- Packages come from the Python Package Index by default.
pip3is quite flexible, allowing you to install from many kinds of sources. See the Python package tutorial for details.
You can invoke pip3 in two ways, for example, to install ipython:
python3 -m pip install ipython
or
pip3 install ipython
Virtual Environments
Different Python projects may use different versions of the same package. To avoid conflicts, use virtual environments.
In the root directory of your Python project, create your virtual environment with:
python3 -m venv venv
This creates a virtual environment in the venv subdirectory of your project root directory. Activate the virtual environment on macOS or Linux with:
source venv/bin/activate
or in Windows PowerShell (if this doesn't work, try Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser. See venv docs for details.):
venv\bin\activate.ps1
Deactivate a virtual environment with (macOS, Linux, or Windows):
deactivate
Direnv
To make activating and deactivating easier, I recommend using direnv.
Put this content into a file called .envrc in your project root directory.
export VIRTUAL_ENV=venv
layout python3
Then give direnv permission to use the .envrc file to activate environments by entering the following shell command in your project root directory:
direnv allow .
No whenever you enter this directory, or a subdirectory within it, your Python venv will be activated, and whenever your enter a parent directory, it will be deactivated.
Turn-in Procedure
Submit your pa0-output.txt file to the assignment on D2L.