w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Python: select packages to import in runtype [dynamically]

Based on post of Python Core Developer Andrey Svetlov.

I can suggest you to add platform/hardware specific Python modules in separate directories and append this modules to sys.path in runtime.

For example: We have a package

  • package
    • __init__.py
    • a.py
    • x86_64
      • b.py
    • i386
      • b.py

__init__.py will contain something like this:

import sys
import platform
from os.path import join, dirname
__path__.append(join(dirname(__file__), platform.machine()))

With this approach common code is located in package while platform specific distributed in subdirectories.

Take attention that x86_64 and i368 do not have __init__.py module.

Import required module b

from package import b

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.