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

Problem Description

You are a CS 8375 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.

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.

  1. On Unix/BASH you can create both of these directories at once with
mkdir -p cs8375/pa0

Windows uses the same commands as Unix for directory navigation and creation, cd and mkdir, but Windows's mkdir command doesn't have the -p option, so you'll have to .

  1. On the command line, go to the pa0 directory you just created and enter:
python3 --version > pa0-output.txt

> redirects the output of a program, in this case to the pa0-output.txt file. Important note: if the line above doesn't write your Python version to the pa0-output.txt file then replace the > with 2> and try again. Some versions of Python, such as the one installed by Anaconda and miniconda, write the Python version to stderr instead of stdout. > redirects stdout and 2> redirects stderr. For more information, see the GNU Bash documentation on redirection.

  1. Open your text editor, create a file in your newly created pa0 directory named nimbly_bimbly.py and 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")
  1. In your OS command shell, cd to your pa0 directory run the program you created above:
python3 nimbly_bimbly.py
  1. 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:

Follow these steps. If you're not sure what's going on, see the Background section below.

  1. Create a virtual environment. In your pa0 directory, execute:
python3 -m venv venv
  1. 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
  1. Install essential and popular AI libraries. First, download this requirements.txt file to your pa0 directory. Then:
python3 -m pip install -r requirements.txt
  1. 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:

  1. Subdirectories into which modules are organized. See Python's module documentation for details.
  2. 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.

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.

Python dependency management has been a mess from the beginning, and still is. You may balk at the redundant package files many project venvs will leave on your hard disk. In most cases a venv takes a only few hundred kilobytes. But many common cases, like a typical Keras + JAX venv, take 600MB-700MB. If you have many such projects, you will have to use your judgment to manage disk space. One approach is to have a common "playground" venv for machine learning work that you use for all of your learning and scratch projects, and have separate local venvs for official, shared projects -- such projects should always have a requirements.txt stored in Git.

There are many virtual environment managers in the Python world. We will use the standard library's venv.

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: See for details.):

venv\bin\activate.ps1

If the command above doesn't work, try Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Once your venv is activated, you can install packages into it. These packages will only be visible to projects using this virtual environment.

Deactivate a virtual environment with (macOS, Linux, or Windows):

deactivate

If your project is tracked in Git, be sure to add venv/ to your project's .gitignore, e.g.:

echo venv/ >> .gitignore

Direnv

To make activating and deactivating easier, I recommend using direnv. If you use Emacs, there's a nice package that integrates direnv into Emacs: envrc.el.

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 .

Now 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. This is very handy for ensuring you don't forget to activate an environment before installing project-specific packages, or forgetting to deactivate the environment after leaving a project.

Turn-in Procedure

Submit your pa0-output.txt file to the assignment on D2L.