求大神告知为什么python导入自定义模块时失败,导入自带模块却可以。
模块KNN.pyPython2.7.8(default,Jun302014,16:08:48)[MSCv.150064bit(AMD64)]onwin32Type"cop...
模块KNN.py
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> from numpy import *
>>> import operator
>>> def createdataset():
group = arry([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
labels = ['A','A','B','B']
return group,labels
导入自定义模块
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import KNN
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import KNN
File "KNN.py", line 1
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
^
SyntaxError: invalid syntax
>>> from KNN import*
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
from KNN import*
File "KNN.py", line 1
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
^
SyntaxError: invalid syntax
>>>
导入自带模块
>>> import ast
>>>
请问这是什么情况啊 展开
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> from numpy import *
>>> import operator
>>> def createdataset():
group = arry([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
labels = ['A','A','B','B']
return group,labels
导入自定义模块
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import KNN
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import KNN
File "KNN.py", line 1
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
^
SyntaxError: invalid syntax
>>> from KNN import*
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
from KNN import*
File "KNN.py", line 1
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
^
SyntaxError: invalid syntax
>>>
导入自带模块
>>> import ast
>>>
请问这是什么情况啊 展开
展开全部
不是写得很清楚吗,使用了不可用的符号,应该是你写的脚本本身有问题
更多追问追答
追问
不对,所有自定义模块都不可以导入
>>> import sys
>>> print(sys.platform)
win32
>>> print(2 ** 100)
1267650600228229401496703205376
>>> x = 'spam!'
>>> print(x * 8)
spam!spam!spam!spam!spam!spam!spam!spam!
这个模块同样不可以导入
追答
大哥,你回复中的例子是什么意思,这些例子不是运行正常吗?你说的导入和我说的导入是一个东西吗,这样吧, 我给个简单的自定义模块的例子,
你自己新建一个文件,名字叫 haha.py
里面写上如下代码,
class hahaha(object):
def __init__(self, astr):
self.ha = astr
def __str__(self):
return self.ha
def ha1():
print("hahaha")
然后在该目录下启动python,运行
import hahaha
如果不报错就是对的。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询