8

I'm trying to use python's bitstring module in a script and am getting an import error. This error does not happen when running from interactive mode.

Here's the code:

import bitstring
b = bitstring.BitArray(bin='001001111')

When run like this:

python test.py

I get this:

AttributeError: 'module' object has no attribute 'BitArray'

However, when I do this:

$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import bitstring
>>> b = bitstring.BitArray(bin='001001111')
>>> print b
0b001001111

It works just fine! It's the same interpreter being run by the same user. Any pointers?

Dominik Honnef
  • 17,937
  • 7
  • 41
  • 43
nnachefski
  • 1,552
  • 2
  • 18
  • 29
  • 7
    Print out `bitsrting.__file__` from within the script in non-interactive mode and make sure that it's pointing to the correct file – inspectorG4dget Jun 03 '11 at 19:41
  • 4
    Is this happening in the same directory? – 6502 Jun 03 '11 at 19:41
  • 10
    I predict you have created a bitstring.py in your current directory. – Michael Kent Jun 03 '11 at 19:47
  • I `pip`ed `bitstring`, ran your code, and I have no issue. I used cpython 2.7 on WinXP. Have you tried running it or installed the `bitstring` properly? – OnesimusUnbound Jun 03 '11 at 19:52
  • Michael, you are right! lol Thanks for the tip. – nnachefski Jun 03 '11 at 19:56
  • Compare your `sys.path` results in the two environments. – Mark Ransom Jun 03 '11 at 19:58
  • i doubt this is still relivent to the origanal poster but i hope it helps someone trying to find the answer to a problem, my guess would be module caching by the interpreter, if you changed the module and did not reload the interpreter then the module may have been cached – John Aug 16 '14 at 02:28

3 Answers3

7

I predict you have created a bitstring.py in your current directory.

Bart
  • 19,692
  • 7
  • 68
  • 77
Michael Kent
  • 1,736
  • 12
  • 11
1

The problem is caused by a bitstring.py file in sys.path of test.py, but not in that of the interactive python shell. Most likely, there's a bitstring.py file in the directory test.py is in, and you started your shell from another working directory.

Since python traverses sys.path from front to end, modules in the current directory - even if accidentally created - overshadow those in system library directories.

phihag
  • 278,196
  • 72
  • 453
  • 469
0

Google App Engine actually had a similar issue at one point. The easiest solution there was simply comment the offending line or use try...except. Obviously that won't work here.

In that case, the problem was initialization order. A half second later a similar line of code was called again with success. Their solution? refactor. :-(

The best I've seen is a dynamic lookup of the class: bitstring.__dict__.get("BitArray") or getattr(bitstring, "BitArray");. It isn't ideal (and I believe I've even seen those return null), but hopefully it can get you somewhere.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166