VPython is ideal for teaching numerical modeling in introductory physics because its language (such as adding and subtracting vector objects) maps wonderfully onto the mathematical and verbal description we use in physics. VPython in general refers to two necessary parts: (1) the Python language with (2) the Visual package. VPython has evolved since we first began using it in 2003. In 2003, you had to install Python and Visual. The Visual package did not work well with other python packages so we rarely installed additional packages. An expert could get Visual and other packages like Matplotlib to work together, but it was not easy.
Here are the five methods to install VPython or something similar to VPython.
- Install Python and Visual using the installers and instructions at www.vpython.org.
- Install Anaconda and Visual.
- Install Anaconda, iPython Notebook, and iVisual and run VPython code within a notebook in a browser.
- Write GlowScript code in a trinket.
If you are teaching VPython to students in introductory physics, whether in high school or in college, I recommend Method 1 (vpython.org) or Method 2 (GlowScript). The installation is simple, and introductory physics students can pick up the process of: (1) write, (2) run, (3) edit, and (4) re-run with very little effort. Students with zero background in programming can learn how to do this very quickly.
But if your students are advanced or if they have experience with VPython and wish to combine Python with other packages such as Matplotlib or Sympy, I recommend Method 3 (Anaconda and Visual) and Method 4 (iPython Notebook and iVisual). Anaconda and iPython Notebook are frequently used by professional scientists. Experience with Anaconda, iPython Notebook, and other Python packages will help prepare students for future jobs in industry or research, for example.
If you plan to embed VPython code into a blog or other web site, I recommend using Trinket.
This installation procedure given at www.vpython.org is very simple and has not changed since before 2003, although Python and Visual have been upgraded. You can choose to install the version of Visual for Python 2.7 or the version for Python 3. We prefer Python 2.7 because other packages like Serial that you might use with Visual have not been upgraded to Python 3. Many professionals in the Python world still use version 2.7. It is still widely supported with many packages.
This is EASY. There are two installers. First, install Python. Second, install Visual. It's that simple. Thousands of students have installed VPython with no trouble. It is simple, and it works reliably.
Download sample programs from Matter and Interactions.
Go to the GlowScript site, view sample programs, and create an account.
Original VPython programs written for the Matter and Interactions textbook can be run in a browser using GlowScript.
There are some introductory GlowScript tutorials by Rhett Allain. Tutorials 1-7 will get you started.
Installing Anaconda, Visual, and iPython Notebook with iVisual
I. Anaconda and Visual
You can install the Visual package with Anaconda and run VPython programs from the command line. The Anaconda recipe to install visual was created by Matt Craig. Here are the steps to install and run a VPython program using anaconda.
- Install Anaconda. (http://continuum.io/downloads)
- From a command line, type: conda install -c mwcraig vpython
- To run a program called "filename.py", from a command line type: pythonw filename.py
Anaconda has many other scientific useful packages pre-installed, such as Matplotlib for graphing data. THIS is the number one advantage of using Anaconda to install Visual.
II. Anaconda, iPython Notebook, and iVisual
If you installed Anaconda, then it already has iPython and Notebook pre-installed. The only additional package you need for running VPython programs in a notebook is iVisual. Here are the steps to install iVisual (more detailed instructions with screen captures are available):
- From a command line, type: pip install ivisual
- From a command line, type: ipython notebook
- The notebook home screen will open in your browser. Create a new notebook.
- Type the following program into a cell.
from __future__ import division, print_functionfrom ivisual import *#create scenescene = canvas(title='3D scene')sphere()
My Classical Mechanics class used iPython Notebook and iVisual for an entire semester, and it worked wonderfully. Here's a presentation I gave to Guilford College with a notebook.
III. Using Anaconda's Virtual Environments
As you install more and more packages in Python, there is the chance that an installation fails and wrecks your Anaconda library, requiring you to install Anaconda and all of your favorite packages all over again. Therefore, if you use Python for different projects that use different packages, I highly recommend creating virtual environments for each project. A virtual environment is a sandbox where you only have the toys that you want to play with in that particular sandbox.
For example, I starting playing with VPython and Arduinos to create 3D games that can be controlled with sensors and an Arduino. In case I messed something up, I created a virtual environment called pyarduino. So when I'm working on a project with VPython and an Arduino, I go into that environment and install just the packages that I need.
Here are most of the commands that I use to create and use virtual environments:
1. To create a new environment named myenv for example (with a barebones python installation with few packages) , type:
conda create -n myenv python
2. To activate the environment myenv so that installing new packages or running a python program only occurs within myenv, type:
source activate myenv
3. To get out of the environment myenv type:
4. Occasionally I forget what environments I created. To list the environments, use:
conda info -e
Using Trinket to Write and Run GlowScriptTrinket is a web-based programming environment designed for teaching and learning. A "trinket" is program that can be viewed, edited, and run within the browser. You can use Trinket to give example code to students. Then they can modify the code, run it, and view the result of their changes.
You can also embed a trinket in a blog or other web site. For example, here is the familiar bouncing ball from the glowscript.org site embedded as a trinket.
As an aside, you can also use Trinket to create entire course web sites. For more information about Trinket, see my blog post or my sample PHY 3110 Classical Mechanics course.