我是編程和Python的新手。我正在關注Learn Python the Hard Way的書。 鍛鍊25的一部分,我寫了一個腳本:Python ImportError-這裏有什麼問題?
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 words 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)`
我路徑
C:\Users\Brandon\Experiment\Python_ex
下保存這個從gedit中作爲
ex25.py
我運行64位Windows 7.
當我從python.exe 導入ex25時,我得到:
> Traceback (most recent call last):
> File "(stdin)", line 1, in `<module>`
> ImportError: No module named ex25
在電腦\屬性\高級\環境變量我加了系統變量:
PYTHONPATH
C:\Python27
沒有幫助。 我在做什麼錯?
我得到:NameError:名稱'sys'未定義 –
不能爲真。 ['sys'](http://docs.python.org/library/sys.html)是['Python Standard Library']的一部分(http://docs.python.org/library/index.html)並始終「可導入」 – Nemoden
我還沒有導入sys。非常感謝!你提供了我的解決方案。 –