analitics

Pages

Thursday, April 7, 2011

Using modules in Python 3.2

Python is quite known and appreciated.
Today I saw an article "What's your favorite programming language?" LinuxWeek proncetaj of a 26% (1074 votes) ...
In python "module" which is actually a library.
Using it is simple: import "module name"
See example below:

import os
There are many modules in the Python language.
For example I chose an example of how to display modules that are installed. Open the python command line and run the examples below:

C:\Python32>python
Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> help('modules')

Please wait a moment while I gather a list of all available modules...

__future__          audioop             imp                 shlex
_abcoll             base64              importlib           shutil
_ast                bdb                 inspect             signal
_bisect             binascii            io                  site
_codecs             binhex              itertools           smtpd
_codecs_cn          bisect              json                smtplib
_codecs_hk          builtins            keyword             sndhdr
_codecs_iso2022     bz2                 lib2to3             socket
_codecs_jp          cProfile            linecache           socketserver
_codecs_kr          calendar            locale              sqlite3
_codecs_tw          cgi                 logging             sre_compile
_collections        cgitb               macpath             sre_constants
_compat_pickle      chunk               macurl2path         sre_parse
_csv                cmath               mailbox             ssl
_ctypes             cmd                 mailcap             stat
_ctypes_test        code                marshal             string
_datetime           codecs              math                stringprep
_dummy_thread       codeop              mimetypes           struct
_elementtree        collections         mmap                subprocess
_functools          colorsys            modulefinder        sunau
_hashlib            compileall          msilib              symbol
_heapq              concurrent          msvcrt              symtable
_io                 configparser        multiprocessing     sys
_json               contextlib          netrc               sysconfig
_locale             copy                nntplib             tabnanny
_lsprof             copyreg             nt                  tarfile
_markupbase         csv                 ntpath              telnetlib
_md5                ctypes              nturl2path          tempfile
_msi                curses              numbers             test
_multibytecodec     datetime            opcode              textwrap
_multiprocessing    dbm                 operator            this
_pickle             decimal             optparse            threading
_pyio               difflib             os                  time
_random             dis                 os2emxpath          timeit
_sha1               distutils           parser              tkinter
_sha256             doctest             pdb                 token
_sha512             dummy_threading     pickle              tokenize
_socket             email               pickletools         trace
_sqlite3            encodings           pipes               traceback
_sre                errno               pkgutil             tty
_ssl                filecmp             platform            turtle
_string             fileinput           plistlib            turtledemo
_strptime           fnmatch             poplib              types
_struct             formatter           posixpath           unicodedata
_subprocess         fractions           pprint              unittest
_symtable           ftplib              profile             urllib
_testcapi           functools           pstats              uu
_thread             gc                  pty                 uuid
_threading_local    gdata               py_compile          warnings
_tkinter            genericpath         pyclbr              wave
_warnings           getopt              pydoc               weakref
_weakref            getpass             pydoc_data          webbrowser
_weakrefset         gettext             pyexpat             winreg
abc                 glob                queue               winsound
aifc                gzip                quopri              wsgiref
antigravity         hashlib             random              xdrlib
argparse            heapq               re                  xml
array               hmac                reprlib             xmlrpc
ast                 html                rlcompleter         xxsubtype
asynchat            http                runpy               zipfile
asyncore            idlelib             sched               zipimport
atexit              imaplib             select              zlib
atom                imghdr              shelve

Enter any module name to get more help.  Or, type "modules spam" to search
for modules whose descriptions contain the word "spam".

>>> dir('module')
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__
format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt_
_', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__'
, '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__
rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
 'capitalize', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'f
ormat', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'is
identifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupp
er', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'r
find', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines
', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> exit()
This is just another example of how to use python.