Welcome!
Everything is fine.

Virtual Environments

Virtualenv is important if you want to install/import any of thousands of non-standard Python modules.

Virtualenv in CS 304

I'm going to recommend that we avoid re-installing by making one virtualenv that is shared among a number of projects (directories). At the end of the semester, our cs304 folder will look a little like this:

 
cs304/ 
    assignments/
        ajax/
        contacts/
        crud/
        er/
        forms/
        lookup/
        queries/
        web-pages/
    mysql1-queries/
    project/
        alpha/ 
        beta/ 
        draft/ 
    ...
    venv/ 
        bin/ 
            activate 
            pip 
            python 
        lib/ 
            python3.9/ 
                site-packages/ 
                    flask
                    pip
                    pymysql
                    bcrypt

So, in other words, we'll create the venv directory in our cs304 directory, and put our various other folders as siblings or nieces to the venv directory. We will install the extra packages into the site-packages folder.

We'll all create our virtual environments and install pymysql and flask, together.

 
$ cd ~/cs304 
$ virtualenv venv 
$ source ~/cs304/venv/bin/activate 
$ pip install pip --upgrade
$ pip install pymysql 
$ pip install flask
$ python 
>>> import pymysql 
>>> pymysql.__file__ 
>>> quit

That's a double underscore around the word file.

The source Command

We use the source command to activate our virtual environment. We'll do this every time we want to use our venv. We'll only do the pip commands once per package that we want to install in the venv.

Optional Repetition

If that works, let's repeat to see what steps we have to do all the time versus which ones we have to do only once. First, let's close out of what we were doing:

 
$ deactivate 

Now, start again:

 
$ source ~/cs304/venv/bin/activate 
$ python
>>> import pymysql

Does that work?

Adding cs304dbi to the Virtual Env

The pip command is great for installing public packages to the virtual environment. These public packages are hosted in the cloud (typically from PyPi. But we will also use a home-grown Python module that isn't on pypi.org, namely cs304dbi.py. So, we also need to copy that into the VENV.

(We will probably not do this the first day we use virtual environments; we might wait for the second day.)

The following commands will copy the cs304dbi.py file into your virtual environment:

source ~/cs304/venv/bin/activate
~cs304flask/pub/bin/install-cs304dbi

You can test that it worked like this:

source ~/cs304/venv/bin/activate
python -m pydoc cs304dbi

If the installation fails, talk to Scott

Summary

Virtual environments allow you to install Python packages and modules for your own projects, without concern for permissions from the system administrator or with different, possibly conflicting, sets of modules for different projects.

Concepts and tool:

  • You start by creating a venv (a special folder)
  • You have to activate a venv in order to use it or install things into it
  • You have to activate the venv in every terminal or shell where you want to use it
  • To activate it, use source path/to/venv/bin/activate, modifying the pathname as needed.
  • Use the pip command to install packages from the cloud into the venv

These are the commands for CS 304 for your personal account:

cd ~/cs304/
virtualenv venv         # creates virtual environment folder
source venv/bin/activate
pip install --upgrade pip
pip install flask
pip install pymysql
pip install bcrypt
~cs304flask/pub/bin/install-cs304dbi

You can pip install any additional packages you might need.