If you are running a script in the folder a, I would like to get the absolute path of c.py in the folder b in the same hierarchy as a in that script.
For example, a and b are in the same folder named d.
python
From Comments
It's solved.We actually used os.path.dirname to specify the upper hierarchy.
_init__.py
must be placed in order to import
anything below the b folder.
_init__.py
should be empty.
You can specify where this file is located in import
.
Folder Configuration
a/
├ ├ hoge.py
└ b/
├ __init__.py
└ └ c.py
hoge.py
from b.import something
something()
c.py
def something():
print('test')
By the way, the import
method can also be import b.c
.
However, if you import it that way, it must be b.c.something()
when calling the method.
Class definitions can also be invoked.
hoge.py
from b.import something, TestClass
something()
test_class = TestClass()
test_class.something()
c.py
def something():
print('test')
class TestClass():
def__init__(self):
pass
def something (self):
print('class test')
Check the documentation for details.
http://docs.python.jp/2/tutorial/modules.html#packages
© 2023 OneMinuteCode. All rights reserved.