Given the full path, how to import the module?

Given the full path, how to load Python module? Note that the file can be anywhere in the file system because it is a configuration option.

#1 building

The advantage of adding a path to sys.path (by using imp) is that it simplifies operations when importing multiple modules from a single package. For example:

import sys
# the mock-0.3.1 dir contains testcase.py, testutils.py & mock.py
sys.path.append('/foo/bar/mock-0.3.1')

from testcase import TestCase
from testutils import RunTests
from mock import Mock, sentinel, patch

#2 building

You can use the pkgutil module (in particular walk_packages Method) to get the list of packages in the current directory. From there, using importlib to mechanically import the required modules is simple:

import pkgutil
import importlib

packages = pkgutil.walk_packages(path='.')
for importer, name, is_package in packages:
    mod = importlib.import_module(name)
    # do whatever you want with module now, it's been imported!

#3 building

In Linux, you can add symbolic links to the directory where python scripts are located.

Namely:

ln -s /absolute/path/to/module/module.py /absolute/path/to/script/module.py

If you change the contents of / absolute/path/to/module/module.py, python creates / absolute/path/to/script/module.pyc and updates it.

Then include the following in mypython script.py

from module import *

#4 building

I think the best way is to get it from official documents( 29.1. Import - access imp ort internals ):

import imp
import sys

def __import__(name, globals=None, locals=None, fromlist=None):
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module(name)

    try:
        return imp.load_module(name, fp, pathname, description)
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close()

#5 building

This area of Python 3.4 seems hard to understand! However, some hacking was done using Chris Calloway's code, and I managed to make some work properly. This is the basic function.

def import_module_from_file(full_path_to_module):
    """
    Import a module given the full path/filename of the .py file

    Python 3.4

    """

    module = None

    try:

        # Get module name and path from full path
        module_dir, module_file = os.path.split(full_path_to_module)
        module_name, module_ext = os.path.splitext(module_file)

        # Get module "spec" from filename
        spec = importlib.util.spec_from_file_location(module_name,full_path_to_module)

        module = spec.loader.load_module()

    except Exception as ec:
        # Simple error printing
        # Insert "sophisticated" stuff here
        print(ec)

    finally:
        return module

This seems to use modules that are not recommended in Python 3.4. I don't pretend to understand why, but it seems to work in the program. I found that Chris's solution worked on the command line, but not inside the program.

Keywords: Python Linux

Added by yaatra on Fri, 13 Dec 2019 16:24:57 +0200