-1

I am struggling to successfully import my project to the test-suite in my project, as well as being able to run the program from the command-line. I've been able to run my test-suite for some time, under the impression that if the tests work, so does the command-line stuff--evidently this isn't the case. I do not yet intend on using my program as a library. The api.py acts is the entry-point for the program.

I have a project with the following structure (the same directory hierarchy as requests):

myapp/
    myapp/
        __init__.py
        api.py # depends on commands.py
        commands.py # depends on utils.py
        utils.py
    tests/
        context.py
        test_api.py # depends on api.py
        test_commands.py # depends on commands.py, utils.py

In the file context.py I have a path modification adding myapp to the PYTHONPATH, so I can successfully run the tests on my code. Here is the contents of that file

import os
import sys

sys.path.insert(0, os.path.abspath('..'))
import myapp

I've tried imaginable import combination I can think of. Far too many to list! I have also perused the Python reference import system page, and this tutorial.

How should I import my dependencies?

kluvin
  • 5,235
  • 3
  • 13
  • 20

1 Answers1

0

Turns out this was the correct layout, I mistook the error for something else. Although for future reference, relative imports in Python 3 must be explicit: when in the myapp package directory you can't say import commands, you must instead import it as from . import commands. This was defined in PEP 328 also see this SO post on the topic. Run your package with python -m mutil.api not python ./mutil/api.py as the latter won't give the interpreter context of the current path.

kluvin
  • 5,235
  • 3
  • 13
  • 20