WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Python · Intermediate · question 39 of 100

What is the purpose of the Python Standard Library, and what are some commonly used modules from it?

📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

In Python, an import statement is used to load code from other modules into the current module. There are two ways to specify the import path: relative and absolute.

A relative import specifies the location of the module relative to the current module. It is indicated by a dot (.) at the beginning of the module name. The dot represents the current directory, and additional dots represent higher-level directories. For example:

    # assuming the current module is in the directory mypackage/mymodule.py
    from . import myothermodule  # relative import of a module in the same package
    from .. import myparentmodule  # relative import of a module in the parent package

An absolute import specifies the full path of the module from the top-level directory of the project. It is indicated by the module name alone, without any dots. For example:

    # absolute import of a module in the same package
    from mypackage import myothermodule
    
    # absolute import of a module in a different package
    from otherpackage import anothermodule

Here are some differences between relative and absolute imports in Python:

Relative imports are only available within a package, whereas absolute imports can be used from anywhere in the project. Relative imports are resolved relative to the current module, whereas absolute imports are resolved from the top-level directory of the project. Relative imports are preferred for intra-package imports, as they are more concise and less prone to naming conflicts. Absolute imports are preferred for inter-package imports, as they are more explicit and less ambiguous.

In general, it is recommended to use absolute imports whenever possible, as they provide a more explicit and predictable way to import modules. However, relative imports can be useful in certain situations, such as when importing from a subpackage within a larger package.

In summary, relative and absolute imports in Python are used to load code from other modules into the current module, and differ in the way the import path is specified. Relative imports are resolved relative to the current module and are only available within a package, while absolute imports are resolved from the top-level directory of the project and can be used from anywhere in the project.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Python interview — then scores it.
📞 Practice Python — free 15 min
📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

All 100 Python questions · All topics