Relative imports use dots: from . import module (current package), from .. import module (parent package). Only work within packages. Use explicit relative imports for clarity. Consider package restructuring implications.
Avoid using import statements inside functions, use dependency injection, restructure code to break cycles. Consider lazy imports, import inside functions. Signals possible design issues. Review module dependencies.
Build distribution using setuptools/wheel, upload to PyPI using twine. Create source and wheel distributions. Consider versioning, documentation, licenses. Handle package data, dependencies correctly.
Use requirements.txt or pyproject.toml, specify version ranges appropriately. Consider dev vs production dependencies. Use pip-tools or poetry for dependency management. Handle transitive dependencies.
Modern configuration file for Python projects (PEP 518). Specifies build system requirements, project metadata. Replaces setup.py, setup.cfg. Used by poetry, flit. Consider migration from setuptools.
Use __init__.py for package setup, expose public API. Handle optional dependencies, perform checks. Consider backwards compatibility. Implement proper error handling.
Use entry points, namespace packages, or import hooks. Define plugin interface, discovery mechanism. Consider version compatibility, security. Handle plugin loading/unloading, configuration.
Use virtual environments, specify version constraints carefully. Consider dependency resolution tools, container isolation. Handle conflicts through requirement specifications. Review dependency tree.
Use docstrings, maintain README files, generate API documentation. Follow PEP 257. Include usage examples, installation instructions. Consider documentation generation tools (Sphinx). Maintain changelog.
Import modules when needed, use importlib.import_module(). Consider performance implications, circular dependencies. Handle import errors appropriately. Implement proper cleanup.
Common import methods: 'import module', 'from module import item', 'from module import *', 'import module as alias'. Each has different namespace effects. 'import *' not recommended due to namespace pollution. Consider relative vs absolute imports.
__init__.py marks directory as Python package, can initialize package attributes, execute package initialization code. Can be empty. Controls what's exported with __all__. Essential for Python 2, optional but recommended for Python 3.
Python searches modules in order: current directory, PYTHONPATH, standard library directories, site-packages. Accessible via sys.path. Can modify runtime using sys.path.append(). Consider virtual environment impact on search path.
Use venv module: python -m venv env_name. Activate using source env/bin/activate (Unix) or env\Scripts\activate (Windows). Manages project-specific dependencies. Consider requirements.txt for dependency tracking.
Use MANIFEST.in, package_data in setup.py, include_package_data=True. Access using pkg_resources or importlib.resources. Consider data file locations, installation requirements. Handle path resolution.
Use packages for logical grouping, maintain clear hierarchy. Consider separation of concerns, circular dependencies. Implement proper initialization. Handle configuration, plugin systems appropriately.
setup.py defines package metadata, dependencies, entry points for distribution. Used with setuptools for package building and installation. Includes version, requirements, package data. Consider modern alternatives like pyproject.toml.
pip is Python's package installer, conda is cross-platform package/environment manager. pip works with PyPI, conda has own repositories. conda handles non-Python dependencies. Consider project requirements for choosing.
__all__ list controls what's imported with 'from module import *'. Explicitly defines public API. Good practice for large modules. Example: __all__ = ['func1', 'func2']. Consider documentation and maintenance.
Checks if module is run directly or imported. Common for script entry points, testing. Code under this block only runs when module executed directly. Consider module reusability, testing implications.
Wheels are built package format, faster installation than source distributions. Contains pre-built files, metadata. Reduces installation time, ensures consistency. Consider pure Python vs platform-specific wheels.
importlib provides import mechanism implementation, custom importers. Used for dynamic imports, import hooks. Access to import internals. Consider performance, security implications. Handle import errors.
Use try/except for optional imports, implement fallbacks. Consider platform-specific modules. Handle missing dependencies gracefully. Document requirements clearly.
Import hooks customize module importing process. Implement custom importing behavior, transformations. Used for special loading requirements. Consider performance impact, maintenance complexity.
Use semantic versioning, maintain changelog. Consider backwards compatibility, deprecation policy. Handle version bumping, release process. Document version requirements clearly.
Handle platform-specific code, use conditional imports. Consider environment differences. Test on multiple platforms. Handle path separators, line endings. Document platform requirements.