Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Learning python part 5 (modules)

hello world

  • Please log in to reply
1 reply to this topic

#1 saeras


    CC Resident

  • Just Joined
  • PipPipPipPip
  • 64 posts

Posted 18 February 2010 - 05:42 AM

Modules are a fantastic and easy tool to start using. They speed up the coding progress considerably if you're able to find the right modules. An example of this is a module i've been using alot recently, Pygame. What this module allows you to do is easy 2d game making. These modules are made to ease programming in python.
Before i go any further let's see a little example:

import module3


This code first imports module3 and then uses function() from module3. Now let's make module3.

def function():
    print('hello world')

This is then saved as "module3.py" in the same folder as the above program. So looking at module3 we can see that the above program will print out 'hello world'. Now whenever you want module3.py in a program you can just place it in the same folder and import it. So say you have something you do often and don't want to recode all the time, Then you can simply make a module for it. You can also put modules in sub folders. This is done in this manner.

1. Create the subfolder
2. Create a "__init__.py" file in the subfolder, this is made so that python will "find" the subfolder.
3. Then you simply import it in this manner "from 'subfoldername' import 'module name'

This is good if you want to create more structure in larger applications.

So you're all set to start using modules now. Where do i get some then?
Well here are a few places you can look at:
builtin modules (in 2.6.4) (this is the standard library included when installing python 2.6.4)
A pretty neat list of various useful modules.
Here are some on pythons main site.

Most modules around are pretty well documented and learning them should be an easy task compared to making it yourself. Also most modules that you install will put them self in the python folder. These can be imported by a simple import 'module' so don't worry about moving them from folder to folder. So if you make a program with a special module dependency don't forget to include for example "this program uses pygame version x.x.x" somewhere.

All this should more than get you going on modules in python. And as usually don't be afraid to play around with them. Unless you have a really ominous module on your hands the worst that could happen is an error message while parsing.

Feel anything have been left out? Had trouble with something? Or want something changed?
Post a comment about it and I will see what I can do.
  • 1
Posted Image

#2 James.H


    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 569 posts

Posted 19 February 2010 - 04:21 AM

Nice easy to follow tutorial. +rep!
  • 0

Also tagged with one or more of these keywords: hello world

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download