Welcome!
Everything is fine.
Virtual Environments¶
Virtualenv is important if you want to install/import any of thousands of non-standard Python modules.
As of this writing (Fall 2025), the newest version of Python installed on the CS server is Python 3.12. The version of Python compiled into Apache, the production web server, is Python 3.9. So, we will use Python 3.9 in CS 304, just in case you decide to deploy your software. Python 3.9 is pretty new.
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
python3.9 -m venv venv
source ~/cs304/venv/bin/activate
pip install pip --upgrade
pip install flask
pip install pymysql
pip install bcrypt
~cs304flask/pub/bin/install-cs304dbi
We can see that it worked by running python, importing a package, noticing that the import worked, and see that the package was imported from our virtual environment:
$ python >>> import cs304dbi >>> cs304dbi.__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 cs304dbi
Does that work?
Appendix¶
The commands above had a number of pip install commands, but also
another, custom, command that adds a home-grown package to your
virtual environment. Let's explain that.
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.
The following commands (which we did above) copied 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 failed, talk to Scott.