Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

py3 regression causing ImportError #839

Closed
jayvdb opened this issue Sep 10, 2014 · 1 comment
Closed

py3 regression causing ImportError #839

jayvdb opened this issue Sep 10, 2014 · 1 comment

Comments

@jayvdb
Copy link

jayvdb commented Sep 10, 2014

With the following files:

  • a/ _ _ init _ _ .py (empty)
  • a/foo.py (empty)
  • a/bar.py:
        import foo
  • tests/foo_tests.py
        import unittest
        from a import foo
  • tests/bar_tests.py
        import unittest
        from a import bar
$ nosetests-2.7

...
Ran 0 tests...

$ nosetests-3.4 --debug=nose.importer
nose.importer: DEBUG: Add path /home/jayvdb/tmp/nosetest-import-issue
nose.importer: DEBUG: insert /home/jayvdb/tmp/nosetest-import-issue into sys.path
nose.importer: DEBUG: Add path /home/jayvdb/tmp/nosetest-import-issue
nose.importer: DEBUG: Import a from /home/jayvdb/tmp/nosetest-import-issue
nose.importer: DEBUG: Add path /home/jayvdb/tmp/nosetest-import-issue
nose.importer: DEBUG: find module part a (a) in ['/home/jayvdb/tmp/nosetest-import-issue']
nose.importer: DEBUG: Add path /home/jayvdb/tmp/nosetest-import-issue/a
nose.importer: DEBUG: Add path /home/jayvdb/tmp/nosetest-import-issue
nose.importer: DEBUG: Add path /home/jayvdb/tmp/nosetest-import-issue/tests
nose.importer: DEBUG: insert /home/jayvdb/tmp/nosetest-import-issue/tests into sys.path
nose.importer: DEBUG: Import bar_tests from /home/jayvdb/tmp/nosetest-import-issue/tests
nose.importer: DEBUG: Add path /home/jayvdb/tmp/nosetest-import-issue/tests
nose.importer: DEBUG: find module part bar_tests (bar_tests) in ['/home/jayvdb/tmp/nosetest-import-issue/tests']
Enose.importer: DEBUG: Import foo_tests from /home/jayvdb/tmp/nosetest-import-issue/tests
nose.importer: DEBUG: Add path /home/jayvdb/tmp/nosetest-import-issue/tests
nose.importer: DEBUG: find module part foo_tests (foo_tests) in ['/home/jayvdb/tmp/nosetest-import-issue/tests']
nose.importer: DEBUG: Remove path /home/jayvdb/tmp/nosetest-import-issue/tests

======================================================================
ERROR: Failure: ImportError (No module named 'bar')
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/nose-1.3.4-py3.4.egg/nose/failure.py", line 39, in runTest
    raise self.exc_val.with_traceback(self.tb)
  File "/usr/local/lib/python3.4/dist-packages/nose-1.3.4-py3.4.egg/nose/loader.py", line 414, in loadTestsFromName
    addr.filename, addr.module)
  File "/usr/local/lib/python3.4/dist-packages/nose-1.3.4-py3.4.egg/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/local/lib/python3.4/dist-packages/nose-1.3.4-py3.4.egg/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/usr/lib/python3.4/imp.py", line 235, in load_module
    return load_source(name, filename, file)
  File "/usr/lib/python3.4/imp.py", line 171, in load_source
    module = methods.load()
  File "<frozen importlib._bootstrap>", line 1220, in load
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1448, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/home/jayvdb/tmp/nosetest-import-issue/tests/bar_tests.py", line 2, in <module>
    from a import bar
  File "/home/jayvdb/tmp/nosetest-import-issue/a/bar.py", line 1, in <module>
    import bar
ImportError: No module named 'bar'

----------------------------------------------------------------------
Ran 1 test in 0.048s

FAILED (errors=1)

_ _ init _ _ .py can include _ _ name _ _ ; same result

For the real scenario encountered, see https://gerrit.wikimedia.org/r/#/c/159410/

wmfgerrit pushed a commit to wikimedia/pywikibot that referenced this issue Sep 10, 2014
py3 nosetests fails on 'import upload' in the
data_ingestion script, causing a travis build breakage.

Two other scripts use 'import upload'
- flickrripper
- imagetransfer

And other use 'import <script>' syntax, which will
also fail on py3 nosetests when we have tests for
them.

This change only updates data_ingestion to use
the 'from scripts import upload' import style, to fix
the build breakage while waiting on feedback from nosetests
nose-devs/nose#839

Also update data_ingestion tests to use the new test metaclass.

Change-Id: If40087657718749dd54a37834d551f97424b4914
@jszakmeister
Copy link
Contributor

Up above, you say a/bar.py has this:

import foo

But in the error message, it says:

File "/home/jayvdb/tmp/nosetest-import-issue/a/bar.py", line 1, in <module>
    import bar
ImportError: No module named 'bar'

Which shows us that a/bar.py contains import bar instead of import foo. So that's one problem. But assuming it really should be import foo, the other issue is that Python 3.4 does not do relative imports by default. So the import foo in a/bar.py won't work correctly. If you want to keep the relative feel of it, you need to write it as from . import foo, or write it out absolutely as import a.foo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants