Skip to content

Instantly share code, notes, and snippets.

@alexwhittemore
Last active August 27, 2018 20:03
Show Gist options
  • Save alexwhittemore/fb5913c8146ab441e795a13f9ec8d7ea to your computer and use it in GitHub Desktop.
Save alexwhittemore/fb5913c8146ab441e795a13f9ec8d7ea to your computer and use it in GitHub Desktop.
A simple tutorial using a Jupyter notebook
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Using Python packages\n",
"## A super-simple how-to, illustrated with a Jupyter notebook\n",
"\n",
"In order to make use of a Python package we have installed, we must first \"import\" it."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.7.15 (default, Jul 23 2018, 21:27:06) \n",
"[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)]\n"
]
}
],
"source": [
"import sys\n",
"print sys.version"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we don't import it, it simply doesn't exist, and we'll get a NameError trying to reference it."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'numpy' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-2-aa642aae9327>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfoo\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'numpy' is not defined"
]
}
],
"source": [
"foo = numpy.array([1, 2, 3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"But if we import numpy first, everything works just dandy."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3]\n"
]
}
],
"source": [
"import numpy\n",
"foo = numpy.array([1, 2, 3])\n",
"print foo"
]
}
],
"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.15"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment