3

My project structure is like this:

--base
    __init__.py
    mod1.py
    mod2.py
    version.py

Contents of __init__.py:

from .version import __version__ as version

__version__ = version

mod2 contains a class called classA.

Inside mod1 I am importing classA like this:

mod1.py:

from base.mod2 import classA

However I get an error saying "No module named base".

Note: import mod2 works. However, I want to use the absolute path approach, but it does not work. Can anyone tell me what I am doing wrong?

EDIT: I am executing the python script from the base directory. Also, base is in sys.path (just verified).

EDIT 2: I want to import the class in a similar way.

  • 1
    take a look at [this](https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path) – Joao Pereira Mar 06 '19 at 16:47
  • That's very helpful. But I don't want to use the complete path of the file. I want to try to implement something like [this](https://github.com/user-cont/release-bot/blob/master/release_bot/releasebot.py#L26) – James Bringer Mar 06 '19 at 16:52

1 Answers1

0

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.

Hefnawi
  • 171
  • 1
  • 5
  • `from base import .mod2` in Python 3 – BHC Mar 06 '19 at 17:01
  • Thanks for your answer. Looks like you got the question wrong. I have edited the question to make it more clear. – James Bringer Mar 06 '19 at 17:04
  • @James did you try to call it from `ipython` from the parent directory of `base` or even from a different directory since it's already in the path? – Hefnawi Mar 06 '19 at 17:20
  • I am not using `iPython` but I am calling it from `base` directory. – James Bringer Mar 06 '19 at 17:23
  • Ok no problem, have you tried calling from a different directory, and double check that the `base` directory is in `sys.path` please. – Hefnawi Mar 06 '19 at 17:24
  • Yes. base is in sys.path. I have printed and verified it. My guess is that Python is somehow not recognizing the package, even though I have a `__init__.py` there. – James Bringer Mar 06 '19 at 17:28
  • I tested with the same directory structure used in your answer, but I ran it from the parent directory of `base` not from inside `base` directory itself. Could you try this from the parent directory of `base`: `from base.mod2 import classA` – Hefnawi Mar 06 '19 at 17:30
  • Please check my updated answer and let me know if something is not clear in my answer, thanks. – Hefnawi Mar 06 '19 at 17:41
  • Your solution is working because you are running it from the interactive shell, and not running mod2.py. However, I intend to execute mod2.py. Which is giving me an error – James Bringer Mar 06 '19 at 17:44
  • I will try to demonstrate it using a python script instead of interactive shell, give me a moment. Thanks for your feedback. – Hefnawi Mar 06 '19 at 17:45
  • Please check my UPDATE 2. Thanks. I used `Python 2.7.15rc1` – Hefnawi Mar 06 '19 at 17:51
  • Sorry for being very picky, but I was talking about `mod2.py` which is in `base` directory. All your solutions work for me, but mod2.py is giving me the error. Thanks for all your help! – James Bringer Mar 06 '19 at 18:03
  • I don't really understand why you want to call `mod2.py` from inside `base`, could you elaborate more? Also the reason why people use packages is not to be able to call sub-modules from inside the package directory, I am just trying to help so let me know ;) – Hefnawi Mar 06 '19 at 18:05
  • Also please tell us how are you running mod2.py, is it just like this: `python mod2.py` ? – Hefnawi Mar 06 '19 at 18:09
  • @JamesBringer Ok if you must import the module from the `base` directory itself which is the package directory, you could just do this `from mod2 import classA` and it would work, please confirm if this is what you want – Hefnawi Mar 06 '19 at 20:03