Importing Python modules from a scripts directory beside the tasks directory

Aperta
#994 1 commento 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
38/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python
Ambito
cli, tooling

Direzione di ricerca

Reproduce the import failure with the shown scripts and tasks layout, starting from tasks/init.py and the loader path in invoke/loader.py. Compare behavior with Invoke 2.0.0 and the latest version; done means from scripts import useful_script_1 works from an Invoke task without the ImportError.

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

Descrizione

We use invoke in a directory structure which looks like this:

-- scripts
   -- __init__.py
   -- useful_script_1.py
   -- useful_script_2.py
   -- ...
-- tasks
   -- __init__.py
   -- task_1.py
   -- task_2.py

In our invoke tasks we import stuff from the scripts directory like this:

from scripts import useful_script_1

Some time between version 2.0.0 of invoke and the latest version something changed in invoke which stopped this working; we get the error:

ImportError: cannot import name 'useful_script_1' from 'scripts' (unknown location)

We have made this work up to now by sticking with version 2.0.0 of invoke but it would be better to adopt the latest version: what is the simplest way to make script-import work from an invoke task again?

The exact error with backtrace is:

Traceback (most recent call last):
  File "c:\program files\python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\program files\python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Program Files\Python39\Scripts\inv.exe\__main__.py", line 7, in <module>
  File "c:\program files\python39\lib\site-packages\invoke\program.py", line 387, in run
    self.parse_collection()
  File "c:\program files\python39\lib\site-packages\invoke\program.py", line 479, in parse_collection
    self.load_collection()
  File "c:\program files\python39\lib\site-packages\invoke\program.py", line 716, in load_collection
    module, parent = loader.load(coll_name)
  File "c:\program files\python39\lib\site-packages\invoke\loader.py", line 91, in load
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 855, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "c:\projects\my_project\tasks\__init__.py", line 2, in <module>
    from scripts import useful_script_1
ImportError: cannot import name 'useful_script_1' from 'scripts' (unknown location)
Lingua principale
Python
Stelle
4.8k
Fork
412
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

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.

Altre issue di pyinvoke/invoke

Tutte le issue di pyinvoke/invoke

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.