0

suppose I have a file my_plugin.py

var1 = 1
def my_function():
    print("something")

and in my main program I import this plugin

import my_plugin

Is there a way to silently disable this plugin with something like a return statement?

for example I could "mask" the behavior of my_function like this:

def my_function():
    return
    print("something")

I am wondering if I can do this for the module as a way to turn it on and off depending on what I am trying to do with the overall project. So something like:

return  # this is invalid, but something that says stop running this module
        # but continue on with the rest of the python program
var1 = 1
def my_function():
    print("something")

I suppose I could just comment everything out and that would work... but I was wondering if something a little more concise exists

--- The purpose: The thinking behind this is I have a large-ish code-base that is extensible by plugins. There is a plugins directory so the main program looks in the directory and runs all the modules that are in there. The use case was just to put a little kill switch inside plugins that are causing problems as an alternative to deleting or moving the file temporarily

Vince W.
  • 3,561
  • 3
  • 31
  • 59
  • Take a look at https://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module . To unload a module in python can be tricky – rafaelc May 29 '18 at 18:25
  • 2
    I've never needed to do this. What's your use case? It's probably something you can tackle from your main program to decide if you want to import or not. – Robert Seaman May 29 '18 at 18:25
  • 1
    You can put whole module contents under `if` statement. You can put `import` statement under `if` statement. You can monkey-patch that module - load a stub module and put it into `sys.modules['my_plugin']` – Andrii Maletskyi May 29 '18 at 18:27
  • @RobertSeaman I added an explanation of what I am doing in the question, thanks – Vince W. May 29 '18 at 18:33
  • (With your edit in mind) This violates the zen of python. "Explicit is better than implicit." Instead of loading ALL plugs you should load each of them individually then add or remove them from the main file as necessary. – Ryan Schaefer May 29 '18 at 18:34
  • @RyanSchaefer, yes, there is probably some wisdom in that. – Vince W. May 29 '18 at 19:06

1 Answers1

4

You can just conditionally import the module:

if thing == otherthing:
   import module

This is entire valid syntax in python. With this you can set a flag on a variable at the start of your project that will import modules based on what you need in that project.

Ryan Schaefer
  • 3,047
  • 1
  • 26
  • 46
  • Also add `# NOQA` at the end of these out of place imports to avoid linters getting offended by it. More info https://stackoverflow.com/a/45346791/3820185 – wiesion May 29 '18 at 18:27
  • Thanks, sometimes things that seem obvious escape the mind. This fits in quite well with the structure I already have in place – Vince W. May 29 '18 at 19:03