A module is something like a package. We can import it into our code.
Or we can import functions from the module into our code.
For example, you can import the function sqrt
from the module math
:
from math import sqrt
print(sqrt(2))
You can also import a whole module. You can access a module's
functions through a period - the same way as you access string methods
('Hello'.upper()
).
For example:
import turtle
turtle.left(90)
turtle.color('red')
turtle.forward(100)
turtle.exitonclick()
import math
print(math.cos(math.pi))
We don't want asterisks
In documentation or in another course, you have maybe seen
an import with an asterisk (*
).
In this course, we won't use it, we always import the whole module instead.
When you write more difficult programs in the future, this will make
your work easier.
You can also create your own module simply by creating a Python file. Functions and variables that you create there will be available in programs where you import this module.
Try it!
Create file meadow.py
and write:
meadow_colour = 'green'
number_of_kitties = 28
def description():
return 'The meadow is {colour}. There are {number} kitties.'.format(
colour=meadow_colour, number=number_of_kitties)
And then write in another file (write.py
):
import meadow
print(meadow.description())
and run:
$ python write.py
The command import
looks for files in the same folder
of the file where you imported the module - in our
case write.py
. So place both of the files into the
same folder.
What exactly does the command import meadow
do?
Python will look for a matching file (meadow.py
) and run all the commands
there from top to bottom, like it was a normal program.
It will give all the global variables (including defined functions) to the
program that imported that module.
When you import the same module a second time, it doesn't run everything again - it will just use what it already has.
Try it - write in the end of meadow.py
:
print('The meadow is green!')
And then run python
in the command line (if you already have an interactive
Python open, close it, and run again) and enter:
>>> print('First import:')
>>> import meadow
>>> print('Second import:')
>>> import meadow
The print we wrote in the end of the module file will appear only once.
When the module is "doing something" (it prints something, asks the user,
writes something into a file) - we say that it has a side effect.
We try to avoid writing modules that have side effects:
because the purpose of a module is to give us functions, that we
will use to do something, not to do it instead of us.
For example, when we write import turtle
, no window opens. It opens
only when we write turtle.forward()
.
So you better delete print
from our module.
From now on, we will work on bigger projects that contain more files. We recommend that you create a folder for each of them.
You can find more info about import and modules here.
{ "data": { "sessionMaterial": { "id": "session-material:2018/pyladies-en-prague:tests:1", "title": "Modules", "html": "\n \n \n\n <h1>Modules</h1>\n<p>A module is something like a package. We can import it into our code.\nOr we can import functions from the module into our code.\nFor example, you can import the function <code>sqrt</code> from the module <code>math</code>:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"kn\">from</span> <span class=\"nn\">math</span> <span class=\"kn\">import</span> <span class=\"n\">sqrt</span>\n\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">sqrt</span><span class=\"p\">(</span><span class=\"mi\">2</span><span class=\"p\">))</span>\n</pre></div><p>You can also import a whole module. You can access a module's\nfunctions through a period - the same way as you access string methods\n(<code>'Hello'.upper()</code>).</p>\n<p>For example:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"kn\">import</span> <span class=\"nn\">turtle</span>\n\n<span class=\"n\">turtle</span><span class=\"o\">.</span><span class=\"n\">left</span><span class=\"p\">(</span><span class=\"mi\">90</span><span class=\"p\">)</span>\n<span class=\"n\">turtle</span><span class=\"o\">.</span><span class=\"n\">color</span><span class=\"p\">(</span><span class=\"s1\">'red'</span><span class=\"p\">)</span>\n<span class=\"n\">turtle</span><span class=\"o\">.</span><span class=\"n\">forward</span><span class=\"p\">(</span><span class=\"mi\">100</span><span class=\"p\">)</span>\n<span class=\"n\">turtle</span><span class=\"o\">.</span><span class=\"n\">exitonclick</span><span class=\"p\">()</span>\n</pre></div><div class=\"highlight\"><pre><span></span><span class=\"kn\">import</span> <span class=\"nn\">math</span>\n\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">math</span><span class=\"o\">.</span><span class=\"n\">cos</span><span class=\"p\">(</span><span class=\"n\">math</span><span class=\"o\">.</span><span class=\"n\">pi</span><span class=\"p\">))</span>\n</pre></div><div class=\"admonition note\"><p class=\"admonition-title\">We don't want asterisks</p>\n<p>In documentation or in another course, you have maybe seen \nan import with an asterisk (<code>*</code>).\nIn this course, we won't use it, we always import the whole module instead.\nWhen you write more difficult programs in the future, this will make\nyour work easier.</p>\n</div><h2>Custom modules</h2>\n<p>You can also create your own module simply by\ncreating a Python file. Functions and variables\nthat you create there will be available\nin programs where you import this module.</p>\n<p>Try it!\nCreate file <code>meadow.py</code> and write:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"n\">meadow_colour</span> <span class=\"o\">=</span> <span class=\"s1\">'green'</span>\n<span class=\"n\">number_of_kitties</span> <span class=\"o\">=</span> <span class=\"mi\">28</span>\n\n<span class=\"k\">def</span> <span class=\"nf\">description</span><span class=\"p\">():</span>\n <span class=\"k\">return</span> <span class=\"s1\">'The meadow is {colour}. There are {number} kitties.'</span><span class=\"o\">.</span><span class=\"n\">format</span><span class=\"p\">(</span>\n <span class=\"n\">colour</span><span class=\"o\">=</span><span class=\"n\">meadow_colour</span><span class=\"p\">,</span> <span class=\"n\">number</span><span class=\"o\">=</span><span class=\"n\">number_of_kitties</span><span class=\"p\">)</span>\n</pre></div><p>And then write in another file (<code>write.py</code>):</p>\n<div class=\"highlight\"><pre><span></span><span class=\"kn\">import</span> <span class=\"nn\">meadow</span>\n\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">meadow</span><span class=\"o\">.</span><span class=\"n\">description</span><span class=\"p\">())</span>\n</pre></div><p>and run:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"gp\">$ </span>python write.py\n</pre></div><p>The command <code>import</code> looks for files in the same folder\nof the file where you imported the module - in our\ncase <code>write.py</code>. So place both of the files into the \nsame folder.</p>\n<h2>Side effects</h2>\n<p>What exactly does the command <code>import meadow</code> do?</p>\n<p>Python will look for a matching file (<code>meadow.py</code>) and run all the commands\nthere from top to bottom, like it was a normal program.\nIt will give all the global variables (including defined functions) to the\nprogram that imported that module.</p>\n<p>When you import the same module a second time, it doesn't\nrun everything again - it will just use what it already has.</p>\n<p>Try it - write in the end of <code>meadow.py</code>:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'The meadow is green!'</span><span class=\"p\">)</span>\n</pre></div><p>And then run <code>python</code> in the command line (if you already have an interactive\nPython open, close it, and run again) and enter:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"gp\">>>> </span><span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'First import:'</span><span class=\"p\">)</span>\n<span class=\"gp\">>>> </span><span class=\"kn\">import</span> <span class=\"nn\">meadow</span>\n<span class=\"gp\">>>> </span><span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'Second import:'</span><span class=\"p\">)</span>\n<span class=\"gp\">>>> </span><span class=\"kn\">import</span> <span class=\"nn\">meadow</span>\n</pre></div><p>The print we wrote in the end of the module file\nwill appear only once.</p>\n<p>When the module is "doing something" (it prints something, asks the user, \nwrites something into a file) - we say that it has a <em>side effect</em>.\nWe try to avoid writing modules that have side effects:\nbecause the purpose of a module is to give us <em>functions</em>, that we\nwill use to do something, not to do it instead of us.\nFor example, when we write <code>import turtle</code>, no window opens. It opens\nonly when we write <code>turtle.forward()</code>.</p>\n<p>So you better delete <code>print</code> from our module.</p>\n<h2>One directory for every project</h2>\n<p>From now on, we will work on bigger projects that contain\nmore files. We recommend that you create a folder for each\nof them.</p>\n<p>You can find more info about <a href=\"https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html#basics-of-the-python-import-and-syspath\">import and modules here</a>.</p>\n\n\n " } } }