pkgutil isn't considering an absolute path properly

Viewed 32

When I use pkgutil, for some reason its importing packages, but not from the path I expect, even when I use the import object provided by walk_packages.

In some circumstances its considering the syspath and not the full path I specified. Here is a code example:

import os
import pkgutil
import importlib

hip_dir = "C:/thepathicareabout/pythonStuff"
package_path = os.path.split(hip_dir)[0]
print("walk packages in path: {}".format(package_path))

packages = pkgutil.walk_packages([package_path])
found_module = False
for importer, name, is_package in packages:
    if "tests.acceptance.test_deadline" == name:
        print(name, importer, is_package)
        test_module = importlib.import_module(name)
        print("might be wrong", test_module)
        test_module = importer.find_module(name).load_module(name)
        print("should be right", test_module)
        found_module = True

produces this output:

walk packages in path: C:/thepathicareabout/pythonStuff
('tests.acceptance.test_deadline', <pkgutil.ImpImporter instance at 0x000000004B26D548>, False)
('might be wrong', <module 'tests.acceptance.test_deadline' from 'S:\someotherpath\pythonStuff\tests\acceptance\test_deadline.py'>)
('should be right', <module 'tests.acceptance.test_deadline' from 'S:\someotherpath\pythonStuff\tests\acceptance\test_deadline.py'>)
testing module at path: <module 'tests.acceptance.test_deadline' from 'S:\someotherpath\pythonStuff\tests\acceptance\test_deadline.py'>

Note the full path is incorrect and being affected by sys path

What might I be doing wrong here? I thought the point of package util would be to be able to import based on an explicit full path, but the sys path is changing the outcome. How might I get what I'm after regardless of sys path?

0 Answers
Related