1

I have a generic issue with IronPython module import processing.

Given a Python package, submodules can refer to each other with relative references. Assume the package is called Poultry and submodules within it are Chicken.py and Duck.py. Chicken can then import Duck simply as "import Duck" and vica versa. A Python script loaded by the application can import the package correctly, can use the submodules and everything works fine.

Directory structure of package looks like this:

+ - Poultry
| - __init__.py
| - Duck.py
L - Chicken.py

However, when I open Duck.py with my application for instance, I can't run and debug it as IronPython throws an import error on line "import Chicken" with the message "No module name Chicken".

I understand this is not strictly an issue with IronPython but the hosting application. In the end the question is, how could I modify my hosting application to process these import statements correctly ? Shall I set some path variable in scope, or what ? How could I make the Python engine understand that the the currently loaded file is the part of a specific package ?

Any ideas are welcome :-)

2 Answers2

0

You have to always import Duck as part of its package. Inside an application, always do either of:

import Poultry.Duck
from Poultry import Duck

If you run it directly with an interpreter, you must use the -m switch as

python -m Poultry.Duck
MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
  • Thanks for your quick answer MisterMiyagi. In fact I have no problem importing Poultry.Duck into an application, it works just fine. The issue is when I open Duck.py, IronPython can't handle the import statement of "import Chicken.py" present in Duck.py. – user6165992 Jul 07 '16 at 10:14
0

You still need to import Chicken by typing from Poultry import Chicken. You can't just do import Chicken. It would be the same from any file in your project.

David
  • 131
  • 4
  • 9