Navigation Menu

Search code, repositories, users, issues, pull requests..., provide feedback.

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly.

To see all available qualifiers, see our documentation .

  • Notifications You must be signed in to change notification settings

Latest commit

File metadata and controls.

Scientific Programming - Home

  • Suggest edit

Modules, import mechanism, namespaces, scope(s)

Modules, import mechanism, namespaces, scope(s) #.

In this chapter we will dive into one of the most important features of python: the import mechanism. We will explain why modules are useful, how to “import” them, and how you can write one yourself.

Variable scopes #

The scope of a variable is the region of the program where the variable is valid, i.e. the “location” where the variable name can be used to refer to the data it links to. Let’s consider this first example:

It doesn’t work because the name i is defined in the function foo , which we defined but never called (you can see this because nothing is printed), so the function’s statements were actually never run.

Let’s see if the following example works better:

Here, the function is called and i is defined and equal to 5. However, the scope of the variable i is the block defined by the function foo : outside of this scope the variable doesn’t exist anymore, leading to the NameError at the global scope.

If this is understood, the following example might be clear as well?

The global scope refers to the highest scope level (the module or, in interactive mode, the interpreter). The function’s scope in turn is called local scope . One says that the global scope prevails because what is possible in one direction isn’t possible in the other:

Yes, k is available in foo() : global variables are also available in the nested local scopes. They can be overridden locally (like in the example above), but this change won’t be visible at the global scope level.

Python modules are used to store a collection of functions and variables that can be imported into other modules or into the main environment .

A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.

For instance, I used my favorite text editor to create a file called greetings.py with the following content:

greetings.py:

I copied this file in the current working directory (this “detail” is important, as we will see below). From the (i)python interpreter I can now import this module with the following command:

By importing the module, I have access to the functions it contains:

But also to the global variables defined at the top level:

Exercise 10

Repeat the steps above on your laptop and do additional experiments / ask questions until these variable scope mechanisms are well understood.

Namespaces #

Global variables in modules or scripts are useful, but they should be used with care . Indeed, since they are available from everywhere in the module, the namespace can quickly become “polluted”: for large modules, it becomes hard to keep track of all variables. This can become even more complicated when new modules with their own new variables and functions are used as well.

Fortunately, the global scope is constrained to the current script or module, and using external modules is unlikely to cause confusion thanks to the import mechanism:

By importing the math module (available in python’s standard library ), we have access to a new variable called math . The variable math has the type module , and provides new functions and variables (such as math.pi ). What are all these functions and variables? Let’s use the built-in dir() function to find out:

Exercise 11

Ask the ipython documentation about dir (remember how?)

Like our greetings module, the math module contains functions (e.g. math.radians ) as well as variables (e.g. math.nan or math.pi ). For Python there is no fundamental difference between variables and functions: they are simply all attributes of the math module and therefore listed by dir (we’ll get back to attributes later: for now, remember the dot . syntax, dir and TAB autocompletion). Some of the attributes listed by dir have a prefix of two underscores __ . We’ll get back to them soon.

There are several ways to import a module. See these four examples:

All four options lead to the same result: they compute the sinus of π/2.

Now, which one to use? It is up to you, but there are some implicit rules (conventions) which are widely accepted:

In case of doubt, use option A. It is the most readable and the most explicit .

The exception to the first rule is when the library has a naming convention for its acronym. A good example is numpy , which recommends to use import numpy as np .

Option C might be useful if the names you are importing are very explicit, and if you expect to use them often in your script. Otherwise, it is not recommended.

Option D is bad. Don’t use it.

You can make an exception to rule 4 when working in the command line and exploring data. Never use option 4 in scripts.

Exercise 12

Try to find arguments why option D is a bad idea. If you can’t find any reason, ask me !

But how does python know where to look for modules when you type import mymodule in the interpreter? Well, it relies on a mechanism very similar to linux’s PATH environment variable. Remember this one? Within python, the directories where the interpreter will look for modules to import are listed in sys.path :

