Python import error :No module named Fabric.api?
Asked Answered
M

5

17

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?

Milkweed answered 15/10, 2015 at 12:39 Comment(0)
M
2

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.

Milkweed answered 15/10, 2015 at 14:18 Comment(0)
A
24

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.

Aussie answered 27/2, 2020 at 9:57 Comment(2)
Wow, how did you find it out that fabric is fabric2 and not compatible?Marietta
Hey @Marietta what you can do to check any version of any installation / library is - pip show fabric and it'll show you the current version pip has installed for it.Aussie
N
9

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'
Numerator answered 11/12, 2019 at 0:2 Comment(0)
M
2

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.

Milkweed answered 15/10, 2015 at 14:18 Comment(0)
C
1

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.

Centrifuge answered 15/10, 2015 at 12:56 Comment(10)
I am new to fabric and python, what other information should I post?Milkweed
I have to run my python code on a remote server from my computer, I have the python 2.7 installed on my computer, I am using the terminalMilkweed
I did pip install fabric and it was fine, when I run fab it says : Fatal error: Couldn't find any fabfiles! Remember that -f can be used to specify fabfile path, and use -h for help. Aborting.Milkweed
Are you running your file directly or are you using fab ? if you want yo do $ fab <name of operation> you MUST have a fabfile.py. And i mean paste the whole source. and how you're running it. PLEASE make sure you remove any secrets /passwords/keys from source.Centrifuge
No, I have a script that imports the library, I used it to do some operations on the server, but its a part of a python script called dryad.py.Milkweed
I run my file as I wrote above python dryad.pyMilkweed
I cant paste the code here, the formatting gets all messed up. Look at the part "lets see what you have"Centrifuge
It gives all the packages installed, and fabric is not present there! How do I include it there?Milkweed
Right thats what i feared. You need to look at where you installed it. This is why virtualenv is so important -- saves you from pulling your hair out. You can do this: python -c "import sys; print sys.path" and make sure where ever fabric is -- is in that list. you can also add it 'sys.path.append("<path here>")'Centrifuge
Frankly. I would add a virtualenv and run your code in there. You can alway be sure that all your modules will be in the pip you installed it in.Centrifuge
F
0

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

Fabianfabianism answered 19/12, 2022 at 5:32 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewJedlicka

© 2022 - 2024 — McMap. All rights reserved.