0

I have the following setup in my Spyder ide:

enter image description here

this program is showing the following exception:

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

how can I resolve this issue?


source code:

my files are as follows:

.\

python_package_and_module_test.py

import token.factoryclass as a

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

factoryclass.py

class factoryclass(object):
    def print(object):
        print("factoryclass")

tokenclass.py

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

factory\

factoryclass.py

class factoryclass(object):
    def print(object):
        print("factory.factoryclass")

tokenclass.py

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

token\

factoryclass.py

class factoryclass(object):
    def print(object):
        print("token.factoryclass")

tokenclass.py

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

However, the same package and module structure is working in repl.it:

enter image description here

user366312
  • 16,949
  • 65
  • 235
  • 452
  • Does this answer your question? [How to write a Python module/package?](https://stackoverflow.com/questions/15746675/how-to-write-a-python-module-package) – Macattack Feb 06 '21 at 21:00
  • you need a file (can be empty) `__init__.py` in the `token/` directory – Macattack Feb 06 '21 at 21:01
  • @Macattack, adding `__init__.py` didn't work. – user366312 Feb 07 '21 at 20:18
  • This might be an issue with the the places python searches for imports. Generally the first place it checks is your current working directory, then it checks all the normal system paths for installed packages. I can't find documentation on `runfile('C:/Users/pc/source/repos/python_package_and_module_test/python_package_and_module_test.py', wdir='C:/Users/pc/source/repos/python_package_and_module_test')` to know if it switches your current working directory. You can check these things with `os.getcwd()` (your current working directory) and `sys.path` (your import locations, `''` is cwd) – Macattack Feb 07 '21 at 20:45

0 Answers0