1

How can I load a Python module given its full path? Note that the file can be anywhere in the file system, as it is a configuration option. i am using python 3.5 in ubutnu , my code so far is :

Create python module test.py

import sys
sys.path.append("test/lib/")
from tes1 import Client1
from tes2 import Client2
import tes3
hard_worker
  • 138
  • 4
  • sys.path.append() would work as long as the appended path contains the modules you want to import. What is your question exactly? Isn't it working? – Karl Jun 18 '19 at 10:29

1 Answers1

1

try this (Python 3.5+ support this syntax):

import importlib.util
spec = importlib.util.spec_from_file_location("lib.test", "/test/lib/test.py")
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
foo.MyClass()
Omer Anisfeld
  • 1,236
  • 12
  • 28