python / python/cpython

Allow relative imports in the `__main__` module

Aperta
#126,926 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Feature or enhancement

Proposal:

Expanding on https://github.com/python/cpython/issues/109853#issuecomment-2076845977

@zooba, were you envisioning to do this for all scenarios where a __main__ module is exists? I think that makes sense for consistency, but we may want to consider all scenarios to make sure the UX makes sense.

AFAICT, these should be all of them:

(this refers to relative imports in the same directory, it does not cover relative imports to the parent directory)

  1. Running a package module via python -m <module>
    • Relative imports already work here, no changes needed.
  2. Running a source or extension module via python -m <module>
    • Relative imports don't work here.
    • Allowing relative imports here feels a bit weird, especially if we were to enable -P by default. For it to run, the module would have to be in sys.path, meaning it probably installed rather than being provided by the user, so there isn't much necessity for relative imports.
  3. Running a package module directly (python <module.zip>)
    • Relative imports already work here, no changes needed.
  4. Running a file directly (python <script.py>)
    • Relative imports don't work here.
  5. Running a command string via python -c <command>
    • Relative imports don't work here.
    • There's no canonical base directory to use as a reference, should we use the current directory?
  6. Running a command string via the stdin
    • Relative imports don't work here.
    • There's no canonical base directory to use as a reference, should we use the current directory?
  7. Running the repl
    • Relative imports don't work here.
    • There's no canonical base directory to use as a reference, should we use the current directory?

I think 4), 5), 6), and 7) are the only use-cases that call for relative imports to work. 2) feels a bit weird, but it may be worth supporting for consistency.

I played a bit with the code, and the implementing this doesn't seem much difficult. I was able to write a working PoC targeting only 4) with much difficulty, so I don't think that's a worry.

Considering this, do we think it makes sense to make this change?
If so, should it cover all scenarios, or do we want to keep some of them as-is?

For further reflection, how much of an actual improvement would this actually be over the current behavior of adding the current directory to sys.path?
I can't help but feel a bit like we are trading one weird behavior for another.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia leggendo la discussione collegata nell’issue 109853 e confronta i sette scenari di esecuzione di main elencati qui, in particolare i file diretti, -c, stdin e la REPL. Determina quali casi dovrebbero supportare le importazioni relative, quale directory dovrebbe fungere da riferimento quando non esiste una base canonica e se il comportamento migliora aggiungendo la directory corrente a sys.path. Il lavoro è completo quando l’ambito supportato e l’esperienza utente risultante sono concordati prima dell’implementazione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.