Python import error :No module named Fabric.api?

Question:

I am getting the following error:

Traceback (most recent call last):
  File "drayd.py", line 2, in <module>
    from fabric.api import *
**ImportError: No module named fabric.api**

I am running my program using:

python drayd.py

These are my imports:

import os,pprint
from fabric.api import *
import time
import argparse
import ConfigParser

I don’t have a file named fabric as other answers suggested. I installed fabric using pip but it still doesn’t work, any suggestions?
I am using the OSX Terminal.

NOTE: I realized the fabric I installed is not linked to python installation ie it does not recognize that fabric is installed by pip.
I am using the python version 2.7 default by osx.
How do I link fabric installation to python?

Asked By: LoveMeow

||

Answers:

You’re going to have to be more explicit. I created a new virtualenv, installed fabric and everything is fine. You need to paste more source or more information about your environment.

$ cd /tmp
$ virtualenv test && source test/bin/activate
$ pip install fabric
...
Successfully installed fabric-1.10.2
$ python
>>> from fabric.api import *
>>> 

lets see what you have:

$ python
>>> import pkgutil
>>> [name for _, name, _ in pkgutil.iter_modules()]
... paste THIS output somewhere ...

PS. it’s really good to do all your tests/projects inside a virtualenv/pyenv so that you never have conflicts with current/future projects.

Answered By: Javier Buzzi

The answer to my question is right here :

PIP install and Python path

I had to add the location of my packages( which were installing not in the sys.path) so I had to add them manually
Use pip show to find location of the packages and add them to .bash_profile
as @Javier Buzzi said I will take the advice and also run my python code from virtualenv.

Answered By: LoveMeow

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'
Answered By: Robert Lujo

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.

Answered By: Mohseen Mulla

Solved the error by switching from python3.9 to python3.7

Answered By: suhail_bhat
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.