#21 Python Modules / Python Lessons for Beginners

by Alex
#21 Python Modules / Python Lessons for Beginners

Previous lesson: Python Iterators

What is a module?

A module is a file containing python code that you want to include in a project. We have compiled documentation on python modules in Russian in the Modules section.

Creating a module

In order to create a module you just need to save the code into a file with the extension .py: Let’s save this code to a file called mymodule.py

def greeting(name):
print("Hello, " + name)

Using the module

Now we can use the module we have just created, using the import operator: Import the module named mymodule, and call the hello function:

import mymodule
mymodule.greeting("Andrei")

Output:

Hello, Andrew

Note: While using a function from a module, the syntax: module_name.function_name.

Variables in a module

A module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects, etc.). Let’s save this code to the file mymodule.py

person1 = {
"name": "Viktor",
{ "age": 36,
{ "country": "Russia"
}

Import the module with the name mymodule, and get access to the dictionary person1:

import mymodule
a = mymodule.person1["age"]
print(a)

Output:

36

Module name

You can name the module file whatever you like, but it is important to specify the .pyfile extension

Renaming the module

You can create an alias when importing a module using the as keyword.

import mymodule as mx
a = mx.person1["age"]
print(a)

Output:

36

Built-in modules

Python has several built-in modules that you can import whenever you want.

import platform
x = platform.system()
print(x)

Conclusion:

Windows

Using the dir() function

There is a built-in function for listing all function names (or variable names) in a module. The function dir().

import platform
x = dir(platform)
print(x)

Output:

['DEV_NULL', '_UNIXCONFDIR', '_WIN32_CLIENT_RELEASES', '_WIN32_SERVER_RELEASES',
'__builtins__', '__cached__', '__copyright__', '__doc__', '__file__',
'__loader__', '__name__', '__package__', '__spec__', '__version__',
'_default_architecture', '_dist_try_harder', '_follow_symlinks',
'_ironpython26_sys_version_parser', '_ironpython_sys_version_parser',
'_java_getprop', '_libc_search', '_linux_distribution', '_lsb_release_version',
'_mac_ver_xml', '_node', '_norm_version', '_parse_release_file', '_platform',
'_platform_cache', '_pypy_sys_version_parser', '_release_filename',
'_release_version', '_supported_dists', '_sys_version', '_sys_version_cache',
'_sys_version_parser', '_syscmd_file', '_syscmd_uname', '_syscmd_ver',
'_uname_cache', '_ver_output', 'architecture', 'collections', 'dist',
'java_ver', 'libc_ver', 'linux_distribution', 'mac_ver', 'machine', 'node',
'os', 'platform', 'popen', 'processor', 'python_branch', 'python_build',
'python_compiler', 'python_implementation', 'python_revision', 'python_version',
'python_version_tuple', 're', 'release', 'subprocess', 'sys', 'system',
'system_alias', 'uname', 'uname_result', 'version', 'warnings', 'win32_ver']

Note: The dir() function can be used on all modules, including those you create yourself.

Import from a module

You can import a module only partially by using the keyword from

def greeting(name):
print("Hello, " + name)
person1 = {
"name": "Victor",
"age": 36,
"country": "Russia"
}

Import the dictionary person1 from the module:

from mymodule import person1
print (person1["age"])

Output:

36

Note: When importing using the from keyword, do not use the module name when referring to elements. Example: person1["age"], not mymodule.person1["age"] Next: Dates in Python

Related Posts

LEAVE A COMMENT