Python is a popular language for research computing, data science AND and general-purpose programming. We recommend using Anaconda, an all-in-one installer of python and many of it's most useful packages for scientific computing. Please make sure you install Python version 3.x (e.g., 3.6 is fine).
We will teach Python using the Jupyter notebook, a programming environment that runs in a web browser. For this to work you will need a reasonably up-to-date browser. The current versions of the Chrome, Safari and Firefox browsers are all supported (some older browsers, including Internet Explorer version 9 and below, are not).
Windows
Open (https://www.anaconda.com/download/#windows) with your web browser.
Download the Python 3 installer for Windows.
Install Python 3 using all of the defaults for installation except make sure to check Add Anaconda to my PATH environment variable.
macOS
Open (https://www.anaconda.com/download/#macos) with your web browser.
Download the Python 3 installer for OS X.
Install Python 3 using all of the defaults for installation.
Conda is an open source package management system and environment management system that runs on Windows, macOS and Linux. Conda quickly installs, runs and updates packages and their dependencies. Conda easily creates, saves, loads and switches between environments on your local computer.
Python language companion that may help you enjoy your journey.
Start using Jupyter Notebook
Windows
Open the Anaconda prompt. You should see a black box.
Copy/paste the following command into the prompt and press Enter: jupyter notebook
The Jupyter notebook interface should pop up with your default browser.
macOS
Open a terminal window (Find the "Terminal" app in you Applications or use Spotlight search to search for terminal). You should see a white box.
Copy/paste the following command into the terminal and press Return: jupyter notebook
The Jupyter notebook interface should pop up with your default browser.
Jupyter graffiti (for interactive tutorial screencast)
Open the Anaconda prompt (for Windows) or a terminal window (for macOS).
Copy/paste the following command into the terminal and press Enter: conda install -c willkessler jupytergraffiti
When you see Proceed ([y]/n)?, type y and press Enter
If this doesn't work, you can also try entering the following command into the terminal and press Enter:
pip install -c willkessler jupytergraffiti
We will be using the commercial solver Gurobi in this unit. We will assume you have installed Anaconda Python and learned how to use jupyter notebook, jupytergraffiti from QBUS1040 (see the section above).
Register a Gurobi account as an academic user at https://pages.gurobi.com/registration . Please register as a student using your university email address and don’t lose the password.
Go to https://anaconda.org/Gurobi/gurobi. Copy the command and paste it to your terminal. When you see Proceed ([y]/n)?, type y and press Enter
If that doesn't work (due to your geographical location), you may want to try pip install -i https://pypi.gurobi.com gurobipy
Request for a license at https://www.gurobi.com/downloads/end-user-license-agreement-academic/ and you will obtain a Gurobi key. Copy and paste the Gurobi key to the terminal.
Enter jupyter notebook at the terminal. Start a new notebook for this tutorial, rename it and save it in your folder.
Try running the following code in your jupyter notebook:
import gurobipy as grb
M = grb.Model('my_model')
If your code runs without an error message, then you have successfully installed the Gurobi for this unit.
Note: Some Mac users may encounter an error from Jupyter Notebook saying the kernel is dead after running Step 5 in the installation guide. This is probably due to a memory access violation. To check if your machine encounters such problem, please type the following into the terminal line by line:
python
import gurobipy as grb
If the machine would spit out an error message “segmentation faulty:11”, then you can try the following steps to fix the problem:
Copy and paste the following command into the terminal, please proceed with yes for all the changes when asked: conda config --add channels conda-forge
Copy and paste the following command into the terminal, please proceed with yes for all the changes when asked: conda update -n base conda. Close the current terminal when done.
Start a new terminal and copy and paste the following command into the terminal: conda update --all
Type conda list into the terminal and you should see most of your packages are associated with a particular channel conda-forge
Try Step 5 from above again.
Please come forward to us (during the tutorial or during consultation hours) if you need help with the Gurobi installation. You will need Gurobi for all of your assessments in this unit.
You may need to install a couple of popular packages in this new environment such as numpy and pandas. But they can be easily installed with one line in the terminal. Google the package that you need and look for the installation command that starts with conda install ...
We will be using Kevin Sheppard's 'arch' package and the latest version (0.11.0) of the 'statsmodel' package in this unit.
Download the 'arch' package
1. Open the Anaconda prompt (for Windows) or a terminal window (for macOS).
2. Copy/paste the following command into the prompt/terminal and press Enter: conda install arch -c bashtage
3. When you see Proceed ([y]/n)?, type y and press Enter
Update 'statsmodels' to the latest version
The 'statsmodels' package comes together with the standard Anaconda Python distribution. To ensure we have the latest version (0.11.0), we can ask anaconda to update the package.
1. Open the Anaconda prompt (for Windows) or a terminal window (for macOS).
2. Copy/paste the following command into the prompt/terminal and press Enter: conda update statsmodels
5. When you see Proceed ([y]/n)?, type y and press Enter.
CVXPY (maybe useful for honours students)
We can use conda to install the package CVXPY via the channel conda-forge.
Open the Anaconda prompt (for Windows) or a terminal window (for macOS).
Copy/paste the following command into the terminal and press Enter: conda config --add channels conda-forge
Copy/paste the following command into the terminal and press Enter: conda config --set channel_priority strict
Copy/paste the following command into the terminal and press Enter: conda install -c cvxgrp cvxpy
When you see Proceed ([y]/n)?, type y and press Enter.
Alternative
You can also try the following:
conda install pip
pip install --upgrade setuptools
pip install cvxpy
Sometimes some packages only support certain versions of Python, say, Python 3.7.
If you have installed Anaconda with Python 3.8 or above, you can either re-install your anaconda with Python 3.7 or you can create an environment for Python 3.7.
How to create an environment:
conda create --name py37 python=3.7
To start using the environment, you need to type the following command in the terminal:
For Windows: activate py37
For Linux, macOS: source activate py37
If you encounter any errors with either of the above commands, please let us know via email at jessica.leung@sydney.edu.au