I want to import sibling module.
I searched for it and got dozens of solutions. But it all didn't work at all.
This is the example structure.
test/
+ __init__.py
+ A.py
+ test_B/
+ __init__.py
+ B.py
+ test_C/
+ __init__.py
+ C.py
I tried to import module B in module C.
# import test_B.B
# => Error!
# import test.test_B.B
# => Error!
# from test.test_B import B
# => Error!
# from ..test_B import B
# => Error!
# from test_B import B
# => Error!
# import sys
# sys.path.insert(0, "../test_B")
# import B
# import test_B.B
# => Error!
How can I do this?
I found a method.
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(__file__))
from test_B import B
This method works good.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.