|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Loading and using modules\n", |
| 8 | + "\n", |
| 9 | + "## What is a module?\n", |
| 10 | + "\n", |
| 11 | + "A *module* in Python is simply a Python `.py` file that contains a list of related functions that can be loaded and used.\n", |
| 12 | + "Modules are similar to what are more generally called libraries in programming languages, which again contain code related to a specific task such as mathematical operations.\n", |
| 13 | + "There are a *HUGE* number of Python modules, and many of them greatly extend what can be done in a normal Python program.\n", |
| 14 | + "In fact, the abundance of free Python modules is one of the best reasons to learn and start using Python.\n", |
| 15 | + "\n", |
| 16 | + "## How can modules be loaded?\n", |
| 17 | + "\n", |
| 18 | + "Python modules can be loaded in a number of different ways.\n", |
| 19 | + "\n", |
| 20 | + "### Loading a module\n", |
| 21 | + " \n", |
| 22 | + "Let's start simple with the math module.\n", |
| 23 | + "Here, we’ll load the math module using the `import` statement." |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": null, |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [], |
| 31 | + "source": [] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": null, |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [], |
| 38 | + "source": [] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "markdown", |
| 42 | + "metadata": {}, |
| 43 | + "source": [ |
| 44 | + "Here we have loaded the math module by typing `import math`, which tells Python to read in the functions in the math module and make them available for use.\n", |
| 45 | + "In our example, we see that we can use a function within the math library by typing the name of the module first, a period, and then the name of function we would like to use afterward (e.g., `math.sqrt()`).\n", |
| 46 | + "Built-in functions such as `print()` do not require the name of the module first since nothing is explicitly imported.\n", |
| 47 | + "\n", |
| 48 | + "### Renaming imported modules\n", |
| 49 | + "\n", |
| 50 | + "We can also rename modules when they are imported.\n", |
| 51 | + "This can be helpful when using modules with longer names." |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "code", |
| 56 | + "execution_count": null, |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [], |
| 59 | + "source": [] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": null, |
| 64 | + "metadata": {}, |
| 65 | + "outputs": [], |
| 66 | + "source": [] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": null, |
| 71 | + "metadata": {}, |
| 72 | + "outputs": [], |
| 73 | + "source": [] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "markdown", |
| 77 | + "metadata": {}, |
| 78 | + "source": [ |
| 79 | + "In this example we now see that when the math module is imported, it is imported to be usable with the name `m` instead of `math`.\n", |
| 80 | + "It doesn't matter much in our toy example here since math is not a long module name, but we will see other examples later in the course where renaming the modules is very helpful (e.g., matplotlib).\n", |
| 81 | + "\n", |
| 82 | + "### Importing a single function\n", |
| 83 | + "\n", |
| 84 | + "It is also possible to import only a single function from a module, rather than the entire module.\n", |
| 85 | + "This is sometimes useful when using large modules that have much more available than the desired use." |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": null, |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [], |
| 93 | + "source": [] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "code", |
| 97 | + "execution_count": null, |
| 98 | + "metadata": {}, |
| 99 | + "outputs": [], |
| 100 | + "source": [] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "markdown", |
| 104 | + "metadata": {}, |
| 105 | + "source": [ |
| 106 | + "Though this can be useful, it has the drawback that the imported function could conflict with other built-in or imported function names, and you lose the information about which module contains the function.\n", |
| 107 | + "You should only do this when you truly need to.\n", |
| 108 | + "\n", |
| 109 | + "### Importing part of a module\n", |
| 110 | + "\n", |
| 111 | + "Some modules have sub-modules that can also be imported without importing the entire module.\n", |
| 112 | + "We may see examples of this later when making data plots using the pyplot sub-module of the [Matplotlib module](http://matplotlib.org/).\n", |
| 113 | + "In case you're curious, here is an example." |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "code", |
| 118 | + "execution_count": null, |
| 119 | + "metadata": {}, |
| 120 | + "outputs": [], |
| 121 | + "source": [] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "code", |
| 125 | + "execution_count": null, |
| 126 | + "metadata": {}, |
| 127 | + "outputs": [], |
| 128 | + "source": [] |
| 129 | + }, |
| 130 | + { |
| 131 | + "cell_type": "markdown", |
| 132 | + "metadata": {}, |
| 133 | + "source": [ |
| 134 | + "This creates a new figure window for a pyplot figure.\n", |
| 135 | + "Again, we'll see how this works and what it means later in the course." |
| 136 | + ] |
| 137 | + }, |
| 138 | + { |
| 139 | + "cell_type": "markdown", |
| 140 | + "metadata": {}, |
| 141 | + "source": [ |
| 142 | + "## How can modules be used?\n", |
| 143 | + "\n", |
| 144 | + "As we see above, the easiest way to use a module is to import it an then use its functions by typing `modulename.functionname()` and providing the necessary arguments.\n", |
| 145 | + "Yes, it is that simple.\n", |
| 146 | + "\n", |
| 147 | + "However, there are times you may not know the names of all of the functions in a given module, or which are part of a module.\n", |
| 148 | + "You can view the list of functions that are part of a module by using the `dir()` function." |
| 149 | + ] |
| 150 | + }, |
| 151 | + { |
| 152 | + "cell_type": "code", |
| 153 | + "execution_count": null, |
| 154 | + "metadata": {}, |
| 155 | + "outputs": [], |
| 156 | + "source": [] |
| 157 | + }, |
| 158 | + { |
| 159 | + "cell_type": "markdown", |
| 160 | + "metadata": {}, |
| 161 | + "source": [ |
| 162 | + "So that's helpful, but what about when you don't know what a given function does?\n", |
| 163 | + "The easiest solution is to use the `help()` function." |
| 164 | + ] |
| 165 | + }, |
| 166 | + { |
| 167 | + "cell_type": "code", |
| 168 | + "execution_count": null, |
| 169 | + "metadata": {}, |
| 170 | + "outputs": [], |
| 171 | + "source": [] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "markdown", |
| 175 | + "metadata": {}, |
| 176 | + "source": [ |
| 177 | + "Note that you'll need to press `q` to exit the help viewer." |
| 178 | + ] |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "markdown", |
| 182 | + "metadata": {}, |
| 183 | + "source": [ |
| 184 | + "## What should I not do?\n", |
| 185 | + "\n", |
| 186 | + "Here are a few things to avoid.\n", |
| 187 | + "\n", |
| 188 | + "### from X import *\n", |
| 189 | + "\n", |
| 190 | + "Don't use `from X import *`.\n", |
| 191 | + "This may be easier to understand by way of an example, but assuming `X` above is a Python module, `from X import *` will import all of the functions in module X.\n", |
| 192 | + "Though you might think this is helpful, it is much better to simply `import X` or `import X as Y` to keep the connection between the functions and their module.\n", |
| 193 | + "It is also much more likely you will encounter conflicting names when using `from X import *`.\n", |
| 194 | + "\n", |
| 195 | + "### Poor names when renaming on import\n", |
| 196 | + "\n", |
| 197 | + "Don't use confusing names when renaming on import.\n", |
| 198 | + "Be smart when you import modules.\n", |
| 199 | + "If you want to make the module name shorter on import, pick a reasonable abbreviation.\n", |
| 200 | + "For instance, `import matplotlib as m` could be confusing, especially if you're also using `import math as m` in other Jupyter notebooks or script files.\n", |
| 201 | + "Similarly, `import matplotlib as math` is perfectly OK syntax in Python, but bound to cause a world of trouble.\n", |
| 202 | + "Remember, people need to be able to read and understand the code you write, keep it simple and logical." |
| 203 | + ] |
| 204 | + } |
| 205 | + ], |
| 206 | + "metadata": { |
| 207 | + "kernelspec": { |
| 208 | + "display_name": "Python 3", |
| 209 | + "language": "python", |
| 210 | + "name": "python3" |
| 211 | + }, |
| 212 | + "language_info": { |
| 213 | + "codemirror_mode": { |
| 214 | + "name": "ipython", |
| 215 | + "version": 3 |
| 216 | + }, |
| 217 | + "file_extension": ".py", |
| 218 | + "mimetype": "text/x-python", |
| 219 | + "name": "python", |
| 220 | + "nbconvert_exporter": "python", |
| 221 | + "pygments_lexer": "ipython3", |
| 222 | + "version": "3.6.4" |
| 223 | + } |
| 224 | + }, |
| 225 | + "nbformat": 4, |
| 226 | + "nbformat_minor": 2 |
| 227 | +} |
0 commit comments