سؤال

This is a simplification of a problem that exists in a complex project. In a folder called root_test, I have a folder called test, containing tester.py. I also have (in test) a modules directory (empty) containing a lib directory, which holds logger.py. Dir structure below.

|-root_test
|---test/
|-----tester.py
|-----__init__.py
|-----modules/
|-------__init__.py
|-------lib/
|---------__init__.py
|---------logger.py

alternatively, run from root_test:

 $ ls
test

 $ ls test/
__init__.py  modules      tester.py

 $ ls test/modules/
__init__.py  lib

 $ ls test/modules/lib/
__init__.py  logger.py

tester.py is as follows:

#!/usr/bin/env python

import sys, os

# allow running without installing
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))

import test.modules.lib.logger

but when I try and run it from root_test dir, I get the following error:

 $ python test/tester.py 
Traceback (most recent call last):
  File "test/tester.py", line 8, in <module>
    import test.modules.lib.logger
ImportError: cannot import name logger

This doesn't happen on my other laptop, and their $PYTHONPATHs are identical:

 $ echo $PYTHONPATH
/usr/local/lib/python2.7/dist-packages/:/usr/local/lib/python2.7/site-packages/:
هل كانت مفيدة؟

المحلول

The solution was that there was a module installed called tester. And even though I was explicitly running python ~/test/tester.py, it still ran the installed module instead. Removing that module fixed the problem.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top