news.volyx.in

How the Python import system works (tenthousandmeters.com)

497 points by zikohh · 1888 days ago · 203 comments on HN

Article summary

The article explains how the Python import system works, covering topics such as modules, submodules, packages, and relative imports. It delves into the details of how Python creates module objects and how the import system resolves module names. The article aims to provide a comprehensive understanding of the import system, which is often considered one of the most misunderstood aspects of Python. By studying the import system, developers can better understand how to structure their projects and avoid common pitfalls.

Main themes

  • Python import system
  • Modules and packages
  • Relative imports
  • Circular dependencies
  • Modularity and independence
  • Dependency management
  • Best practices for imports

What commenters say

  • The Python import system is complex and difficult to understand, even for experienced developers.
  • Relative imports can be fragile and error-prone, and some argue that absolute imports are a better approach.
  • Circular dependencies can arise in large codebases, and there are different opinions on how to handle them, ranging from avoiding them altogether to using techniques like lazy loading.
  • Modularity is important for managing complex codebases, but it can be challenging to achieve when dealing with circular dependencies.
  • Some developers advocate for using tools like pipenv and virtual environments to manage dependencies and avoid import-related issues.
  • Others argue that understanding the import system is not necessary for most use cases, and that following best practices can help avoid common problems.
  • The importance of modularity and independence of modules is debated, with some arguing that it is crucial for maintainability and others seeing it as less important in certain contexts.