2 years ago

#70080

test-img

geekscrap

Differenciate between import statements

I have a requirement to differentiate between the following import techniques from within mylibrary:

import mylibrary

and

from mylibrary import abc

The reason I need to do this is my library is used by a 3rd party tool (lets call it othertool) which only does import mylibrary. This will then run some code specific to othertool (setting up the environment specific to othertool)

mylibrary can of course also be used as a library, and so I dont want the code specific to othertool being run when it is used like from mylibrary import abc.

I have tried things like the following, but can't find anything to differentiate between the two import techniques.

import sys
binary_names = ['othertool', 'othertool.py']
if any(bn in arg for bn in binary_names for arg in sys.argv):
    from mylibrary import main_othertool

But this would require modification if othertool changed it's name.

Any suggestions? TIA!

python

python-import

0 Answers

Your Answer

Accepted video resources