Python Package “No module named…”

2019-03-30 05:44发布

问题:

I'm fairly new to Python, and I'm working on creating my first simple package. Here's my structure:

Math/
    __init__.py
    divide.py
    minus.py
    multiply.py
    plus.py

Each of the four files has a simple mathematical function declared. My init file is simply

from plus import *
from minus import *
from multiply import *
from divide import *

When I try to "import Math", however, I get the following error:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import Math
  File ".\Math\__init__.py", line 1, in <module>
    from plus import *
ImportError: No module named 'plus'

And yes, I know my package has to be in the correct folder; if I move any one of my files outside of the Math folder and run the import call on it by itself from the shell it works just fine.

回答1:

You are using Python 3 and it requires relative imports inside packages.

from .plus import *
from .minus import *
from .multiply import *
from .divide import *