Page 1 of 1

Modules in Python

#1 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 143
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Posted 23 January 2009 - 06:06 AM

*
POPULAR

What is module?

By defining functions you can reuse a block of code several times in a program. But, if you want to reuse these defined functions in another programs, then you need to do something more, need to make a module. A module is a file containig all of your defined functions. The file name of a module should have “.py” extension. A module is imported by another program to make use of its functionality.

Make your first module
You can easily create your own module. So, let’s create our first module-

def sayHello():
	print “Hello!This is my first module.”
version=’1.0’



Save it as mymodule.py .

As you see, there is nothing special here than a simple python program. Now we will use it in another program.

Import a module

The functionality of a module is used by importing it in another program. There are two ways to import a module-
1.import moduleName
2.from...import
Take a look at the following two examples to clarify the difference between them-

Example:1
import mymodule
mymodule.sayHello()
print mymodule.version



Example:2
from mymodule import *
sayHello()
print verion



Both cases results the same output-
Hello!This is my first module.
1.0

Note: from...import statement is used to import functions, variables and classes directly from a module. If you wish to import a specific function/variable then use “from moduleName import functionName”. If you want to import all the functions, variables and classes directly from a module, then use “from moduleName import *”.

The Module Search Path

So far we have seen how to make your own module and how to import it. Now we will learn something about module search path. Module search path simply means which paths are being searched when a module is imported. Let’s go back to our first module named mymodule. When this module is imported, the interpreter searches for a file named mymodule.py in the current directory. Then it searches in the list of directories specified by the environment variable ‘PYTHONPATH’. If environment variable is not set, then interpreter searches in the installation-dependent default path. On windows operating systems, it is generally ‘C:\Python30’ or on linux distributions it is usually ‘/usr/local/bin/python’.

Compiled .pyc files

When a module is imported, that is the corresponding file containing the module is successfully compiled, a compiled version of this file is created with the extension ‘.pyc’. Next time when you import the module, even from a different program, it will be much faster due to this byte compiled file. This byte-compiled files are platform-independent.

A module's __name__

Built-in variable __name__ is set with the start of a program. When the program runs as a
script, __name__ has the value __main__ and the test block is executed. On the other hand, if the module is being imported, the test code is skipped.

Example: Using a module's __name__
if __name__ == '__main__':
print 'This program is running by itself!'

Output:
This program is running by itself!


The dir() function
The dir() function is used to find out which functions, variables and classes are defined. When you supply a module name to the dir() function, it will return the list of the names defined in that module.
When no argument is supplied to it, it returns the list of names defined in the current module. See what happpens when we supply the module name sys to the dir() function-

>>>import sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'intern', 'last_traceback', 'last_type', 'last_value', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'setcheckinterval', 'setfilesystemencoding', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']



Is This A Good Question/Topic? 5
  • +

Replies To: Modules in Python

#2 numberwhun  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 87
  • Joined: 28-March 08

Posted 13 June 2010 - 08:14 PM

@atik97 Thanks! This is an excellent tutorial.
Was This Post Helpful? 0
  • +
  • -

#3 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

Posted 13 January 2011 - 09:05 PM

Thanks. I modified the code some to make it compatible with Python 3.0 though.
Was This Post Helpful? 0
  • +
  • -

#4 Mossypne  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 133
  • Joined: 18-August 10

Posted 15 April 2011 - 07:17 AM

Where abouts would you save the module???
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1