Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 导入函数有效,导入类无效';T_Python - Fatal编程技术网

Python 导入函数有效,导入类无效';T

Python 导入函数有效,导入类无效';T,python,Python,当我导入一个函数时,它运行代码没有问题,但是如果我导入一个类,它就不工作了。上面写着ImportTerror:无法导入名称“Y”,我已经在sublime文本和命令提示符上尝试过了。 如果我导入一个函数,它就会工作 from testing2 import x x() #testing2.py has: def x(): print("It worked") 如果我导入一个类,它就不起作用 from testing2 import Y Y().x() #testing2 has: c

当我导入一个函数时,它运行代码没有问题,但是如果我导入一个类,它就不工作了。上面写着ImportTerror:无法导入名称“Y”,我已经在sublime文本和命令提示符上尝试过了。 如果我导入一个函数,它就会工作

from testing2 import x
x()
#testing2.py has:
def x():
    print("It worked")
如果我导入一个类,它就不起作用

from testing2 import Y
Y().x()

#testing2 has:

class Y:
    def x(self):
        print("It worked")

两个人都为我工作。你有没有在class
Y
下面把别的名字命名为
Y
?代码没有问题。您可能会保存在某个路径中,并尝试从另一个路径访问。这是有效的,因为meThis是文件中唯一的代码,而我只使用这些相同的文件。我将成功导入一个函数,然后删除所有内容并用一个类重写它,在导入一个类时它会带来一个错误。你试过一个完全空白的目录吗;使用类和函数创建Python文件;cd到该目录,打开控制台,键入:'import,并在模块上使用dir-检查函数和类在dir输出中是否都可见。