0

我是编程和 Python 的新手。我正在关注Learn Python the Hard Way一书。作为练习 25 的一部分,我编写了一个脚本:

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)`

我把它从 gedit 保存为

ex25.py

在路径下

C:\Users\Brandon\Experiment\Python_ex

我正在运行 64 位 Windows 7。

当我从 python.exe 导入 ex25 时,我得到:

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

在 Computer\Properties\Advanced\Environment Variables 下,我添加了系统变量:

蟒蛇路径

C:\Python27

那没有帮助。我究竟做错了什么?

4

2 回答 2

5

C:\Users\Brandon\Experiment\Python_ex不在您的系统路径上,因此 python 不知道在哪里ex25可以找到您的模块

import sys
sys.path.append(r'C:\Users\Brandon\Experiment\Python_ex')
于 2011-07-28T16:18:53.773 回答
0

I just had the same problem. As my file was saved in Desktop/mint/ex25.py . I first changed the directory to desktop by command cd Desktop/mint. and than run the way it has been recommended. It will solve it. Want to go back to older directory use command cd -.

于 2014-05-06T13:11:11.620 回答