Link to home
Start Free TrialLog in
Avatar of Massimo Scola
Massimo ScolaFlag for Switzerland

asked on

Error when importing Python module: ValueError: Attempted relative import in non-package

I am learning Python and I need some help with this:

I understand that that in order to import, I write from x import y.  In my example, I am trying to import quick_sorted from sort.
Both files are in the same folder.

The quick_sort method relies on code which is in the priority queue file.
Interestingly, it is imported this way:
from .priority_queue import PriorityQueue

Open in new window

The files to import are supplied for me to use.

Is there anything wrong with my implementation or with the files supplied?

I import it the following way:

from sort import quick_sorted

Open in new window


I am trying to use the quick sort method so that I can sort one of my lists.

This is the error I get:

Traceback (most recent call last):
  File "C:\Users\massi\Documents\OU\TMA02_Q2.py", line 5, in <module>
    import sort
  File "C:\Users\massi\Documents\OU\sort.py", line 11, in <module>
    from .priority_queue import PriorityQueue
ValueError: Attempted relative import in non-package

TMA02_Q2.py
sort.py
priority_queue.py
ASKER CERTIFIED SOLUTION
Avatar of gelonida
gelonida
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Massimo Scola

ASKER

Great this worked fine for me.
Thanks a lot