Search code examples
pythonpython-2.7redhatfabricimporterror

Running fabric script throws ImportError: No module named fabric.main


I am on Redhat, and when I run any fabric scripts I see the following error:

Traceback (most recent call last): File "/usr/bin/fab", line 8, in from fabric.main import main ImportError: No module named fabric.main

The file /usr/bin/fab is configured to use python 2.7 (/usr/local/bin/python):

#!/usr/local/bin/python     
# -*- coding: utf-8 -*- import re import sys

from fabric.main import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

The result is the same even if I just call fab. Not sure what else I should be configuring. I have not set up a virtualenv for fab. If I must, I will do so.

I installed python 2.7, and then I installed fab as follows:

wget https://bootstrap.pypa.io/get-pip.py
sudo /usr/local/bin/python get-pip.py
sudo /usr/local/bin/pip install fab

Solution

  • I ended up doing the following:

    1. Install Python 2.7 in a virtualenv (~/virtualenvs/py2.7) by following the top-rated answer from Is it possible to install another version of Python to Virtualenv? by DTing

    2. Install pip in ~/virtualenvs/py2.7/bin/:

      wget https://bootstrap.pypa.io/get-pip.py sudo ~/virtualenvs/py2.7/bin/python2.7 get-pip.py

    3. Install fab in ~/virtualenvs/py2.7/bin:

      sudo ~/virtualenvs/py2.7/bin/pip install fab

    4. For some reason, I still did not have the fab file under ~/virtualenvs/py2.7/bin, so I just copied the original /usr/bin/fab that was giving me issues to ~/virtualenvs/py2.7/bin/ and edited it to point to the virtualenv python2.7 (~/virtualenvs/py2.7/bin/python2.7)

    Running ~/virtualenvs/py2.7/bin/fab worked, and gave me the following (welcome) error:

    Fatal error: Couldn't find any fabfiles!

    Remember that -f can be used to specify fabfile path, and use -h for help.

    Aborting.

    I am content for now -- since fab seems to work. But if anyone has ideas why the actual fab file was not created in the ~/virtualenvs/py2.7/bin/ directory, I am all ears. Thank you!