0

EDIT: Solved! Solution on the bottom of this post. tl;dr: I should have been using relative imports, and launching python with the correct flag.

So I'm trying to get used to some python stuff (version 2.7.3—it's what's installed on the work PC), and I've got a little project to teach me some python stuff. I've got a module defined like so:

curses_graphics
| __init__.py
| graphicsobject.py
| go_test.py

(code to follow) I've got a decorator defined in __init__, and I'm trying to use it on methods defined in a class in graphicsobject.py that I'm trying to attach the decorator to, and I'm testing its functionality in go_test.

When I run go_test (just being in the directory and calling "python go_test.py"), I get a no package found error. Same happens if I rename go_test to __main__.py and try to run the package from its parent directory. If I try to run the go_test without importing the package, it doesn't recognise my function.

How can I reference a function defined in __init__.py from within the same package? Is it wrong to try and import while within the package?

Thanks!

__init__.py:

import curses
import logging
#Define logging stuff
gLog = logging.getLogger("cg_log")
gLog.basicConfig(filename="log.log", level=logging.DEBUG)

# Logging decorators for this library

def debug_announce(module_func):
    log=logging.getLogger("cg_log")
    log.info("Curses Graphics Lib:Entering function:"+module_func.__name__)
    module_func()
    log.info("Curses Graphics Lib:Left function:"+module_func.__name__)

go_test.py

@debug_announce
def logTester():
    print("A test method")

logTester()

logger.debug("Curses initialization")
screen=curses.initscr()
curses.start_color()
screen.keypad(True)

logger.debug("Initializing a green block filled with red '.'s")
block = GraphicsOBject.blankline(0, 0, 3, curses.COLOR_GREEN, curses.COLOR_RED, 20, '.')
logger.debug("Drawing the block.")
block.draw(screen)

logger.debug("Pausing execution with a getch")
screen.getch()

logger.debug("Cleaning up curses")
screen.keypad(False)
curses.endwin()
os.system('stty sane')

I can include graphicsobject.py, though I suspect that would be clutter here, as the issue occurs on the first line of go_test.py

Thanks, everyone!

EDIT: I'm attaching a capture of the errors reported. In the first error, I've added "from curses_graphics import debug_announce" and in the second the code doesn't have that line.

Errors with and without debug_announce import

EDIT: Some further searching led me to relative imports. For anyone who has my issue, you use those to import something defined in your own module. In my case, I appended "from . import debug_announce" to the head of my go_test.py file. I attempted to run it, and received the error “Attempted relative import in non-package”.

Some further searching led me to this question: How to fix "Attempted relative import in non-package" even with __init__.py

Which told me that it wasn't attempting to run this program as a package. This meant the "__init__.py" was never running, as the package wouldn't be imported. Further, since I was inside the package, attempting to import the package (i.e. "import curses_graphics") would result in it searching inside curses_graphics... for curses_graphics.

To run this correctly, as the linked question implies, I need to go to the parent directory of curses_graphics and execute it with "python -m curses_graphics.go_test". This runs the package with its inits and such and run the script of go_test.

(And, FWIW, my code had other issues. Don't take this as an example of how to use curses or logging :P)

0 Answers0