我是编程和Python的新手。我正在关注学习Python困难之路一书。 作为练习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:\用户\布兰登\实验\ 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下,我添加了系统变量:
PYTHONPATH
C:\ Python27
这没有帮助。 我做错了什么?
答案 0 :(得分:5)
C:\Users\Brandon\Experiment\Python_ex
不在您的系统路径上,因此python不知道您的ex25
模块可以找到的位置
import sys
sys.path.append(r'C:\Users\Brandon\Experiment\Python_ex')
答案 1 :(得分:0)
我遇到了同样的问题。由于我的文件保存在Desktop / mint / ex25.py中。我首先通过命令cd Desktop / mint将目录更改为桌面。而不是按照推荐的方式运行。它会解决它。 想要回到旧目录使用命令cd - 。