2011-07-28 15 views
0

私はプログラミングとPythonが初めてです。私はを学んでいます。Pythonの難問を学ぶ本。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

ex25.py

としてのgeditからこれを保存し、私は64ビットのWindowsを実行しています: エクササイズ25の一環として、私は、スクリプトを書きました7.

python.exeからex25をインポートすると、私は を取得します:

PYTHONPATH

C:\Python27

助けにはならなかった:コンピュータ\プロパティ\アドバンスド\環境変数の下

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

私はシステム変数を追加しました。 私は何が間違っていますか?

答えて

5

C:\Users\Brandon\Experiment\Python_exあなたex25モジュールが

import sys 
sys.path.append(r'C:\Users\Brandon\Experiment\Python_ex') 
+0

私は以下を取得しますNameError:名前 'sys'が定義されていません –

+0

trueにはなりません。 ['sys'](http://docs.python.org/library/sys.html)は、' 'Python標準ライブラリ' '(http://docs.python.org/library/index.html)の一部です。常に "インポート可能" – Nemoden

+1

私はまだsysをインポートしていませんでした。どうもありがとうございます!あなたは私の解決策を提供しました。 –

0

見つけることができる場所ので、Pythonは、私はちょうど同じ問題を抱えていた認識していない、あなたのシステムパス上にありません。私のファイルはDesktop/mint/ex25.pyに保存されていました。まず、cd Desktop/mintコマンドでディレクトリをデスクトップに変更しました。それが推奨されている方法を実行するよりも。それはそれを解決します。 古いディレクトリに戻るにはcd - を使います。

関連する問題