简体   繁体   中英

Fabric load fabfile as a module

My fabfile contains relative imports, thus it has to be loaded as a module.

It seems that fab loads the fabfile as standalone script, so the relative imports are not working.

Here is my folder structure:

scripts
 |-> __init__.py 
 |-> deployment
      |-> __init__.py
      |-> fabfile.py
 |-> other-module

To debug/test the fabfile I can load it using:

python -m scripts.deployment.fabfile

Is there a way to force the fab tool to load the fabfile as module?

In order to make that happen, you need to add a scripts/deployment/__main__.py .

scripts
 |-> __init__.py 
 |-> deployment
      |-> __init__.py
      |-> __main__.py
      |-> fabfile.py
 |-> other-module

Supposing there's an entrypoint function inside your scripts/deployment/fabfile.py called my_fabric_entry , you can call it from scripts/deployment/__main__.py and it will do the trick.

$ cat scripts/deployment/__main__.py
from __future__ import print_function
from .fabfile import my_fabric_entry

print('About to call fabfile.my_fabric_entry()')
my_fabric_entry()

$ python -m scripts.deployment
About to call fabfile.my_fabric_entry()

Check this answer for more info https://stackoverflow.com/a/36320295/977593

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM