{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Modules and Packages\n", "\n", "There's no code here because it didn't really make sense for the section. Check out the video lectures for more info and the resources for this.\n", "\n", "Here is the best source the official docs!\n", "https://docs.python.org/2/tutorial/modules.html#packages\n", "\n", "But I really like the info here: https://python4astronomers.github.io/installation/packages.html\n", "\n", "Here's some extra info to help:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Modules in Python are simply Python files with the .py extension, which implement a set of functions. Modules are imported from other modules using the import command.\n", "\n", "To import a module, we use the import command. Check out the full list of built-in modules in the Python standard library here.\n", "\n", "The first time a module is loaded into a running Python script, it is initialized by executing the code in the module once. If another module in your code imports the same module again, it will not be loaded twice but once only - so local variables inside the module act as a \"singleton\" - they are initialized only once.\n", "\n", "If we want to import module math, we simply import the module:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# import the library\n", "import math" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# use it (ceiling rounding)\n", "math.ceil(2.4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exploring built-in modules\n", "Two very important functions come in handy when exploring modules in Python - the dir and help functions.\n", "\n", "We can look for which functions are implemented in each module by using the dir function:\n", "\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']\n" ] } ], "source": [ "print(dir(math))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When we find the function in the module we want to use, we can read about it more using the help function, inside the Python interpreter:\n", "\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function ceil in module math:\n", "\n", "ceil(...)\n", " ceil(x)\n", " \n", " Return the ceiling of x as an int.\n", " This is the smallest integral value >= x.\n", "\n" ] } ], "source": [ "help(math.ceil)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Writing modules\n", "Writing Python modules is very simple. To create a module of your own, simply create a new .py file with the module name, and then import it using the Python file name (without the .py extension) using the import command.\n", "\n", "## Writing packages\n", "Packages are name-spaces which contain multiple packages and modules themselves. They are simply directories, but with a twist.\n", "\n", "Each package in Python is a directory which MUST contain a special file called **\\__init\\__.py**. This file can be empty, and it indicates that the directory it contains is a Python package, so it can be imported the same way a module can be imported.\n", "\n", "If we create a directory called foo, which marks the package name, we can then create a module inside that package called bar. We also must not forget to add the **\\__init\\__.py** file inside the foo directory.\n", "\n", "To use the module bar, we can import it in two ways:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Just an example, this won't work\n", "import foo.bar" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# OR could do it this way\n", "from foo import bar" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the first method, we must use the foo prefix whenever we access the module bar. In the second method, we don't, because we import the module to our module's name-space.\n", "\n", "The **\\__init\\__.py** file can also decide which modules the package exports as the API, while keeping other modules internal, by overriding the **\\__all\\__** variable, like so:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "__init__.py:\n", "\n", "__all__ = [\"bar\"]" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }