Not appending ("lib") to sys.path breaks tests.

Leam Hall leamhall at gmail.com
Fri Sep 8 17:18:45 EDT 2017


A kind soul pointed out that my code uses a sys.path.append("lib") to 
get files to be imported:

	sys.path.append("lib")
	from character_tools import *

He noted that having an __init__.py in lib and using:

	from .character_tools import *

Should be sufficient for "please don't comment yet about 'import *'" 
levels of sufficient.   :P

The code works under python 2.6.6 and 3.6.2. However, py.test (python 2) 
and pytest (python 3) fails. Besides my usual clue, what am I missing?



### py.test (python 2)
========================================= test session starts 
=========================================
platform linux2 -- Python 2.6.6 -- pytest-2.3.5
collected 0 items / 3 errors

=============================================== ERRORS 
================================================
______________________________ ERROR collecting tests/test_base_tools.py 
______________________________
tests/test_base_tools.py:6: in <module>
 >   import lib.base_tools
E   ImportError: No module named lib.base_tools
______________________________ ERROR collecting tests/test_character.py 
_______________________________
/usr/lib/python2.6/site-packages/_pytest/python.py:352: in _importtestmodule
 >           mod = self.fspath.pyimport(ensuresyspath=True)
/usr/lib/python2.6/site-packages/py/_path/local.py:621: in pyimport
 >           __import__(modname)
E             File 
"/home/leam/lang/git/makhidkarun/py_tools/tests/test_character.py", line 6
E               import .lib.character
E                      ^
E           SyntaxError: invalid syntax
___________________________ ERROR collecting 
tests/test_character_tools.py ____________________________
tests/test_character_tools.py:6: in <module>
 >   from ..lib.character_tools import *
E   ValueError: Attempted relative import in non-package
======================================= 3 error in 0.05 seconds 
=======================================



### pytest (python 3)

========================================= test session starts 
=========================================
platform linux -- Python 3.6.2, pytest-3.2.2, py-1.4.34, pluggy-0.4.0
rootdir: /home/leam/lang/git/makhidkarun/py_tools, inifile:
collected 0 items / 3 errors 


=============================================== ERRORS 
================================================
______________________________ ERROR collecting tests/test_base_tools.py 
______________________________
ImportError while importing test module 
'/home/leam/lang/git/makhidkarun/py_tools/tests/test_base_tools.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_base_tools.py:6: in <module>
     import lib.base_tools
E   ModuleNotFoundError: No module named 'lib'
______________________________ ERROR collecting tests/test_character.py 
_______________________________
/usr/local/lib/python3.6/site-packages/_pytest/python.py:395: in 
_importtestmodule
     mod = self.fspath.pyimport(ensuresyspath=importmode)
/usr/local/lib/python3.6/site-packages/py/_path/local.py:662: in pyimport
     __import__(modname)
E     File 
"/home/leam/lang/git/makhidkarun/py_tools/tests/test_character.py", line 6
E       import .lib.character
E              ^
E   SyntaxError: invalid syntax
___________________________ ERROR collecting 
tests/test_character_tools.py ____________________________
tests/test_character_tools.py:6: in <module>
     from ..lib.character_tools import *
E   ValueError: attempted relative import beyond top-level package
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 3 errors during collection 
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================= 3 error in 0.22 seconds 
=======================================



More information about the Python-list mailing list