Title: Enable import behavior consistency option
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.9
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
`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       # 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:

|    |_________________________________
|    | from subdir.funca import funcA  |
|    | funca()                         |
|    |_________________________________|
      |       |____________________________
      |       | from .funcb import funcB   |
      |       | def funcA():               |
      |       |    funcb()                 |
      |       |____________________________|
              | def funcB():               |
              |    print("funcB")          |

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

(B) The following call will succeed:

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

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

(E) The following call will not succeed:

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`  
`Option relImports`

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