Downloading each .py file along the way - imp.load_module complains about relative imports

I am trying to analyze a given path for python source files, import each file and DoStuff & trade; for each imported module.

def ParsePath(path):
    for root, dirs, files in os.walk(path):
        for source in (s for s in files if s.endswith(".py")):
            name = os.path.splitext(os.path.basename(source))[0]
            m = imp.load_module(name, *imp.find_module(name, [root]))
            DoStuff(m)

The code above works, but the packets are not recognized ValueError: Attempted relative import in non-package

My question is mainly how to report imp.load_modulethat this module is part of the package?

+3
source share
3 answers

You cannot directly specify the import protocol method load_modulethat this module is part of a package. Adapted from PEP 302 New Imported Hooks

Built-in function __import__( PyImport_ImportModuleEx      import.c) ,     import - .     ( a) ,      ( ). ,      "" " ",      "spam.eggs". ,      : "".      : ""      "import eggs.bacon" ( "spam.eggs"     ), "spam.eggs.bacon" . , "eggs.bacon"     . ( , ,      ).

,      . "import spam.ham" " "      , , ""      "".

   . , "Spam.ham",      "" .

, , .

+3

imp.find_module , imp.load_module

name ( , ).

, :

def ParsePath(path):
    for root, dirs, files in os.walk(path):
        for source in (s for s in files if s.endswith(".py")):
            name = os.path.splitext(os.path.basename(source))[0]
            full_name = os.path.splitext(source)[0].replace(os.path.sep, '.')
            m = imp.load_module(full_name, *imp.find_module(name, [root]))
            DoStuff(m)
+2

. , , imp importlib. :

import imp
import importlib
package_path = r"C:\path_to_package"

package_name = "module"
module_absolute_name = "module.sub_module"
module_relative_name = ".sub_module"

# Load the package first
package_info = imp.find_module(package_name, [package_path]) 
package_module = imp.load_module(package_name, *package_info)

# Try an absolute import
importlib.import_module(module_absolute_name, package_name)

# Try a relative import
importlib.import_module(module_relative_name, package_name)

This will allow us to import the sub_module using relative module paths, because we already loaded the parent package, and the submodule was loaded correctly by importlib to find out that it is imported relatively.

I believe that this solution is only necessary for those of us stuck in Python 2. *, but someone would need to confirm this.

0
source

Source: https://habr.com/ru/post/1790177/


All Articles