attempted relative import in non-package python #431

Open
richardzulu opened this Issue Feb 23, 2014 · 7 comments

Comments

Projects
None yet
5 participants

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 commented Apr 23, 2014

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

Contributor

michelts commented Apr 23, 2014

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 commented Jul 14, 2014

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

dhiana commented Jul 14, 2014

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 commented Jul 14, 2014

Another workaround:

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

LETTUCE_APPS = tuple(LETTUCE_APPS)
Collaborator

danni commented Jul 23, 2015

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.

Collaborator

danni commented Jul 23, 2015

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 pushed a commit to infoxchange/aloe that referenced this issue Jul 23, 2015

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.

koterpillar added a commit to aloetesting/aloe that referenced this issue Sep 6, 2015

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment