Importing and Using Modules
Another method is to write the modules in the native language in which the Python interpreter itself was written. For example, you can write modules in the C programming language 1 and when compiled, they can be used from your Python code when using the standard Python interpreter.
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.
The Importing Trace
Tracing a Backup Program's Modules
Follow the module-related steps in a program that creates a dated backup archive.
Import the modules: The program first imports os and time so that it can use them.
Identify backup inputs: The files and directories to be backed up are specified in the source list.
Choose the destination: The target_dir variable specifies the directory where the backup files are stored.
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.
Importing is the starting point: after the modules are imported, the program can use their described functionality as it builds the backup archive.
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 form | What the source describes | How Python uses it |
|---|---|---|
| Module used by Python | A module is imported before its functionality is used. | Python code uses the imported module. |
| Module written in C | The 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.
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
Treating compilation as unnecessary when using a C module.
The source states that C modules can be used from Python when compiled.
Fix:
Keep the sequence clear: write the module in C, compile it, and then use it from Python with the standard Python interpreter.Using a module before importing it.
The source's backup example first imports os and time and only then uses them.
Fix:
Make importing the first module-related step, followed by using the module's functionality.Assuming os.system is the preferred archive solution in the backup example.
The source calls replacing os.system with zipfile or tarfile the most important refinement.
Fix:
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
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?
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
- Python programs import modules before using their functionality.
- A module can be written in C, compiled, and then used from Python code with the standard Python interpreter.
- 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().
- 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.
Key Takeaways
- 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.