[issue39473] Enable import behavior consistency option
DarkTrick
report at bugs.python.org
Tue Jan 28 02:17:04 EST 2020
New submission from DarkTrick <notebook22312 at gmail.com>:
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.
----------
components: Interpreter Core
messages: 360839
nosy: bluelantern
priority: normal
severity: normal
status: open
title: Enable import behavior consistency option
type: enhancement
versions: Python 3.9
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39473>
_______________________________________
More information about the Python-bugs-list
mailing list