[PYTHON] The material covered by the module name cannot be called correctly at the import destination / source.

Outline of the task

Reproduction

module configuration and contents

Constitution

$ find . -type f
./test1/bin/test.py
./test1/conf/__init__.py
./test1/conf/test_settings.py
./submodule/__init__.py
./submodule/conf/__init__.py
./submodule/conf/submodule_settings.py
./submodule/submodule_test.py

Caller python content

$ cat ./test1/bin/test.py
# -*- coding: utf-8 -*-
import sys
import os

base_dir = os.path.dirname(__file__) + "/.."
sys.path.append( base_dir )
from conf import test_settings
#import conf.test_settings

sys.path.append(base_dir + "/../submodule")
import submodule_test

Contents of submodule

$ cat ./submodule/submodule_test.py
# -*- coding: utf-8 -*-
import sys
import os

#Even if you try to search your directory again, test.I go to read py's conf
sys.path.insert(0, os.path.dirname(__file__))
print sys.path
import conf.submodule_settings

solution

1. Call submodule from one level above

$ cat ./submodule/submodule_test.py
# -*- coding: utf-8 -*-
import sys
import os

#There should be no module name for submodule in the import source, so you can call it correctly.
sys.path.insert(0, os.path.dirname(__file__) + "/../")
print sys.path
import submodule.conf.submodule_settings

Hmm. You can read it, but you have to fix the name of the module. There seems to be a better way, but I don't know.

2. Use ConfigParser

I feel like reading a separate file of submodule as config, so it seemed that I could get rid of this problem just by using ConfigParser.

Recommended Posts

The material covered by the module name cannot be called correctly at the import destination / source.
If you are told cannot by Python import, review the file name
The avi file output by OpenCV cannot be played