Python – import and reload modules

This post is the third of many that attempts to document everything I have been learning about Python.

Some ways to execute a Python program:

  • From the command line you can invoke the interpreter using

python source.py

  • Double clicking the file icon in windows. This assumes that the .py file extension is associated with the Python runtime in the windows registry.
  • Inserting this string in the first line of your .py source file will turn the file into a executable python script (on *nix), the second line turns the script into an executable.

#! /usr/bin/python

chmod +x sourcefile.py

By launching an interactive session using either the system shell or via IDLE and using import to load the module.

>>> import math
>>> math.sqrt(144)
12.0

When you load a module using a import statement, the python interpreter executes all the code in that module.

If you change the source and import the same file again in the same session, you will not see your changes reflected. This is because import is a expensive operation to run multiple times. Every time you import a module, the module code has to be compiled and then executed.

# sourcefile module
# Function accepts 2 parameters and displays them.

def printname(first, last):
       print(first, last)

Import the sourcefile module, importing it will execute the code in the module.

>>> import sourcefile
>>> sourcefile.printname(‘Rakshit‘, ‘Pai‘)
>>> Rakshit Pai

If you have made changes to the code of a module after you have imported it, you can reload it using using the reload function of the imp module.

# sourcefile module
# Modify function to accept 3 parameters and display them.

def printname(first, last, middle):
       print(first, last, middle)

>>> import imp

>>> imp.reload(sourcefile)

>>> sourcefile.printname(‘Rakshit‘, ‘Pai‘, ‘K‘)
Rakshit Pai K

The reload function of the imp module will only reload a module that was previously loaded via a import call in the session. When you load modules using import, the module name acts as a namespace through which the attributes in the imported modules can be accessed, it acts as a buffer that helps you avoid naming clashes if variables with the same name are already declared in the importing environment.

reload only loads the module that you have passed in as a parameter. Any modules being imported in the module being loaded have to be re-loaded separately.

For example, when you import the module sourcefile, you can access the attributes of sourcefile by qualifying their names with the namespace of their enclosing module (sourcefile)

# sourcefile is the module name.
# it has a function attribute named printname

>>> sourcefile.printname(‘Rakshit‘, ‘Pai‘, ‘K‘)

When you import modules using the from… import… syntax, its attributes are copied over in the environment you imported the module into, this means that any previously declared variables that have the same name in the loading environment will now be overwritten by the variables in the module

>>> # declare a list named lst in the current session.
>>>
>>> lst = [1, 2, 3]
>>> lst
[1, 2, 3]

>>> # module named sourcefile has a lst list variable

>>> lst = [‘hello‘, ‘world‘]

using from… import… syntax import the lst variable from the sourcefile module.

>>> # Declare a variable lst in the current session
>>> lst = [1, 2, 3]
>>> lst
[1, 2, 3]
>>>
>>> from sourcefile import lst
>>> lst
[‘hello‘, ‘world‘]
>>>
>>> # The original lst variable declared earlier in the session
>>> # has now been replaced with the variable in the imported module

You can also use the built in exec function to load a module. Using exec is similar to pasting the modules code and running it. But as with from.. import… this will override the values of any variables that have the same names as variables in the working environment.

>>> # This will open the sourcefile.py file
>>> # read its contents,
>>> # execute the contents as Python code
>>>
>>> exec(open(‘sourcefile.py‘).read())
>>>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s