In [1]:
from notebook.services.config import ConfigManager
from IPython.paths import locate_profile
cm = ConfigManager(profile_dir=locate_profile(get_ipython().profile))
cm.update('livereveal', {
              'theme': 'solarized',
              'transition': 'zoom',
              'start_slideshow_at': 'selected',
})


Out[1]:
{'start_slideshow_at': 'selected', 'theme': 'solarized', 'transition': 'zoom'}

Modules, Imports and Packages

Dr. Chris Gwilliams

gwilliamsc@cardiff.ac.uk

Python Modules

We have seen that there are many things one can do using Python, but this barely touches the surface.

Python uses modules (a.ka. libraries) to extend the basic functionality and we have 3 ways of doing this:

Standard Modules

These are modules built into the Python Standard Library, similar to the built in functions (type, len) that we have been using.

The majority of them are listed here

Exercise

Follow the link in the previous slide and find the documentation for the random module.

External Modules

These are libraries, written by developers (like you), that extend the functionality of Python. They do things like:

  • Web Scraping
  • Network Visualisation
  • Neural Networks
  • Gaming

We will cover these a bit later in the course.

Local Modules

These are .py within your file system and we will look at these later on in the session.

DO NOT EVER SAVE A SCRIPT WITH THE SAME NAME AS A MODULE YOU USE

import statements

A Python script is, typically, made up of three things at the high level:

  1. import - modules you can use within your code
  2. Executable code - the code you have written
  3. Comments - ignored by the interpreter

In [3]:
import random

dir(random)


Out[3]:
['BPF',
 'LOG4',
 'NV_MAGICCONST',
 'RECIP_BPF',
 'Random',
 'SG_MAGICCONST',
 'SystemRandom',
 'TWOPI',
 '_BuiltinMethodType',
 '_MethodType',
 '_Sequence',
 '_Set',
 '__all__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_acos',
 '_ceil',
 '_cos',
 '_e',
 '_exp',
 '_inst',
 '_log',
 '_pi',
 '_random',
 '_sha512',
 '_sin',
 '_sqrt',
 '_test',
 '_test_generator',
 '_urandom',
 '_warn',
 'betavariate',
 'choice',
 'expovariate',
 'gammavariate',
 'gauss',
 'getrandbits',
 'getstate',
 'lognormvariate',
 'normalvariate',
 'paretovariate',
 'randint',
 'random',
 'randrange',
 'sample',
 'seed',
 'setstate',
 'shuffle',
 'triangular',
 'uniform',
 'vonmisesvariate',
 'weibullvariate']

Exercise

Using the dir method (and the documentation), import the random module and generate a random number between 42 and 749.


In [4]:
import random #import section

print(random.randrange(42,749)) #code section


480

In [5]:
from random import randrange

print(randrange(10,300))


101

Modules and Packages

A Python module is just a .py file, which you can import directly.

import config (relates to config.py somewhere on your system)

A package is a collection of Python modules that you can import all of, or just import the modules you want. For example:

import random (all modules in random package)

from random import randint (importing module from packages)

Import dos and don'ts

You can (and will) import many modules in one script, PEP asks that you follow this structure:

import standard_library_modules
import external_library_modules
import local_modules

More info on this and other styles can be found here

You will also see that some people will import multiple modules in one line:

import os, sys, csv, math, random

Do not do this, it makes your code hard to read and modularise

However, it is good to import multiple modules from the same package in one line:

from random import randrange, randint

Writing Your Own Local Modules

Exercise

Create a file and call it city.py.

Put some variables in there that describe a city of your choice (size, population, country etc)

Now, create a file and call it main.py.

Import your city.py file and print out the city information with formatted strings.

https://gitlab.cs.cf.ac.uk/scm7cg/cm6111_python_modules/tree/master


In [2]:
import city

print(city.name)
print("This city has {0} people".format(city.pop))


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-24c84227b539> in <module>()
----> 1 import city
      2 
      3 print(city.name)
      4 print("This city has {0} people".format(city.pop))
      5 

ImportError: No module named city

Installing packages

Ever used aptitude or yum on Linux? These are package managers that allow you to extend the functionality of the system you are using. Python has these, in the form of pip and easy_install.

Exercise

Which one of these should you use? (Cite your sources)

pip easy_install
actively maintained partially maintained
part of core python distribution support for version control
packages downloaded and then installed packages downloaded and installed asynchronously
allows uninstall does not provide uninstall functionality
automated installing of requirements if an install fails, it may not fail cleanly and leave your environment broken

pip

The recommended tool for installing Python packages.

Stands for Pip Installs Packages. Packages can be found on PyPi

Usage:

pip install <package-name>

Note: Some packages will require administrator privilges to be installed.

Exercise

  • Install a package called blessings
  • Find the documentation on PyPi
  • Write a script that uses blessings
  • Make the script print Sup, World in bold
  • List 3 commands you can run with pip

Virtual Environments (virtual env)

Picture this: You are given a project to work on in a team. You install some packages, write some code and push it to git. Your team-mates say they cannot get it to run.

Why can they not get it to run?

  • Modules not installed
  • Modules won't install
  • Different operating system
  • Different Python version

Any number of these and more.

VirtualEnv

pip installs packages globally by default. This means your Python code is always affected by the current state of your system. If you upgrade a package to the latest version that breaks what you are working on, it will also break every other project that uses that system.

VirtualEnv aims to address this. This package creates an isolated Python environment in a directory with your name of choice.

From here you can, specify Python versions, install packages and run code.

virtualenv <environment_name>

That is how you get started. Do not type this yet!

What does virtualenv <env> do?

Installs an isolated Python environment in a directory named after your env variable. All scripts are put into the bin folder, like so:

Exercise

Create a virtual environment, called 'comp_thinking'

Activating your virtual environment

Unix: source bin/activate

Windows: \Scripts\activate

This adds the scripts in bin to your PATH, so they are executed when you run pip or python.

You can also call the scripts directly:

bin/python <script>.py

Exercise

Activate your environment!

Deactivating an environment

Guess...

deactivate

It is that simple. If you do not want to use the environment again, then you can simply delete the folder.

Exercise

You guessed it, deactivate your environment!

Exercise

  • Create a new virtual environment, call it test
  • Activate it (or just use the scripts)
  • Install the terminaltables package
  • Write a script to read details about the user (favourite movie/game, age, height etc)
  • Use the documentation to print an ASCII table of these data

Command Line Arguments

So, we can output with print and we can input with input. But...input relies on user interaction as the script runs. Here, we can use arguments in the command line to act as our input.

python <script>.py argument some_other_argument

NOTE: This is only a brief intro to using arguments and we will come back to these as the course progresses.


In [4]:
import sys #system package to read command line args


print(len(sys.argv))
print(sys.argv)


3
['/usr/local/lib/python3.4/site-packages/ipykernel/__main__.py', '-f', '/Users/encima/Library/Jupyter/runtime/kernel-21926d1d-70ac-4e16-8ec5-a8b802d20df1.json']

Exercise

Rewrite the movie exercise you wrote in the last session to use command line arguments

No homework, do coursework