0

I have a python script that refers to a module from its ROOT directory. The directory has the following structure:

tensorflow_2
  Mask_RCNN
    mrcnn
      model.py
    samples
      balloon
        balloon.py

The code goes as follows

import os  
ROO_DIR = os.path.abspath("../../")
print(ROOT_DIR)

/home/mypc/tensorflow_2/Mask_RCNN

from mrcnn.model import log
from samples.balloon import balloon

The first of the imports stated above can be run without issue. The second won't run. It can be sovled by copying the balloon.py to the ROOT directory and just stating:

import balloon

However, this is not a fix in my eyes, just mediation of the problem. I already tried calling:

export PYTHONPATH="$PYTHONPATH:~/mypc/tensorflow/Mask_RCNN"

to add the ROOT directory to my pythonpath.

log is a function in model.py.

can someone explain to my why it is possible to import a function but not the balloon.py file.

I have not written the code, btw. I am running python 2.7 due to ROS limitations. Running the same script in a python3 virtualenv gave the same result.

1 Answers1

0

Try

import samples.balloon.balloon

or

from samples.balloon.balloon import someFunction

Notice that balloon.py sits in a subdirectory called balloon.

Hope this helps.

x3l51
  • 693
  • 4
  • 19
  • When i try import samples.balloon.balloon it states that there is no module named samples.balloon.balloon. The second gives the same statement, when i try from samples.balloon.balloon import *. to import all functions – Thomas Hordijk Mar 15 '19 at 14:23
  • Yes there is `from samples.balloon.balloon import *` where `*` is kind of a "wildcard" that imports all functions – x3l51 Mar 15 '19 at 14:24
  • They both return the same statement – Thomas Hordijk Mar 15 '19 at 14:26
  • Weird, please take a look at this for I have found this just now: https://stackoverflow.com/questions/12229580/python-importing-a-sub-package-or-sub-module – x3l51 Mar 15 '19 at 14:29
  • 1
    Thank you for the help, it didn't solve the problem for me. I did find a solution though. I saw that the mrcnn dir had an `__init__.py` file included. I copied this (empty) file to the samples and balloon directory and now it is working. – Thomas Hordijk Mar 15 '19 at 14:42