pypa / pypa/packaging.python.org

Suggestion for disambiguation re import expectations and __init__.py

Aperta
#1,929 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Python
Stelle
1.7k
Fork
1.7k
Merge medio
3g 12h
PR unite (30g)
4

Descrizione

Issue Description

Having just gone through the tutorial, I found myself confused because once the project is packaged and installed, and then imported, the add_one() function seemed unavailable.

  • "import example_package" was without error, but attempting to use example_package.add_one(3) gave AttributeError: module 'example_package' has no attribute 'add_one'

  • "from example_package import add_one" gives ImportError: cannot import name 'add_one' from 'example_package'

  • "from example_package import *" runs without error, but then attempting to use add_one gives NameError: name 'add_one' is not defined

The only way I have been able to run "add_one()" has been as
"from example_package.example import add_one".

While this is understandable as the function is defined in the top level example.py file, it definitely violated my expectations as almost every package I use would allow me to use "import example_package" and then "example_package.add_one()" or "from example_package import add_one" and then run "add_one()"

I am suggesting either an example of loading the package and using the function, or the addition of modifying the init.py to add "from .example import add_one"

Code of Conduct
  • I am aware that participants in this repository must follow the PSF Code of Conduct.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

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 individuando i file example_package/example.py e init.py del tutorial e leggendo le istruzioni relative al packaging che li circondano. Decidi se il tutorial debba documentare l’importazione annidata o esporre add_one da init.py, quindi verifica i comandi di importazione documentati rispetto all’esempio pacchettizzato.

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

Valutazione

Stack tecnologico
python
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
52/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.