1

I have a directory structure that looks like this:

Project
|+-- test.py
|
+>> agents
|  +-- __init__.py
|  +-- TestAgent.py
|    
+>> automation
|  +-- __init__.py
|  |
|  +>> navigators
|      +--__init__.py
|      +--SystemNavigator.py 

Inside _ _ init _ _.py under agents I have the following code:

from TestAgent import TestAgent

And inside SystemNavigator I have:

from agents import TestAgent

When I attempt to run the program, I get:

Traceback (most recent call last):
File "test.py", line 1, in <module>
from navigators import SystemNavigator
File "/Users/jb/Desktop/code/tools/pythontest/automation/navigators/__init__.py", line 1, in <module>
from SystemNavigator import SystemNavigator
File "/Users/jb/Desktop/code/tools/pythontest/automation/navigators/SystemNavigator.py", line 1, in <module>
from agents import TestAgent
ImportError: No module named agents

I setup an identical file structure in a separate directory to double check, and I was able to get this working there. I read around Stack Overflow and elsewhere. I'd like to avoid any changes to sys.path or changes to my directory structure. Based on others suggestions, I tried deleting .pyc files within the directory and also deleting / recreating the _ _ init _ _.py under agents.

Does anyone have advice on where I should look next to troubleshoot? I'd really appreciate it!

  • 2
    Is the `Project` directory on your `sys.path`? If not, you will not be able to import from packages inside it. – BrenBarn Oct 16 '15 at 20:44
  • 1
    I would recommend the use of lowercase filenames. Having __init__.py import TestAgent from TestAgent means that TestAgent has a somewhat ambiguous name. – user3757614 Oct 16 '15 at 21:11
  • see [how-to-import-a-python-class-that-is-in-a-directory-above](http://stackoverflow.com/questions/1054271/how-to-import-a-python-class-that-is-in-a-directory-above) – rebeling Oct 16 '15 at 21:15
  • Thanks for the feedback on this. Adding the directory to the sys.path and the directions on how to import a python class from a directory above are both good options. I appreciate it! – user3821892 Oct 18 '15 at 16:01

0 Answers0