0

I am creating a package that depends on the python-gnupg PyPi package. In Python it is imported as gnupg. It is installed during the setup of my package.

Unfortunately there is the gnupg PyPi package which is also imported as gnupg in Python.

How do I make sure that the right package is called when I run from gnupg import ...? I want my package to work for users who have already installed the 'wrong' gnupg in their Python distribution and now have both packages residing in their site-packages directory.

Elias Strehle
  • 1,722
  • 1
  • 21
  • 34

1 Answers1

1

You can load a module by its full path. That way you are sure which one is loaded:

import importlib.util
spec = importlib.util.spec_from_file_location("module.name", "/path/to/file.py")
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)

Taken from: How to import a module given the full path?

Sharku
  • 1,052
  • 1
  • 11
  • 24
  • Is it possible to make this a bit more flexible? Instead of specifying an absolute path, can I tell importlib: 'Import whatever `gnupg` you find on the path, but only consider packages that have `python-gnupg` in their name'? – Elias Strehle Mar 04 '19 at 11:07