没有命名的模块(导入错误Python 2.7)

时间:2014-07-19 10:04:27

标签: python ubuntu gnome gedit

我试图通过遵循每一步来解决“学习Python艰难之路”中的练习25,但我不断收到此错误:

>>> import ex25
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named ex25

我将本书网站上的代码复制粘贴到Gedit(Linux文本编辑器),但它似乎不是一个错字。

   def break_words(stuff):
        """This function will break up words for us."""
        words = stuff.split(' ')
        return words

    def sort_words(words):
        """Sorts the words."""
        return sorted(words)

    def print_first_word(words):
        """Prints the first word after popping it off."""
        word = words.pop(0)
        print word

    def print_last_word(words):
        """Prints the last word after popping it off."""
        word = words.pop(-1)
        print word

    def sort_sentence(sentence):
        """Takes in a full sentence and returns the sorted words."""
        words = break_words(sentence)
        return sort_words(words)

    def print_first_and_last(sentence):
        """Prints the first and last words of the sentence."""
        words = break_words(sentence)
        print_first_word(words)
        print_last_word(words)

    def print_first_and_last_sorted(sentence):
        """Sorts the words then prints the first and last one."""
        words = sort_sentence(sentence)
        print_first_word(words)
        print_last_word(words)

1 个答案:

答案 0 :(得分:3)

@Padraic Cunningham和@James Mills正在提出正确的问题,你只是得到一个导入错误,没有任何暗示代码错误。您需要确保该文件存在于您正在使用的目录中。请记住它是区分大小写的。

Learnpython.org ExamplesPython.org Modules Docs