0

My apologies if this post is a bit inappropriate, but I'm desperate for comments on a programming problem.

I have been frustrated for years about what I believe is a Python design flaw, and many others agree. It's about how import statements work, particularly code like:

from ..mypackage import mymodule
from ..mypackage.mymodule import mymethod
from .. import mypackage

and similar statements.

Only the most simple, contrived, canonical cases actually work. Anything else results in the error message:

ImportError: attempted relative import with no known parent package

I use sys.path.append() as a workaround, but that should not be necessary, and it is not portable.

It seems the issue revolves around where Python thinks the importing module is in the file system at the time it attempts to execute the import statements. My opinion is that Python should be able to figure out if it is in a package and exactly where in the file hierachy that is. The import statements should work as expected if the importing module is called from another module, or if it is run from an interpreter, or PyCharm, IDLE, Spyder, or by some other way.

There is a SO post, Relative imports for the billionth time, which addresses this problem. The main article plus 15 answers and 36 comments indicate that this issue has been around for a long time. Many very smart people have offered exotic explanations and proposed cumbersome solutions to an issue that should not exist, and yet the powers that control the development of Python have not moved on the matter. It should not be necessary to be knee deep in Python internals in order to write a simple application. That's whole idea of a high level language; this is not C++.

Someone reading this must have influence with Python developers. Please use it.

Any comments, please.

fossildoc
  • 31
  • 5
  • There are two common cases: You're importing from a system module found in `sys.path`, or you're importing from a module that's packaged with the script. Relative paths handle the second case. – Barmar May 19 '22 at 17:46
  • 1
    Is your project properly packaged and installed? – juanpa.arrivillaga May 19 '22 at 17:47
  • @Barmar It shouln't make a difference, which is my point. When a .. is encountered, Python should know where it is and should look at the parent directory which, if it is a package, should deliver the requested module. Everything else is irrelevant. – fossildoc May 19 '22 at 18:19
  • @juanpa What is 'properly package and installed'? That's nonsense; a program is a program. It's a main module and all of its referenced modules, classes, and functions. How it got there is completely irrelevant. – fossildoc May 19 '22 at 18:20
  • @fossildoc as in, you defined the package in a `my_package/setup.cfg` or `my_package/setup.py` (and nowadays, with a `pyproject.toml` as well) and then did `pip install my_package`. – juanpa.arrivillaga May 19 '22 at 18:23
  • @juanpa I have no idea what any of that means. I wrote a program on my PC for local use only. No pip, no install, no packaging for distribution, etc. None of this has anything to do with my post. A package is a directory which contains modules and a __init__.py file (which doesn't seem to be necessary lately). Pip has nothing to do with anything. – fossildoc May 19 '22 at 18:27
  • @fossildoc yea, see, then you need to add the package to the path. If you *don't* want to actually package your package, then you can try to set the `PYTHONPATH='/path/to/package'` – juanpa.arrivillaga May 19 '22 at 18:30
  • You, on one hand, complain that `sys.path.append` is *not portable*, you know what *would make everything portable*? Actually packaging your package – juanpa.arrivillaga May 19 '22 at 18:32
  • @juanpa I know about PYTHONPATH. That is needed only when there are no other hints as to where a requested module resides. It is not necessary when relative imports are used. If PYTHONPATH were necessary, then relative import notatoin would be completely redundant. – fossildoc May 19 '22 at 18:32

0 Answers0