为什么我的程序抛出异常 'xyz' is not a package?

时间:2021-02-06 20:57:30

标签: python-3.x module package

我的 Spyder ide 中有以下设置:

enter image description here

此程序显示以下异常:

runfile('C:/Users/pc/source/repos/python_package_and_module_test/main.py', wdir='C:/Users/pc/source/repos/python_package_and_module_test')


Reloaded modules: jupyter_client.session, zmq.eventloop, zmq.eventloop.ioloop, tornado.platform, tornado.platform.asyncio, tornado.gen, zmq.eventloop.zmqstream, jupyter_client.jsonutil, jupyter_client.adapter, spyder, spyder.pil_patch, PIL, PIL._version, PIL.Image, PIL.ImageMode, PIL.TiffTags, PIL._binary, PIL._util, PIL._imaging, cffi, cffi.api, cffi.lock, cffi.error, cffi.model
Traceback (most recent call last):

 File "C:\Users\pc\source\repos\python_package_and_module_test\main.py", line 1, in <module>
import token.factoryclass as a

ModuleNotFoundError: No module named 'token.factoryclass'; 'token' is not a package

我该如何解决这个问题?


源代码:

我的文件如下:

.\

python_package_and_module_test.py
import token.factoryclass as a

var = a.factoryclass();
var.print();

工厂类.py
class factoryclass(object):
    def print(object):
        print("factoryclass")

tokenclass.py
class tokenclass(object):
    def print(object):
        print("tokenclass")

工厂\

工厂类.py
class factoryclass(object):
    def print(object):
        print("factory.factoryclass")

tokenclass.py
class tokenclass(object):
    def print(object):
        print("factory.tokenclass")

令牌\

工厂类.py
class factoryclass(object):
    def print(object):
        print("token.factoryclass")

tokenclass.py
class tokenclass(object):
    def print(object):
        print("token.tokenclass")

然而,相同的包和模块结构在 repl.it 中工作:

enter image description here

0 个答案:

没有答案