
python-nose - issue #197
ImportError running nosetests on namespace package in subdirectory on Windows
On Windows only, if I create a project that shares a namespace with another project on which it depends, and the child project has a packages=find_packages('src') parameter, the tests will fail when they reference the parent project.
I originally described the problem at http://mail.python.org/pipermail/distutils-sig/2008-June/009595.html
What steps will reproduce the problem? 1. On Windows XP or Vista, install Python 2.5.2, setuptools==0.6c8 2. Expand the two packages, myns.projA and myns.projB from zip file (attached) 3. in myns.projA run "python setup.py develop" 3.5. Install nose manually; there appears to be a different issue if nose is installed via tests_require= during the same step as the tests are run. 4. In myns.projB run "python setup.py test"
What is the expected output? What do you see instead? nosetests should run with "Ran 0 tests in 0.032s" (or similar). Instead, the error is received:
"C:\Users\jaraco\projects\distutils_namespace\myns.projB\test\testBasic.py", line 1, in <module> from myns.projA.modA import funcA ImportError: No module named projA.modA
Please reference the distutils-sig thread for more details. Attached you will find the two sample projects that reproduce the issue.
- myns.zip 15.02KB
Comment #1
Posted on Jul 3, 2008 by Happy RhinoHere's the traceback
Traceback (most recent call last): File "c:\program files (x86)\python\lib\site-packages\nose-0.10.3-py2.5.egg\nose\loader.py", line 364, in loadTestsFromName addr.filename, addr.module) File "c:\program files (x86)\python\lib\site-packages\nose-0.10.3-py2.5.egg\nose\importer.py", line 39, in importFromPath return self.importFromDir(dir_path, fqname) File "c:\program files (x86)\python\lib\site-packages\nose-0.10.3-py2.5.egg\nose\importer.py", line 84, in importFromDir mod = load_module(part_fqname, fh, filename, desc) File "C:\Users\jaraco\projects\distutils_namespace\myns.projB\test\testBasic.py", line 1, in from myns.projA.modA import funcA ImportError: No module named projA.modA
Comment #2
Posted on Jul 3, 2008 by Happy RhinoI did confirm that this problem is exhibited with a default Python 2.5.2 install on Windows (to the C:\Python25 directory), so the issue is not with the space in the installation filename, as observed in the traceback.
Comment #3
Posted on Nov 16, 2008 by Happy RhinoThis problem still exists in the trunk. I tried running nosetests with logging in nose.importer, but it doesn't illuminate the problem terribly much. It does indicate that the importer is not responsible for importing myns.projA.modA, but only testBasic.
Can you suggest a test I can use to elicit the cause?
Sincerely, Jason
PS C:\Users\jaraco\projects\myns.projB> nosetests
E
ERROR: Failure: ImportError (No module named projA.modA)
Traceback (most recent call last): File "C:\Users\jaraco\projects\nose\nose\loader.py", line 364, in loadTestsFromName addr.filename, addr.module) File "C:\Users\jaraco\projects\nose\nose\importer.py", line 39, in importFromPath return self.importFromDir(dir_path, fqname) File "C:\Users\jaraco\projects\nose\nose\importer.py", line 84, in importFromDir mod = load_module(part_fqname, fh, filename, desc) File "C:\Users\jaraco\projects\myns.projB\test\testBasic.py", line 1, in from myns.projA.modA import funcA ImportError: No module named projA.modA
Ran 1 test in 0.062s
FAILED (errors=1) PS C:\Users\jaraco\projects\myns.projB> nosetests -l nose.importer nose.importer: DEBUG: Add path C:\Users\jaraco\projects\myns.projB nose.importer: DEBUG: insert C:\Users\jaraco\projects\myns.projB into sys.path nose.importer: DEBUG: Add path C:\Users\jaraco\projects\myns.projB nose.importer: DEBUG: Add path C:\Users\jaraco\projects\myns.projB\src nose.importer: DEBUG: Import myns from C:\Users\jaraco\projects\myns.projB\src nose.importer: DEBUG: Add path C:\Users\jaraco\projects\myns.projB\src nose.importer: DEBUG: find module part myns (myns) in ['C:\Users\jaraco\projects\myns.projB\src'] nose.importer: DEBUG: sys.modules has myns as nose.importer: DEBUG: module already loaded? mod: c:\users\jaraco\projects\myns.proja new: C:\Users\jaraco\projects\myns .projB\src nose.importer: DEBUG: Add path C:\Users\jaraco\projects\myns.projB\src\myns nose.importer: DEBUG: Add path C:\Users\jaraco\projects\myns.projB\src nose.importer: DEBUG: Import myns.projB from C:\Users\jaraco\projects\myns.projB\src nose.importer: DEBUG: Add path C:\Users\jaraco\projects\myns.projB\src nose.importer: DEBUG: find module part myns (myns) in ['C:\Users\jaraco\projects\myns.projB\src'] nose.importer: DEBUG: sys.modules has myns as nose.importer: DEBUG: module already loaded? mod: C:\Users\jaraco\projects\myns.projB\src new: C:\Users\jaraco\projects\ myns.projB\src nose.importer: DEBUG: find module part projB (myns.projB) in ['C:\Users\jaraco\projects\myns.projB\src\myns'] nose.importer: DEBUG: Add path C:\Users\jaraco\projects\myns.projB\src\myns\projB nose.importer: DEBUG: Add path C:\Users\jaraco\projects\myns.projB\src\myns nose.importer: DEBUG: Add path C:\Users\jaraco\projects\myns.projB\src nose.importer: DEBUG: Add path C:\Users\jaraco\projects\myns.projB\test nose.importer: DEBUG: insert C:\Users\jaraco\projects\myns.projB\test into sys.path nose.importer: DEBUG: Import testBasic from C:\Users\jaraco\projects\myns.projB\test nose.importer: DEBUG: Add path C:\Users\jaraco\projects\myns.projB\test nose.importer: DEBUG: find module part testBasic (testBasic) in ['C:\Users\jaraco\projects\myns.projB\test'] Enose.importer: DEBUG: Remove path C:\Users\jaraco\projects\myns.projB\test nose.importer: DEBUG: Remove path C:\Users\jaraco\projects\myns.projB\src
======================================================================
ERROR: Failure: ImportError (No module named projA.modA)
Traceback (most recent call last): File "C:\Users\jaraco\projects\nose\nose\loader.py", line 364, in loadTestsFromName addr.filename, addr.module) File "C:\Users\jaraco\projects\nose\nose\importer.py", line 39, in importFromPath return self.importFromDir(dir_path, fqname) File "C:\Users\jaraco\projects\nose\nose\importer.py", line 84, in importFromDir mod = load_module(part_fqname, fh, filename, desc) File "C:\Users\jaraco\projects\myns.projB\test\testBasic.py", line 1, in from myns.projA.modA import funcA ImportError: No module named projA.modA
Ran 1 test in 0.102s
FAILED (errors=1)
Comment #4
Posted on Mar 28, 2009 by Quick Panda(No comment was entered for this change.)
Comment #5
Posted on Mar 29, 2009 by Quick PandaThis is fixed in the pycon sprint bitbucket, fix will land on trunk soon. Fix adds an option, --first-package- wins, that will prevent nose from clobbering packages out of sys.modules when it encounters a package with the same name at a different path.
Comment #6
Posted on Apr 19, 2009 by Quick PandaClosing tickets finished in pycon sprint, now that code has landed on trunk.
Comment #7
Posted on Nov 15, 2009 by Happy RhinoThis problem appears not to be fixed. The --first-package-wins parameter appears to prevent the tests from running at all.
PS C:\Users\jaraco\projects\myns\myns.projB> nosetests --first-package-wins 1
Ran 0 tests in 0.006s
And the behavior is the same when the tests are run without --first-package-wins
PS C:\Users\jaraco\projects\myns\myns.projB> nosetests
E
ERROR: Failure: ImportError (No module named projA.modA)
Traceback (most recent call last): File "C:\python\lib\site-packages\nose-0.11.1-py2.6.egg\nose\loader.py", line 379, in loadTestsFromName addr.filename, addr.module) File "C:\python\lib\site-packages\nose-0.11.1-py2.6.egg\nose\importer.py", line 39, in importFromPath return self.importFromDir(dir_path, fqname) File "C:\python\lib\site-packages\nose-0.11.1-py2.6.egg\nose\importer.py", line 86, in importFromDir mod = load_module(part_fqname, fh, filename, desc) File "C:\Users\jaraco\projects\myns\myns.projB\test\testBasic.py", line 1, in from myns.projA.modA import funcA ImportError: No module named projA.modA
Ran 1 test in 0.011s
FAILED (errors=1)
Comment #8
Posted on Nov 15, 2009 by Happy RhinoIn the previous report, I'm running Python 2.6.4, distribute 0.6.8, and nose 0.11.1.
Comment #9
Posted on Nov 26, 2009 by Swift PandaI had the same problem with nosetests ImportError and in my case, the problem was due to the way I named my working directory. The ImportError concerned the function create_engine from sqlalchemy and the directory in which I ran nosetests was also called sqlalchemy. After changing the name of the working dir, nosetests run without problems
Comment #10
Posted on Nov 26, 2009 by Happy Rhinovm: Thanks for the information. Other people who encounter this error may find the information useful.
To be clear, in this issue, the 'myns' name does conflict intentionally because of the way namespace packages work in Python. In your sqlalchemy scenario, sqlalchemy is not meant to be referenced as a package in you suite, so it's important not to create a folder that conflicts with the real package name. In my example, the myns namespace package does exist in two locations and since both packages are registered with setuptools/distribute, both myns.projA and myns.projB should be available.
Comment #11
Posted on Mar 25, 2010 by Quick OxI'm seeing the same failures in another application. I believe that jaracosan is correct, this bug does not appear to be fixed; I vote that its status be changed.
Comment #12
Posted on Apr 15, 2011 by Massive ElephantOne year later and still a problem for me too in nose-1.0.0 on Python 2.6
Comment #13
Posted on Apr 20, 2011 by Massive ElephantThe problem in my case is that in 2 places, nose checks 2 filename/path strings for equality, but they differ in case (and notably, only the drive-letter portion of the case differs.)
in importer.py, the function sameModule has:
if mod_path == new_path: return True
that is one place where the paths are identical except the the first letter, so the function returns False. That problem is worked-around by using the firstPackageWins option - the failing string compare is skipped. Which then brings us to the second problem:
In loader.py in loadTestsFromModule(), we wind up in the block:
if (self.config.traverseNamespace or not path) or \ os.path.realpath(module_path).startswith(path): tests.extend(self.loadTestsFromDir(module_path))
again, module_path and path differ only by the case of the first character (the drive letter). So no tests are found in the package.
So I dug into where the difference in case actually comes from. It turns out my easy_install.pth file has specified the lower-case version of the path for the failing package, whereas os.path.abspath will return an upper-case path if no path exists. ie:
os.path.abspath("\src") 'O:\src' os.path.abspath("o:\src") 'o:\src'
So nose tends to wind up with the upper-case version of the drive letter, but the existing entry in sys.modules has the lower-case version courtesy of the .pth file. Ideally nose should work around this, but a work-around for me (and probably for others) is simply to edit easy-install.pth to specify all upper-case drive letters and the problem is resolved.
Comment #14
Posted on Mar 28, 2012 by Massive GiraffeI have exactly the same problem with the last nose version 1.1.2, python 2.7 and Linux. It's not a windows specific problem to me. As jaracosan pointed it, the problem occurs with a namespaced package splitted in two different directories.
The --first-package-wins parameter option just change the error you get.In in my case I have a myns package split accross two directories: /path1/myns and /path2/myns.
Running nosetests from within the /path2/myns directory raise an import error when trying to import a module located in /path1/myns. Running nosetests from within the /path2/myns directory and with the --first-package-wins parameter raise an import error when trying to import a module located in /path2/myns.
Comment #15
Posted on Mar 28, 2012 by Quick Pandanose development has moved to github. you can find this issue there: https://github.com/nose-devs/nose/issues/19 (I just re-opened it since it seems the problem is still happening)
Comment #16
Posted on Mar 28, 2012 by Massive GiraffeOk thanks. Do you want me to move my comments to github ?
Comment #17
Posted on Mar 28, 2012 by Quick PandaSure, please do. Thanks!
Comment #18
Posted on Mar 28, 2012 by Massive GiraffeOk I did it. I'm gonna try to provide a functionnal test that reproduces the problem. I saw that there are already some functionnal tests on namespace packages...
Thanks.
Status: Fixed
Labels:
Type-Defect
Priority-Medium
Milestone-Release0.11.0
OpSys-Windows
OpSys-OSX