0

I wrote a Python package pack that can perform a set of related tasks, taskA, taskB, ..., taskZ. Each of them has its own module file, e.g. taskN.py.

Now say in taskN.py I import a third party package evilpack. It Works On My Machine™, but a coworker of mine (a) can't install evilpack, but (b) does not even need that module taskN.

My goal is to structure my package so that we can choose at import time whether we want to load the module taskN or ignore it.

What's the most elegant way to solve this? I'm sensing it has something to do with the directories' __init__.py files.

Alexander Engelhardt
  • 1,632
  • 3
  • 16
  • 31
  • using `__all__` in your code to exposure code / module to specific person will help you [link](https://stackoverflow.com/questions/44834/can-someone-explain-all-in-python) – sahasrara62 Jan 10 '19 at 09:01
  • I'd prefer to avoid a `from pack import *` statement, and instead ideally have a `import pack` for both me and my coworker. Would that still work in that case? – Alexander Engelhardt Jan 10 '19 at 09:04
  • What is your criteria for exclusion? The code handling the imports must be able to know what to read from your coworker's system to exclude – ycx Jan 10 '19 at 09:12
  • @ycx I'm not sure what you mean by "criteria for exclusion", but I want to exclude exactly one pre-specified module within my package, and I am looking for a way to specify before/while importing whether or not to exclude that module - via some flag or option. – Alexander Engelhardt Jan 10 '19 at 09:16
  • you can chose from requirment.txt file which package to chose when building a package . dintinguish there which package are necessary for whole package(core component) and which are others then from there making the whole module like that – sahasrara62 Jan 10 '19 at 09:21

2 Answers2

1

A simple way to solve this problem:

Identify all of the modules that may have unfulfilled dependencies.

In the main module that does the importing, surround each such import with a try...except clause:

try:
    import packN
except ImportError as details:
    print ("Could not import packN because of this error:", details)
    print ("Functionality xxxx will not be available")
    packN = None

If your colleague's code doesn't call a function that relies on packN then all will be well.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • Thanks, I've tried it and it works exactly as advertised :) One more question if you don't mind: If I have like ten successive statements of the form of `from .subpack.taskN import TaskN`, with TaskM, TaskN, etc., is there a way to write a loop? I wouldn't know how to do a relative import by picking modules as a string, i.e. from a list of strings. – Alexander Engelhardt Jan 10 '19 at 12:19
  • 1
    @AlexanderEngelhardt I've never tried importing in a loop but that is because I have never worked with 10 like-named imports that differ only in the digit in the name. You *could* call `__import__()` in a loop because that accepts the name of the imported module as a string. But if it's only 10 and not 300 I would do it the long way. – BoarGules Jan 10 '19 at 12:22
0

I think I can only point you in the correct direction via setupscript because I do not have access to your data/package details.

To simply put, you will have to locate your taskN.py's setup.py script, and specifically remove the module from inside the script.

ycx
  • 3,155
  • 3
  • 14
  • 26