Python import error :No module named Fabric.api?

Similar issue happens if you have fabfile.py based on older fabric versions, i.e. 1.x. Currently fabric latest version is 2.x which is not backward compatible:

As of the 2.0 release line, Fabric 2 is not at 100% feature parity with 1.x! Some features have been explicitly dropped, but others simply have not been ported over yet,

Regarding fabric.api - it does not exist any more:

  • Import everything via fabric.api
  • Removed
  • All useful imports are now available at the top level, e.g. from fabric import Connection.

It is recommended to upgrade fabfile.py from 1.x to 2.x for lot of reasons (e.g. Python 3 compatibility - specifically, we now support 2.7 and 3.4+), but if you still don't want to upgrade, you could uninstall 2.x and install 1.x, e.g.

pip uninstall fabric
pip install 'fabric<2.0'

After doing some research I found out that when you pip install fabric, it installs fabric v2. This version introduced "a near-total reimplementation & reorganization of the software". Your code was written for fabric v1, and needs to be rewritten to be compatible with fabric v2.

Python 2.7

Per Robert Lujo's answer, you can downgrade fabric to v1.

pip install 'fabric<2.0'

Python 3

Fabric v1 isn't compatible with Python 3, so you can instead install a fork called fabric3.

pip uninstall fabric
pip install fabric3

Note that the fabric3 fork has been deprecated by the maintainer, so you should consider making the code updates required to upgrade to fabric v2.