image of bot

UMBC CMSC 471 02 Spring 2021
Introduction to Artificial Intelligence

python virtualenv


471 Python Virtual Environment on GL

Using an aima-python module like logic.py requires not only having access to it, but other aima-python modules (e.g., search.py) that it imports, and several packages (i.e., numpy, ipythonblocks, networkx and sortedcontainers) that need to be installed with pip.

We've tried putting all of the required modules (and the ones they require) in your hoework repository, but this does not solve the problem of installing packages.

Here's a solution we recommend for current and future ones to let you

  • Easily run them on gl.umbc.edu and
  • Simplify running them on your own computer.

For running code on gl, the solution uses a python virtual environment and also the environment variable PYTHONPATH. For use on your own computer might use use PYTHONPATH and forgo setting up a virtual environment.


Running 471 python code on GL.UMBC.EDU

(1) Use the environment variable PYTHONPATH to have python always look in a copy of the aima-python directory for modules that you import.

PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules. If you import a module in python it will search for a file in your current directory and then also try each of the directories in the PYTHONPATH variable. Like the Unix PATH variable, it should be a sequence of colon-sperated paths to directories.

You can edit your Unix initialization file (e.g., .bashrc or .cshrc) include an environment variable named PYTHONPATH. If you use the bash shell, edit your .bashrc file to include these lines:

# set directories for python to search for imports
export PYTHONPATH=$PYTHONPATH:/afs/umbc.edu/users/f/i/finin/pub/ai/aima-python/

If you are using csh or tcsh you will add lines to your .cshrc file like

# set directories for python to search for imports
setenv PYTHONPATH ${PYTHONPATH}:/afs/umbc.edu/users/f/i/finin/pub/ai/aima-python/

If you are unsure which shell you are using you can enter the Unix command echo $0 to find out.

This directory is a local clone of the aimi-python repository that we'll keep up to date.

(2) Use a copy of Python via a virtual environment that has required python packages pre-installed.

We've created a python virtual environment that has all of the required python packages that we'll need for 471 installed (e.g., numpy). We'll keep this up to date by installing any new packages that are needed. You can use this version of python by executing one of the following commands, depending on which shell you are usingthis if you are using:

(bash) source /afs/umbc.edu/users/f/i/finin/pub/ai/env/bin/activate

(tcsh or csh) source /afs/umbc.edu/users/f/i/finin/pub/ai/env/bin/activate.csh

After doing that, whenever you call python in your session, you will get this virtual copy that has the needed packages. If you want to revert to using the regular system python, you can execute the Unix command.

deactivate

When you logout and log back in, calling python will launch the default version again.

If you like, you can add the following to your .bashrc file that will define a new command, aipython, that activates the virtual version of python and then calls it

# add an alias to invoke the AI Python virtual environment
alias aipython="source /afs/umbc.edu/users/f/i/finin/pub/ai/env/bin/activate;python"

For csh or tcsh add this to your .cshrc file:

# add an alias to invoke the AI Python virtual environment
alias aipython "source /afs/umbc.edu/users/f/i/finin/pub/ai/env/bin/activate.csh; python"


Running 471 python code on your own Unix or mac computer.

When using your own computer, creating python virtual environments can be useful if you need different Python configurations that might be inconsistent, but for most of us this is not a problem. So you can probably just use pip to install all of the needed packages in your standard python installation. For hw4, for example, you will need to install numpy, ipythonblocks, networkx, and sortedcontainers.

It will still be helpful to setup PYTHONPATH by doing the following.

  • Clone the aima-python github repository on your computer
  • Add the PYTHONPATH environment variable to your system that includes the path to your local copy of aima-python

How you do the second step will vary depending on your operating system: Unix, max osx, or windows.

On Unix you call follow the directions we gave for GL above to add/modify your PYTHONPATH variable in your shell configuration file (e.g., .bashrc or .cshrc). On mac osx, the configuration file to modify is .bash_profile. On windows, you can follow these directions to set or update the PYTHONPATH variable, which say:

"To permanently modify the default environment variables, click Start and search for ‘edit environment variables’, or open System properties, Advanced system settings and click the Environment Variables button. In this dialog, you can add or modify User and System variables. To change System variables, you need non-restricted access to your machine (i.e., Administrator rights)."