{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial 1: Basics about Jupyter Notebooks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You only need to look at this tutorial if you are new to Jupyter Notebooks.\n", "\n", "This page (and its file, `01_notebook_basics.ipynb`) is termed a *notebook*. Each notebook when opened in Jupyter, is a Python main module that can do anything a normal Python module can do. Specifically, different code sections can be executed one by one and execute all in the same Python main module.\n", "\n", "If you view this notebook using the Online Notebook Viewer (e.g. because you clicked on the link in section [Tutorials](https://python-zhmcclient.readthedocs.io/en/latest/tutorial.html#tutorials)), you cannot execute the code sections in the notebook or modify the notebook. Section [Executing code in the tutorials](https://python-zhmcclient.readthedocs.io/en/latest/tutorial.html#executing-code-in-the-tutorials) describes how to do that.\n", "\n", "A code section can be executed by selecting it and pressing Ctrl-Enter (That is for Linux; the key combination depends on the operating system you are using).\n", "\n", "Please execute this code section:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And this code section:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The second code section prints the value only if you have executed the first code section before. You can see the order of code section executions in the number that is shown in brackets to the left of the code section.\n", "\n", "You can repeat the execution of a code section that was executed earlier and you can edit code sections (by double clicking on it).\n", "\n", "For example, you can edit the first code section above to use a different value, and execute both sections again." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Python environment used by Jupyter for a notebook is exactly the Python environment in which the `jupyter notebook` command was issued. It has the same Python version and the same Python packages installed:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import sys\n", "print(sys.version)\n", "print(sys.modules['six'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Jupyter dynamically creates a Python module for each notebook that is open. This notebook here for example runs in the following Python module:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "this_module = sys.modules[__name__]\n", "print(this_module)\n", "print('__name__ = %r' % this_module.__name__)\n", "print('__doc__ = %r' % this_module.__doc__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Different notebooks run in different Python processes and thus have no connection between their modules." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The current working directory in a notebook is the notebooks directory (that was specified when invoking `jupyter notebook`):" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os\n", "print(os.getcwd())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Python module search path within a notebook adds some Jupyter directories at the end but otherwise is the normal search path that would be available when invoking `python`, i.e. it reflects the currently active Python environment. Specifically, it includes the current directory (the empty string), i.e. the notebooks directory." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import sys\n", "from pprint import pprint\n", "pprint(sys.path)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }