A few things on Python importing

Coding Journal
3 min readJun 25, 2017

--

But first, a little bit about myself. I love coding and have been learning how to code for awhile. Writing about technical topics is a great way to develop deeper understanding. That’s why I started this journal.

Have you ever wondered what is the difference between from … import … and import … ? Using one or the other affects how you should reference the imported modules/functions later in your code.

Before we dive into the differences, let’s look at what is happening under the hood when you include the import statement in your Python file.

Import, what exactly am I importing?

You are importing Python modules, which are basically Python files containing Python definitions (e.g. Python functions and classes definitions) and/or executable statements (e.g. statements to execute/call a Python function). (See more: link)

You could totally put all Python functions and declaration in the same file, but breaking them down into different modules greatly enhances code reusability and maintainability.

Hows does Python know where the modules are?

When you try to import a module, Python would go and search for that module in the directory defined in sys.path. It is literally directories stored in a Python list.

You can append to the list using sys.path.insert function (more details: link). After you have appended the directory to the list, Python would then know to also search in that new directory when looking for modules.

If you closes out of Python, this change would be reverted. See this link for how to permanently append a directory to Python.

Importing and symbol tables

You can think of symbol tables as a lookup table for your program to find definition of functions, variables, etc, using their names. When you import another module, Python searches for that module in the list of directories it knows about (as discussed in the previous paragraph) and add to the symbol table of your importing module.

import … vs. from … import …

Ok, finally we get to the meat of this article, the difference between import … and from…import …

import …

When you use the import… statement, the imported module name is being added to the current symbol table of the importing module. The names of the functions inside your imported module is not in the current symbol table at all.

Therefore, when you need to call a function within that module, in your code, you need to reference it first by module name, and then function name. It is because only the name of the imported module is in the global symbol table of your importing module.

from…import …

However, when you use from…import …, you are adding the names of functions/declarations in the imported module directly to the current symbol table of your importing module.

Therefore, you just need to reference the names of functions/declarations directly without its module name. In fact, the module name would not even be defined in the current symbol table of your importing module at all.

A simple example

I have a my_calculations module (my_calculations.py) that simply has one function summing two numbers up.

Below are the two ways I would reference it using the two different import syntaxes.

References:

[1] https://docs.python.org/2.1/ref/import.html

[2] https://www.codementor.io/sheena/python-path-virtualenv-import-for-beginners-du107r3o1

[3] https://docs.python.org/2/tutorial/modules.html

[4] http://www.diveintopython3.net/your-first-python-program.html

[5] https://en.wikipedia.org/wiki/Symbol_table

--

--

Coding Journal

A student of software engineering with a passion to share her learnings.