0

I have a python package "fb" with the following file structure:

packageFolder
├── README.md
├── setup.py
└── fb
    ├── __init__.py
    ├── foo.py
    └── fizz.py

__init__.py contains the following code:

from fb.foo import bar
from fb.fizz import buzz

The issue is that foo.py also needs to import buzz from fizz.py

For the PyPI install to work, foo.py needs to contain the line:

from fb.fizz import buzz

But for things to run locally for development, foo.py needs to instead contain:

from fizz import buzz

Reversing the import statements leads to a module import error in both cases.

What is the resolution to this issue?

Seth
  • 193
  • 1
  • 8
  • How are you doing the local development — specifically, how have you installed your package locally? Are you using Python 2 or 3? – jwodder Apr 05 '21 at 20:37
  • I am using Python3. I have not installed my package locally, so local development runs the .py files directly and there is no danger of conflict. I only installed my package in a virtual environment to test that it works. – Seth Apr 05 '21 at 20:38
  • When you run things locally, the full path e.g. `from fb.fizz import buzz` should work, when you run everything from `packageFolder` -- or if `packageFolder` is in `sys.path` (check the PYTHONPATH environment variable) – ELinda Apr 05 '21 at 20:49
  • @ELinda when I run "python fb/foo.py" from packageFolder with the import statement "from fb.fizz import buzz" I get "ModuleNotFoundError: No module named 'fb'". Is this expected? – Seth Apr 05 '21 at 21:25
  • Ok I've made some progress using "from fb.fizz import buzz" in foo.py under the following conditions: If I run "python -m fb.foo" from packageFolder it works although it gives a confusing warning message. Also if instead of running foo.py directly, I create a file example.py in packageFolder with "from fb import bar" and run it from packageFolder using "python example.py" then it works also. Still I find this very confusing and it isn't clear what the best practice is. – Seth Apr 05 '21 at 22:49
  • this is a good explanation https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time – Seth Apr 05 '21 at 22:58

0 Answers0