Conditionally modify import statements.

huangapple go评论60阅读模式
英文:

Conditionally modify import statements

问题

I have a collection of .py files in the following file structure:

packagename
mod_1.py
mod_2.py
script_1.py
etc.

These files do two things:

  1. They're bundled into an application (a JMP/JSL add-in, although not relevant here). In this application script_1.py is called, and it contains import statements like import mod_1. mod_1.py in turn imports mod_2.py as import mod_2. This is fine.

  2. The files are also bundled up into a python package called packagename. In this context when I want to import mod_2.py inside mod_1.py I call import packagename.mod_2. This is also fine.

I presently implement this in mod_1.py, very painfully, as:

if 'addin' in file:
import mod_2
else:
import packagename.mod_2

This works but I've got lots and lots of modules (more than just mod_1, mod_2). Is there a way to do something like this (pseudocode)?:

if 'addin' in file:
import_prefix = ''
else:
import_prefix = 'packagename.'

import import_prefix + mod_2

This answer seems to cover half of what I'm looking for, but not everything:
https://stackoverflow.com/questions/12229580/python-importing-a-sub-package-or-sub-module

英文:

I have a collection of .py files in the following file structure:

packagename\
    mod_1.py
    mod_2.py
    script_1.py
    etc.

These files do two things:

  1. They're bundled into an application (a JMP/JSL add-in, although not relevant here). In this application script_1.py is called, and it contains import statements like import mod_1. mod_1.py in turn imports mod_2.py as import mod_2. This is fine.
  2. The files are also bundled up into a python package called packagename. In this context when I want to import mod_2.py inside mod_1.py I call import packagename.mod_2. This is also fine.

I presently implement this in mod_1.py, very painfully, as:

if 'addin' in __file__:
    import mod_2
else:
    import packagename.mod_2

This works but I've got lots and lots of modules (more than just mod_1, mod_2). Is there a way to do something like this (pseudocode)?:

if 'addin' in __file__:
    import_prefix = ''
else:
    import_prefix = 'packagename.'

import import_prefix + mod_2

This answer seems to cover half of what I'm looking for, but not everything:
https://stackoverflow.com/questions/12229580/python-importing-a-sub-package-or-sub-module

答案1

得分: 1

你可能只想使用importlib.import_module

import importlib

def _import(module_name: str):
    if 'addin' in __file__:
        return importlib.import_module(module_name)
    return importlib.import_module(f'packagename.{module_name}')

mod_2 = _import(mod_2)
英文:

You may just want to use importlib.import_module.

import importlib

def _import(module_name: str):
    if 'addin' in __file__:
        return importlib.import_module(module_name)
    return importlib.import_module(f'packagename.{module_name}')

mod_2 = _import(mod_2)

huangapple
  • 本文由 发表于 2023年6月9日 00:59:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定