0

The file structure for a module im creating goes as follows:

PIV
| __init__.py
| base.py
| core.py
| exceptions.py
.gitignore
LICENSE
requirements.txt

But whenever I run a file like core.py, I get the following error:

Traceback (most recent call last):
  File "c:/Users/ghub4/OneDrive/Desktop/Python-Image-and-Video-tools/PIV/core.py", line 33, in <module>
    from . import base
ImportError: attempted relative import with no known parent package

The same thing happens when I run the __init__.py file. I'm not sure on what went wrong because all of the python files are in the same folder. Can someone clarify what's the problem and explain how I should fix it?

Import code for core.py file:

from __future__ import absolute_import
import sys
import os
from PIL import Image
import io
from . import base
from . import exceptions

(The __init__.py folder has the same relative imports as in the core file but also including: from . import core)

Sergio Ley
  • 85
  • 1
  • 14
  • 1
    Check this out: https://stackoverflow.com/a/16985066/5362583 – Alok Aug 07 '20 at 05:50
  • After I followed the instructions, Im getting a module not found error: `Traceback (most recent call last): File "c:/Users/ghub4/OneDrive/Desktop/Python-Image-and-Video-tools/happy.py", line 1, in import PIV File "c:\Users\ghub4\OneDrive\Desktop\Python-Image-and-Video-tools\PIV\__init__.py", line 32, in import base ModuleNotFoundError: No module named 'base'` @Alok – Sergio Ley Aug 07 '20 at 06:14
  • 1
    Follow this one for the new problem: https://stackoverflow.com/a/17525397/5362583 – Alok Aug 07 '20 at 07:14
  • 1
    Thank you @Alok! This solved my Problem. If you want to recieve credit you can write an answer based on those links. – Sergio Ley Aug 07 '20 at 07:28
  • I have written an answer based upon the links. I appreciate your generosity on giving the credits :) All the best – Alok Aug 07 '20 at 07:49

1 Answers1

1

Based upon the two links you will given below, here is what needed for the problem to solve:

You need to import the package as this

from mymodule import some_useful_method

Sometimes, we get the no module error, in this case, we can import like this

from module_name.classname import some_useful_method
Alok
  • 8,452
  • 13
  • 55
  • 93