UPDATE 3: If you want to import a submodule from within the package directory, which is base
, you could use this (omitting the base
part in the import) in the file/module mod1.py
,
from mod2 import classA
The above solution also works with run_test_1.py
and run_test_2.py
scripts. Hope this helps =)
UPDATE 2: I will demonstrate the same approach by executing a python script from the test
directory,
create a script called run_test_1.py
under test
directory following the same structure as "UPDATE 1", with the following contents:
from base.mod2 import classA
a = classA()
Also create a second script called run_test_2.py
under test
directory as well, with the following contents:
from base import mod1
mod1.classA()
Evaluate both scripts from your terminal like so,
$ cd ~/path/to/test/
$ python run_test_1.py
hello
$ python run_test_2.py
hello
As demonstrated you should get the output of "hello" on your terminal.
UPDATE 1: I created the same directory structure for your package like so under a directory called test
,
-- test
--base
__init__.py
mod1.py
mod2.py
version.py
Contents of __init__.py
is the same as yours.
from .version import __version__ as version
__version__ = version
Contents of mod1.py
is the same as yours,
from base.mod2 import classA
Contents of mod2.py
:
class classA:
def __init__(self):
print("hello")
Then using the python
interpreter from the directory test
, I tested the following,
>>> from base.mod2 import classA
>>> a = classA()
hello
If you are sure that base
directory is in the sys.path
then you could try this,
from base import mod2
Also I suggest you change base
to something more informative for your project.
EDIT: I also suggest you check the "Packages" documentation on the Python website, which also discusses how to load a submodule.