Running Python
This page is organized as follows:
- Running Python on Eustis
- Running Python on Your Own Machine
- Getting Test Output for Homework
- Troubleshooting
- Advanced Topics
We recommend using Python on your own computer, if possible, for this class.
Running Python on Eustis
Most students will find it easiest to use their own computer system for class work. However, if you do not have your own computer, you could use the eustis.eecs.ucf.edu system.
You can login to your account at eustis.eecs.ucf.edu using your UCF NID as your login. (Thus your password is your UCF NID password.)
To access Eustis, use a ssh client like putty from anywhere on campus. You may need to specify your NID as the login name when you start the ssh connection to Eustis (if your NID is different than your Windows username). From off-campus, you will need a VPN connection to UCF first. For a VPN account and help, go to http://www.noc.ucf.edu/VPN/default.htm
Starting Python
On Eustis, the Python system is found in /usr/bin/. And python3 should be in your PATH by default. (Thus you shouldn't have to do anything special to set this up, although it is in /usr/bin.)
To run the Python interpreter, simply issue the command python3 at the shell's prompt.
Exiting Python
To quit the Python interpreter, use the statements quit() or exit() or type and end-of-file (Control-D).
Pytest
The Pytest framework for unit testing is already installed on Eustis, and pytest should be in your PATH already (it is in /usr/local/bin/pytest).
Running Python on Your Own Machine
Python is most convenient to use on your own computer.
Getting Python for your Own Machine
Python is available on many platforms, including Linux, Macintosh, and Windows. You can download the latest release (e.g., 3.7.2) from python.org/downloads.
Windows
For a Windows machine, download and run the installer. When installing, check the box to add Python to your PATH.
To use Python, from the start menu, run IDLE as an app, or use python from the command line. You might want to pin IDLE to the taskbar or place a shortcut on the desktop for later convenience.
(If you didn't have the installer put Python in your PATH, or if that didn't work, and if you want to be able to use Python from the command line, then you should add relevant directory to your PATH environment variable. For the 32 bit installation, the relevant directories to add are %LOCALAPPDATA%\Programs\Python\Python37-32 and %LOCALAPPDATA%\Programs\Python\Python37-32\Scripts where %LOCALAPPDATA% should be replaced with the path on your machine to your Windows user's local app data directory, which for me was C:\Users\Gary\AppData\Local. For the 64 bit installation, the the relevant directories are %LOCALAPPDATA%\Programs\Python\Python37 and %LOCALAPPDATA%\Programs\Python\Python37\Scripts. You can use the %LOACLAPPDATA% syntax directly when setting your Path variable on Windows. To set environment variables in Windows XP/Vista/7/8, start the control panel, then click on "System", then the "Advanced" tab, then click on "Environment Variables". On Windows 10 you can do the same thing by starting the Settings app, then click on "System", then the "About" item in the lefthand menu (at the bottom), then at the bottom (under "Related Settings") click on "System Info", then from the popup click on "Advanced System Settings" (in the left menu), then click on "Environment Variables" (in the advanced tab).)
Pytest
Install pytest by first installing the latest version of Python (as above), and then running the command in your operating system's shell (e.g., cmd on Windows or a terminal window on MacOS): pip3 install -U pytest
Note that the pip3 install command is not to be issued to the Python interpreter, instead it should be given to your computer's operating system (OS), so that the OS runs this tool. This why you should give the above command to an OS command prompt (e.g., cmd in Windows).
To use pytest from inside IDLE, first edit a file that you want to test or the testing file itself using IDLE, by right clicking on the file, and then choosing the "Edit with IDLE" menu item and clicking on the "Edit with IDLE 3.7 (32-bit)" item that appears. This ensures that IDLE is working in the directory where your files are. Then from IDLE's Run menu select "Run Module" (or type the shortcut, the F5 key). Then you can run pytest on a file such as test_f.py by giving the following statements to the Python interpreter:
import pytest pytest.main(["test_f.py", "--capture=sys"])
You will see the output of the pytest tests in IDLE, which you can copy and paste into a file to hand in.
Cygwin
(If you use cygwin, you can get the cygwin version installed by cygwin itself; then use python3 at the command line. This may be slightly out of date. Or install the latest version from python.org and make sure that the desired version occurs in your PATH before the cygwin version, if it is installed.)
(If you use cygwin, then it may help to use shell scripts to invoke pytest, if the cygwin version occurs before it in your path. A suitable shell script is available to download from Gary Leavens's windows bin in the script named pytest.)
More help
If you have trouble with a system, please see the course staff. If you fix such a problem yourself, please let us know how you fixed the problem.
Macintosh
For a Mac, use Safari to download the installer and then run it. After installation you will find (use the finder) "Python 3.7" (or perhaps a newer version, say "Python 3.8") in your Applications directory. In that run IDLE as an app, and pin that to your dock.
To use Python, run IDLE as an app (by double clicking on it in the finder). It will be helpful to pin that to your dock.
To use the version of Python you downloaded from the command line (i.e. from the Terminal app), run the script named Update Shell Profile.command from the finder (by double clicking on it), and then use python3 from the command line.
You can install and run pytest as described above for Windows machines. To open a command line, use the "Terminal" app, which you can find by searching for Terminal in the Finder. Be sure to use pip3 install -U pytest, with a 3 in "pip3" (as apparently you will not have "pip" installed).
Getting Test Output for Homeworks
Homework problems require you to capture the output of our testing.
A prerequisite for the testing to work is that your function code file (say f.py) and the testing file (say test_f.py) must be in the same directory. Furthermore the operating system (OS) command shell (cmd on Windows or the terminal on MacOS) must have that directory as its current working directory. (It's possible to ask IDLE what its current working directory is, to do that first do import os and then os.getcwd(). You can also change the working directory by using another procedure in the os module, by executing os.chdir(os.path.dirname("your directory's path")).)
You can run the tests from the Python interpreter, IDLE. First, make sure that IDLE has the right directory as its current working directory. One way to do that is to right click on the file you are testing (say f.py) and select the "Edit with IDLE" menu item. If you edit using IDLE, then you can use the File menu and select "Save As". Once IDLE is in the right directory, for a function named f, you would execute the following in IDLE: import pytest and then pytest.main(["test_f.py", "--capture=sys"])
An easier way to capture outputs of a test file, such as test_f.py is to use the operating system (OS) shell (e.g., cmd on Windows or a terminal window on the Mac). First change to the right directory, using the command cd. (The right directory is the directory where the f.py and test_f.py files are.) Then issue the following command at the command line to the OS shell:
pytest test_f.py >f_tests.txt
The above command redirects the standard output to the file to the right of the >, in this case f_tests.txt.
Troubleshooting
The Test files don't seem to be Python files
If you don't see the "Edit with Idle" menu item when you right click on a test file, be sure that you have extracted the test files into a directory, instead of viewing them in their zip file container. The zip file container doesn't allow you to do everything with a file that the operating system does. You must first extract all the files from the zip file.
To extract files from a zip file container, open the zip file (e.g., hw1-tests.zip, with the File Explorer (in Windows), and click on "Extract all" from the "Extract" menu.
Can't Run Pip3 from the Command Line
If the pip3 command doesn't execute on Windows, you may need to uninstall and re-install Python, making sure to click the button to put Python in your PATH. Once this reinstall is complete, start a fresh command prompt (cmd) and you should be able to run pip3.
On a Mac, make sure you have run the script called Update Shell Profile.command that is found under the Python subdirectory of the Applications directory as above (double click on it in the Finder). Then pip3 should work.
Type Errors
If you get a type error message, look at the source code in the given location and see if you can understand why that code may be wrong (in some cases).
Other Problems
If you are having a problem not described above, or if the solutions listed above are not working for you, you can look at the webcourses discussions, where we often post answers to other people's questions. If none of that help then please don't hesitate to contact the course staff.
Advanced Topics
There are a variety of programming environments for Python that are described below.
IDLE
The standard user development environment for Python is IDLE, which is installed with Python.
Python Eclipse Plugin
The PyDev plugin for Eclipse is good if you like working in Eclipse.
Running Python from Within Emacs
If you want to run Python from within Emacs, you can use python-mode, which comes pre-installed in Emacs.
Windows Keystrokes in Emacs
If you like to use the standard Windows keystrokes for cut, paste, and copy, customize Emacs to use CUA-mode by default. (This assumes that you have Emacs version 22 or later. Use the menu "Options", select "Customize Emacs", and from there the select "Top Level Customizations". then select the "Convenience" group by clicking on the "go to group" link, and then go to the Cua group, and click on the "Value Menu" for enabling this customization.
If the above doesn't work, or if you have a version of Emacs previous to 22, put the following in your emacs initialization file, ~/.emacs.el. (The lines that start with a semicolon are comments, and can be omitted.)
(if (< emacs-major-version 22) (progn ;; Make emacs more like standard windows applications in terms of ;; mouse selections (this is similar to pc-select). ;; One advantage is this is that it makes Dragon Dictate work with Emacs. (require 'cua) ;; Make the behavior of emacs on C-c, C-v, C-x, and C-z to be ;; just like that of be as in standard window apps, so you can use ;; these keys to copy, paste, and select when there is a selection ;; active, then uncomment the following. (CUA-mode t) ;; Make C-c leave the region active, which is what happens under Windows. (setq CUA-mode-keep-region-after-copy t) ) ;; The same for emacs version 22 (and hopefully higher...) ;; but not for aquamacs, which already has it. (if (not (featurep 'aquamacs)) (progn (cua-mode) (setq cua-enable-cua-keys t) (setq cua-mode-keep-region-after-copy t) ) ) )
Alternatively, you can use Gary Leavens's .emacs.el file which includes the above lines and several other things. It should be saved into .emacs.el in your home directory. (On Windows you may not have a home directory yet, so pick one. You also have to set HOME in the environment; this is automatic in Unix and hence on Mac OS X, but needs to be done by hand in Windows.)
Making Emacs find your Customization File
On Linux to have emacs find your ~/.emacs.el file you just have to have it in your home directory. On Windows machines, if the emacs is not finding your ~/.emacs.el file, it is because it doesn't know where your home directory is. To do this, you have to set your environment variable HOME to the right directory.
For Vi Users
If you are a vi user, you may want to use viper-mode in emacs to get the vi keystrokes and still use all the wonderful features of Emacs. To do this, put the following in your .emacs.el file:
(viper-mode)
Last modified Wednesday, January 16, 2019.
This web page is for COP 3223H at the University of Central Florida. The details of this course are subject to change as experience dictates. You will be informed of any changes. Please direct any comments or questions to Gary T. Leavens at Leavens@ucf.edu. Some of the policies and web pages for this course are quoted or adapted from other courses I have taught.