Before i go any further let's see a little example:
import module3 module3.function()
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.


Sign In
Create Account



Back to top









