Project 9: Libraries/Modules

--Originally published at TEC GDL 2016

Modules within Python are used, when you want to access a good code or functions defined earlier within a new code. If modules/libraries are not created, usually all functions and variables that you had in the interpreter will get lost. Thus, programmers are advised to use different modules to access in their codes by ‘importing’ them. A module contains definitions and statement.

To create a module a ‘script’ has to be created (on any text editor) which then has to be saved under a file name ending .py . When you wish to use this module within one of your programs, you need to add this to your program, before making use of the module:

import module_name

Once the module is imported, you are able to call the functions used in the original module by typing:

module_name.function_name

When you wish to access different functions of a module directly you can also type:

from module_name import function_name

This part below describes quite well how a module is imported and used again after being saved as such:

bildschirmfoto-2016-09-15-um-01-25-10

Thanks for reading this blog post.
If there are any questions remaining, please comment or hit me up on Twitter!

See ya,
@tecjames

 

Sources:
https://docs.python.org/3/tutorial/modules.html