classification
Title: Enable import behavior consistency option
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: bluelantern
Priority: normal Keywords:

Created on 2020-01-28 07:17 by bluelantern, last changed 2020-01-28 07:17 by bluelantern.

Messages (1)
msg360839 - (view) Author: DarkTrick (bluelantern) Date: 2020-01-28 07:17
Matter:
========
`import`s are not handled the same throughout the different ways of calling.


Current situation:
===================
The resolution of `import` is dependant on the way of calling the script. 
Three ways of calling a script are shown below:
1) python myscript.py       # as script in cwd
2) python -m myscript       # as module in cwd
3) python -m src.myscript   # as module in subpackage of cwd

Given the following situation:

./src
|
|---main.py
|    |_________________________________
|    | from subdir.funca import funcA  |
|    | funca()                         |
|    |_________________________________|
|
|---subdir
      |
      |--- __init__.py
      |
      |--- funca.py
      |       |____________________________
      |       | from .funcb import funcB   |
      |       | def funcA():               |
      |       |    funcb()                 |
      |       |____________________________|
      |
      |
      |--- funcb.py
              |____________________________
              | def funcB():               |
              |    print("funcB")          |
              |____________________________|           

(A) The following call will succeed:
`./src>python -m main`

(B) The following call will succeed:
`./src>python main.py`

(C) The following call will succeed:
`./src>python -m subdir.funca

(D) The following call will not succeed:
`./src>python ./subdir/funca.py

(E) The following call will not succeed:
`./src/subdir>python funca.py


Suggestion:
===========
Supply a functionality / an option that will allow all of A~E to succeed. 

S1) So it doesn't matter, if the script is called with or without the -m option (D)
S2) So a toplevel script can refer to the package it's placed in by ".", even if called direclty (E)


Implementation idea:
=====================
Problem: The current import logic can't be change for compatibility reasons. And maybe it should not. 
Therefore I thought of an option within the python script like
`Option treatAsModule`  
or
`Option relImports`

If such an option would be given, the python interpreter would handle imports differently.
History
Date User Action Args
2020-01-28 07:17:04bluelanterncreate