In Python, scripts with ** hyphens (-) ** in their filenames cannot be imported statements. However, if you really want to import a file name with a hyphen to write a test, I found out what to do, so make a note.
__import__
** \ _ \ _ import \ _ \ _ ** takes the pathname of the file as an argument and returns the module of the file.
load-path.py
import sys
path = sys.path
>>> module = __import__("load-path")
>>> type(module)
<type 'module'>
>>> dir(module)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'sys', 'path']
>>> dir.path
Now you can easily import even files with hyphens.
py:unit.test.py
import unittest
class Tester(unittest.TestCase):
module = __import__("load-path")
def setUp(self):
for key in dir(self.module):
setattr(self, key, getattr(self.module,key))
Recommended Posts