开发者

ImportError using nose, no ImportError using raw unittest?

开发者 https://www.devze.com 2022-12-30 04:45 出处:网络
I get an ImportError when running my unittests using Nose and I don\'t when I just run it standalone. All files referred to here may be seen at http://gist.github.com/395541# .

I get an ImportError when running my unittests using Nose and I don't when I just run it standalone. All files referred to here may be seen at http://gist.github.com/395541# .

If I run the test script, importTest-Test.py, directly I get this output:

C:\usr\x\data\src\Python\mmm>python importTest-Test.py
In mmdb
In BusinessLogic
[]
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

If I allow Nose to run it I get an error :

C:\usr\x\data\src\Python\mmm>nosetests.exe
E
======================================================================
ERROR: Failure: ImportError (No module named mmdb.DataAccess.AttemptDB)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\bin\installed\python2.6\lib\site-packages\nose-0.11.3-py2.6.egg\nose\loader.py", line 382, in loadTestsFromName
    addr.filename, addr.module)
  File "c:\bin\installed\python2.6\lib\site-packages\nose-0.11.3-py2.6.egg\nose\importer.py", line 39, in importFromPath
    return self.im开发者_开发技巧portFromDir(dir_path, fqname)
  File "c:\bin\installed\python2.6\lib\site-packages\nose-0.11.3-py2.6.egg\nose\importer.py", line 86, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "C:\usr\x\data\src\Python\mmm\importtest-Test.py", line 2, in <module>
    import importtest
  File "C:\usr\x\data\src\Python\mmm\importtest.py", line 1, in <module>
    from mmdb.BusinessLogic.AttemptManager import AttemptManager
  File "C:\usr\x\data\src\Python\mmm\mmdb\BusinessLogic\AttemptManager.py", line 1, in <module>
    from mmdb.DataAccess.AttemptDB import AttemptDB
ImportError: No module named mmdb.DataAccess.AttemptDB

----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (errors=1)

The files involved in the package which nose is having difficulties with are in the following structure - some may be seen here http://gist.github.com/395541# .:

mmm\importtest-Test.py
mmm\importtest.py
mmm\mmdb
mmm\__init__.py
mmm\mmdb\BusinessLogic
mmm\mmdb\BusinessObject
mmm\mmdb\DataAccess
mmm\mmdb\__init__.py
mmm\mmdb\BusinessLogic\AttemptManager.py
mmm\mmdb\BusinessLogic\Collections
mmm\mmdb\BusinessLogic\__init__.py
mmm\mmdb\BusinessLogic\Collections\__init__.py
mmm\mmdb\BusinessObject\__init__.py
mmm\mmdb\DataAccess\AttemptDB.py
mmm\mmdb\DataAccess\__init__.py

This is happening on Win32 / Python 2.6 / Nose 0.11.3 .

I'd be grateful for any help.

thanks.


By default, nose manipulates the PYTHONPATH it uses. You might try turning off this behavior using the -P switch.


You could try to remove __init__.py from the top level folder mmm (original answer here: https://stackoverflow.com/a/3073368/19166)


Looks like your problem could be due to the dash "-" in your filename. See also: https://stackoverflow.com/a/11055442/1063605


This is an answer for a very specific use case involving PyUnit.

I had a set of unit tests running fine under PyDev. One day, I made a typing error, and PyDev added an automatic import for a part of the pandas package. I usually keep my code folded so I didn't see this right away.

The error that appeared in a later part of the test set was "Error: could not import nose".

In debugging, I found that a data file name had one of the subdirectory names repeated. It appeared that the test runner was changing the working directory to the subdirectory containing the .py file, but not going back to the project directory. A call to os.path.realpath(" _ file _ ") to set up the data file path was returning the test subdirectory instead of the expected project directory, with the net effect that the data wasn't found and the test failed.

"Fixing" the code that set up the data file path resolved this error. However, as I worked my way back through the remaining errors, I discovered and removed the unwanted import statement. At that point, the data file path started giving errors, so I changed it back to the original form and everything was fine.

So... if you find that you are suddenly getting these "nose" errors, it may be that your editing has inadvertently introduced an import statement that throws PyUnit out of whack.

I'm adding the answer here because part of my experience was running the individual test files with raw unittest (from the PyDev context menu), and feeling very puzzled as to why they worked that way but not when run by the full test runner.

0

精彩评论

暂无评论...
验证码 换一张
取 消