2014-02-22 29 views
6

我在關注Curses programming HowTo on the Python site,但我遇到了一個相當奇怪的問題。Curses returned AttributeError:'module'object has no attribute'initscr'

我的代碼目前很短,實際上並沒有做任何事情因爲這個錯誤,我還沒有能夠繼續前進。這裏是我的代碼:

import curses 
#from curses import wrapper 

stdscr = curses.initscr() 
curses.noecho() 
curses.cbreak() 

stdscr.keypad(True) 

def main(stdscr): 

    begin_x = 20; begin_y = 7 
    height = 5; width = 40 
    win = curses.newwin(height, width, begin_y, begin_x) 

    stdscr.refresh() 
    stdscr.getkey() 

if __name__ == '__main__': 
    wrapper(main) 

和回溯:

Traceback (most recent call last): 
    File "curses.py", line 1, in <module> 
    import curses 
    File "/home/nate/Documents/Programming/Python/curses.py", line 4, in <module> 
    stdscr = curses.initscr() 
AttributeError: 'module' object has no attribute 'initscr' 

我註釋掉from curses import wrapper,因爲這是給我的另一個錯誤,

Traceback (most recent call last): 
    File "curses.py", line 1, in <module> 
    import curses 
    File "/home/nate/Documents/Programming/Python/curses.py", line 2, in <module> 
    from curses import wrapper 
ImportError: cannot import name wrapper 

但我想這將是一個問題。

我現在正在學習單詞教程,以瞭解詛咒,但是目前唯一讓我做的事情就是使用指向Python的curses:P。

我在Ubuntu 13.10執行Python 3.3.2,所以this question無關這一點,因爲他使用的是Windows,我不(謝天謝地:d)

爲什麼我沒有能夠做到這一點?我直接從Python網站複製它,所以你會認爲它會起作用!

回答

32

您將文件命名爲curses.py,因此Python認爲該文件是curses模塊。把它命名爲別的。

+0

哦,當然......我不能相信我那樣做了:P感謝:D –

+2

如果錯誤仍然存​​在,請檢查同一文件夾中的curses.pyc文件,並將其刪除。 –

1

mv curses.py someOtherName.py

如果你得到同樣的錯誤,請嘗試刪除任何.pyc文件。

在這種情況下,它將是rm curses.pyc

+0

感謝您的答覆,但這實質上是上面那個的重複。 :) –

+0

種,但可能會忘記刪除.pyc文件..在這種情況下,同樣的錯誤來 – chandan

+0

這是真的。我原以爲你應該對此作出評論,但直到你有50名代表,你才能做到這一點。 –

相關問題