Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

attempted relative import in non-package python #431

Open
richardzulu opened this Issue · 7 comments

5 participants

@richardzulu

Whenever I run python manage.py harvest, I receive a traceback call as depicted

Traceback (most recent call last):
File   "/home/xxxx/programming/apps/xxxxx/sipi/src/lettuce/lettuce/django/management/commands/harvest.py", line 185, in handle
result = runner.run()
File "/home/xxxxx/programming/apps/xxxx/sipi/src/lettuce/lettuce/__init__.py", line 148, in run
self.loader.find_and_load_step_definitions()
File "/home/xxxxx/programming/apps/xxxxxx/sipi/src/lettuce/lettuce/fs.py", line 58, in   find_and_load_step_definitions
raise e
ValueError: Attempted relative import in non-package when importing  /home/xxxx/programming/apps/xxxx/sipi/local/lib/python2.7/site- packages/debug_toolbar/utils.py
@ersherr

i too am having this issue -- is anyone aware of a fix or workaround?

@michelts

I just remove debug_toolbar from settings when harvesting:

if 'harvest' in sys.argv or 'test' in sys.argv:
    INSTALLED_APPS.remove('debug_toolbar')
@dhiana

I've just faced this issue. Nothing to do with debugtoolbar, though...

@dhiana

The issue here is:

  • some of my project's apps have no "features" module
  • still, i'm reusing a variable "PROJECT_APPS" (used for django_jenkin) for LETTUCE_APPS
  • but their tests.py have some relative imports (which is fair)
  • lettuce can't skip without throwing this error (it ends execution)

Workarounds:

  • Adding "features" directory and it's init.py at each of the LETTUCE_APPS without features
  • or maintaining a curated list of apps with only those who actually have features (DRY violation)

Maybe a good solution would be catching this exception and skipping the loading...

@dhiana

Another workaround:

LETTUCE_APPS = []
for app in PROJECT_APPS:
    try:
        __import__(app+'.features')
        LETTUCE_APPS.append(app)
    except:
        pass

LETTUCE_APPS = tuple(LETTUCE_APPS)
@danni
Collaborator

Okay, this is a bug in the feature loader when combined with relative imports, i.e. one step file importing things from another step file using relative imports.

@danni
Collaborator

There's a fix for this on https://github.com/infoxchange/lettuce/tree/new-parser

Although it requires your steps directory to be importable as part of your Python package to work (i.e. you need __init__.py files. Alternatively consider Aloe, which is next generation Lettuce based on nose (and takes a whole bunch of the code from new-parser).

@danni danni referenced this issue from a commit in infoxchange/aloe
@danni danni Adapt test to demonstrate broken relative imports
From gabrielfalcao/lettuce#431 -- Lettuce's
step loader, on which Aloe is based, cannot handle relative imports.

The problem appears to be caused by adding the directory itself to the
package import root (this would make it an absolute import). It would be
better not to manipulate the import path and instead determine the
Python package path for the file we wish to import from a path we
already have.
457ba56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.