Similar to the linux PATH mechanism, python will look into each of these directories in order . The first directory is always the current working directory (an empty string), the rest of the list may vary depending on your environment. When a file called mymodule.py is found, it is imported once (and only once) and added to the sys.modules variable, effectively telling python that the module has already been imported. This means that if you change the file and import it again, nothing will change . Fortunately, there are ways to avoid this behavior (see below).

As you can see, there are many folders related to miniconda , the tool we used to install python. This makes sense, because we want python to look for modules related to our installation of python (not the one used by our operating system). In particular, the site-packages folder is of interest to us. If you look into this folder using the linux command line or ipython’s magic commands (remember how?) you’ll find the many packages we already installed together last week.

You can edit sys.path at your wish and add new folders to it, exactly as you would with the PATH environment variable in linux. In practice, however, it is recommended to use standard folders to install your packages instead of messing around with sys.path . I personally never had to change sys.path in my scripts, but it can come handy if you want to use a collection of modules that you have not “packaged” yet: in this case, you can put all modules in a folder and add it to sys.path with:

But this will add the folder to the current session or script only. To add a folder permanently, you can use the PYTHONPATH environment variable.

Exercise 13

Print your own sys.path on your laptop and explore the folders listed there.

Now let’s play with sys.path a little. Create a fake module called matplotlib.py in your current working directory. Open a python interpreter and import matplotlib . Which of the modules (the official or the fake one) is loaded by python, and why?

Because of this “feature”, it is important to find meaningful (and unique) names for your own modules (don’t forget to delete the fake matplotlib module after trying that out!).

Note: the “matplotlib trick” above does not work with modules of the standard library, because built-in modules are imported as part of the interpreter’s startup process.

Reimporting modules #

Of course, there is a lot more behind an import statement than this short summary (see the documentation for a more comprehensive overview). What you should remember however is that an import statement is not computationally cheap: it has to look for a file among many folders, compile the python code to bytecode, add the module to a registry… Actually, for all the things it does, it is quite fast already!

Since import statements are used all the time in Python, it is a good idea to optimize them: therefore, Python will not re-import a module that is imported already.

Most of the time this is not a problem, but sometimes (mostly in interactive development) it might be useful to make changes to a module and having them available at the command line without having to restart a fresh Python interpreter each time. To do this, there are two mechanisms available to you:

Using Python’s importlib.reload() function:

This can reimport a module which has been imported before (i.e. it won’t search for it again). But this has to be done each time you change a module, making it quite cumbersome in interactive mode. Here again, ipython comes with a more flexible solution:

Using ipython’s autoreload extension:

From now on, autoreload will make sure that all modules are reloaded automatically before entering the execution of code typed at the ipython prompt.

Executing modules as scripts #

You’ve already learned that importing modules actually runs them, the same way as $ python mymodule.py (from the linux terminal) or %run mymodule.py (in ipython) would. This can have some undesired consequences: what if, for example, you would like certain things to happen when the module is run as script but not when being imported?

There is a mechanism that allows to do exactly this. Add the following block of code at the end of the greetings.py module:

Now you can execute your script like this:

But importing it will produce no output:

How does this work exactly? The important bit is the if __name__ == '__main__': line. __name__ (and all other attributes starting and ending with two underscores) is “reserved” by the language. They are no different than other variables, but you should not erase or replace them since they contain useful information and might be used by other tools. For example:

Now, back to our script: the statement if __name__ == '__main__' is going to be false when the module is imported (since the name is greetings ), but obviously true when the module is run as a script. Indeed, __name__ is set to __main__ when executed.

Take home points #

variables are defined in a scope , and we learned about two scopes: local and global . There are precise rules as to how each scope can interact with the other. As a general rule: global variables are available in nested scopes and cannot be overwritten, and local variables are not available in the global scope.

the import mechanism is useful to avoid namespace pollution . Modules come with their own namespace, and it is recommended to keep each module’s namespace clean (i.e. no from XYZ import * )

python borrowed the PATH mechanism from linux and uses a similar logic to look for modules and packages. Installing a new module is therefore super easy: just put a mymodule.py at the right place. (and don’t mess with the existing modules!)

the %autoreload command from ipython allows to dynamically change a module while using it at the command line

when writing scripts, always put the code that needs to be executed in an if __name__ == '__main__' block: that way, your script can still be imported for the functions it proposes