Search code examples
pythonpython-3.xwinreg

import winreg while retaining linux support


I have a python 3 application which currently supports windows and linux. I want to build some windows specific functionality which requires me to import winreg.

However the import winreg statement prevents the code from being executed under linux.

How can i use winreg without breaking the code under linux?


Solution

  • Manuel, in case you haven't reached the solution yet, although I presume you did, but maybe someone else may found it useful.

    Fence the code with:

    if 'posix' in sys.builtin_module_names:
        # *nix
        ...
    

    or:

    if 'nt' in sys.builtin_module_names:
        # Win
        ...
    

    At least I did so on several occasions writing x-platform apps in python 2.