Concepts / Importing and Using Modules

Importing and Using Modules

After the C module is compiled, it can be used from Python code when using the standard Python interpreter.

  • Programming

From Python Code to Module Functionality

A Python program can use functionality that is organized in modules. The usual sequence is to import the modules first and then use what they provide. The source material illustrates this with the os and time modules: the program imports them, uses information about files and directories, and uses time.strftime() to generate the date and time for a backup archive name.

Python programimports and uses the moduleImportmodule becomes available toPython codeCompiled C modulecompiled native-languagemoduleModule functionalityused from Python coderequestsconnects toprovides
How does a Python program connect to and use a module written in C after that module has been compiled?

The Importing Trace

Worked example

Tracing a Backup Program's Modules

Follow the module-related steps in a program that creates a dated backup archive.

  1. 1
    Import the modules

    The program first imports os and time so that it can use them.

  2. 2
    Identify backup inputs

    The files and directories to be backed up are specified in the source list.

  3. 3
    Choose the destination

    The target_dir variable specifies the directory where the backup files are stored.

  4. 4
    Create the archive name

    The program uses time.strftime() to generate the current date and time, adds the .zip extension, and stores the resulting archive in target_dir.

Result

Importing is the starting point: after the modules are imported, the program can use their described functionality as it builds the backup archive.

Python codeuses imported functionalityPython interpreterstandard interpreterCompiled C modulemodule written in C andcompiledruns withcan useuses through import
What contains or connects the Python code, the Python interpreter, and the compiled C module?

Native-Language Modules

A module does not have to be written directly in Python. Another method is to write a module in the native language in which the Python interpreter itself was written. The source gives C as the example. After the C module is compiled, it can be used from Python code when using the standard Python interpreter.

Module formWhat the source describesHow Python uses it
Module used by PythonA module is imported before its functionality is used.Python code uses the imported module.
Module written in CThe module is written in C and then compiled.The compiled module can be used from Python code with the standard Python interpreter.

The important distinction is how the module is produced; the source emphasizes that a compiled C module can still be used from Python code.

C modulewritten in CCompilationmodule is compiledImportPython code imports itPython useused with the standardinterpreterthenmakes it usable forenables
What happens next from writing a module in C through compiling it to importing and using it in Python?

Choosing Archive Modules

The backup example creates archives through os.system, but the source identifies a more important refinement: use the built-in zipfile or tarfile modules instead. These modules are part of the standard library and are already available without an external dependency on the zip program being present on the computer.

Importing Mistakes

  • Common mistake

    Treating compilation as unnecessary when using a C module.

    What it looks like

    A C module is written, but the compilation step is skipped before trying to use it from Python.

    Why it fails

    The source states that C modules can be used from Python when compiled.

    Do this instead

    Keep the sequence clear: write the module in C, compile it, and then use it from Python with the standard Python interpreter.

  • Common mistake

    Using a module before importing it.

    What it looks like

    A program tries to use the os or time module before the program has imported it.

    Why it fails

    The source's backup example first imports os and time and only then uses them.

    Do this instead

    Make importing the first module-related step, followed by using the module's functionality.

  • Common mistake

    Assuming os.system is the preferred archive solution in the backup example.

    What it looks like

    The program continues to create archives through os.system even though a built-in archive module is suitable.

    Why it fails

    The source calls replacing os.system with zipfile or tarfile the most important refinement.

    Do this instead

    Consider the built-in zipfile or tarfile modules, which are part of the standard library and do not depend on the zip program being available.

Check Your Trace

MEDIUM

Describe the complete module-related sequence for a module written in C. Then describe the corresponding sequence in the backup example, including the imported modules, the source list, target_dir, and the date-and-time archive name.

Hints
  • Begin with the action performed on the C module before Python uses it.
  • For the backup example, identify the two modules imported at the beginning.
  • Separate the files being backed up from the directory where the archive is stored.

What do you think happens?

A module is written in C but has not yet been compiled. Can the source-supported process already use it from Python code with the standard Python interpreter?

  • Yes, compilation is not part of the process

  • No, the C module must be compiled first

Reveal answer
Answer:

No, the C module must be compiled first.

The source describes C modules as becoming usable from Python after they are compiled.

Key Takeaways

  1. 1
    Python programs import modules before using their functionality.
  2. 2
    A module can be written in C, compiled, and then used from Python code with the standard Python interpreter.
  3. 3
    The backup example imports os and time, uses a source list for files and directories, stores the destination in target_dir, and creates a date-and-time .zip name with time.strftime().
  4. 4
    The built-in zipfile and tarfile modules are identified as preferable archive alternatives to os.system because they do not depend on the zip program being available externally.

At a glance

  • Importing establishes the starting point for using module functionality in Python.
  • Compiled C modules can be used from Python code with the standard Python interpreter.
  • The process for a native-language module is write, compile, import, and use.
  • For archive creation, the source recommends considering the built-in zipfile or tarfile modules instead of os.system.

Related concepts

More concepts starting with I