Source From Here
Question
Problem is that I can't find a way to instruct Python to load submodule from string.
How-To
The __import__ function can be a bit hard to understand. In Python 2.7 and Python 3.1 or later, you can use importlib. Supposed you have one module:
- test.py
Then you can import it his way:
Some notes:
Question
Problem is that I can't find a way to instruct Python to load submodule from string.
How-To
The __import__ function can be a bit hard to understand. In Python 2.7 and Python 3.1 or later, you can use importlib. Supposed you have one module:
- test.py
- def add(a, b):
- return a + b
- def sub(a, b):
- return a - b
- class Clz:
- def __init__(self, name):
- self.name = name
- def __repr__(self):
- return self.__str__(self)
- def __str__(self):
- return "Name={}".format(self.name)
Some notes:
沒有留言:
張貼留言