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]:
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
In [3]:
import random
dir(random)
Out[3]:
In [4]:
import random #import section
print(random.randrange(42,749)) #code section
In [5]:
from random import randrange
print(randrange(10,300))
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)
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
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))
| 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 |
The recommended tool for installing Python packages.
Stands for Pip Installs Packages. Packages can be found on PyPi
pip install <package-name>
Note: Some packages will require administrator privilges to be installed.
Any number of these and more.
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!
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)
No homework, do coursework