通过困难方式学习Python,ex25模块未找到
我对这个问题感到很困惑,我看到过很多类似问题的回答,但在我的情况下都没有成功。
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:\Users\Majul> python
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on wi
32
Type "help", "copyright", "credits" or "license" for more information.
>>> execfile("c:\python27\scripts\ex25.py")
import ex25
>>> sentence = "all good things come to those who wait."
>>> words = ex25.break_words(sentence)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'ex25' is not defined
>>>
4 个回答
-1
试试这个,这对我有用:
execfile("/path/to/py/file")
然后在那个模块里使用函数。希望这能帮到你。
编辑:
在包含了 .py 文件之后,就不需要再使用
words = ex25.break_words(sentence)
你要直接使用这个函数,像这样
words = break_words(sentence)
也就是说,看看你发的例子。
0
我也遇到过同样的问题。解决办法是把PYTHONPATH添加到环境变量里,然后把我的工作文件夹的路径设置为C:\users\%User%\Desktop
。大多数情况下,这样做就能解决问题吧?希望这对你有帮助。
0
导入时是区分大小写的,确保你的文件名没有大写字母,比如说文件名不能写成 Ex25.py。
0
根据你提供的信息,你输入了“execfile("c:\python27\scripts\ex25.py")”,但课程里并没有让你这样做。按照课程的要求,你应该先创建这个文件,然后打开Python解释器,准确地输入Zed所说的内容。如果你的ex25.py代码没有语法错误,那么它应该可以正常运行。