Modules/Libraries in python

--Originally published at Hector Martinez Alcantara

If you want to use a  function in other program, you can script it, it’s easy to use it in other program just importing the module. But, what’s importing , and what’s a module?

If you wanto to do your own module, you have to do this:

def function(optionalvalue):
     #Here's what you want to do
     #return a value

Yes, is like doing a simple function, then save it with the extension .py and save it in the current directory.

A module is like a script where you define some functions, and importing is the action when you want to use it, and you take it from the other script to your script.

You can import a function typing this:

#To import all the package
import module
#Or
from package import *

It’s that easy, but if you have a module in a package and you dont want to import all the package, you can specify what module and/or function you want to import.

If you want to do it specific, you have to type something like this:

#To import specific function
import package.module.function
#Or
from package.module import funcion

There’s much libraries (modules) that python also provides, apart of the standard library. The standard library is the library that contents the most basic functions you use in the program.

modules-python

To import other modules is the same process like you do with your own modules, but, with other names, obviously.

There’s a index of some default python modules.

datetime    Basic date and time types.
math           Mathematical functions (sin() etc.).
decimal      Implementation of the General Decimal Arithmetic Specification.
difflib         Helpers for computing differences between objects.
enum          Implementation of an enumeration class.
errno          Standard

system symbols.
formatter  Deprecated: Generic output formatter and device interface.
fpectl          Provide control for floating point exception handling.
fractions   Rational numbers.
ftplib          FTP protocol client (requires sockets).
zipfile        Read and write ZIP-format archive files.

Thanks for reading.

Special thanks to the Python Documentation.