导入python模块的问题

nna*_*ski 7 python import module

我正在尝试bitstring在脚本中使用python的模块,并且导致导入错误.从交互模式运行时不会发生此错误.

这是代码:

import bitstring
b = bitstring.BitArray(bin='001001111')
Run Code Online (Sandbox Code Playgroud)

当像这样运行时:

python test.py
Run Code Online (Sandbox Code Playgroud)

我明白了:

AttributeError: 'module' object has no attribute 'BitArray'
Run Code Online (Sandbox Code Playgroud)

但是,当我这样做时:

$ 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
Run Code Online (Sandbox Code Playgroud)

它工作得很好!它是由同一个用户运行的相同解释器.有什么指针吗?

Mic*_*ent 7

我预测你在当前目录中创建了一个bitstring.py.


phi*_*hag 1

该问题是由ofbitstring.py中的文件引起的,但不是由交互式 python shell 中的文件引起的。最有可能的是,目录中有一个文件,并且您从另一个工作目录启动了 shell。sys.pathtest.pybitstring.pytest.py

由于 pythonsys.path从头到尾遍历,当前目录中的模块(即使是意外创建的)也会掩盖系统库目录中的模块。