Brambor
Brambor

Reputation: 664

importing from same file raises ImportError

I'm sorry for asking but I can't find answer. I have this tree:

Dogs&Sheeps
    simulation.py
    stuff
        main.py
        values.py

Code in simulation.py begins:

import pygame
import sys

from stuff import main
from stuff import values

Code in main.py begins:

from random import randint
from time import sleep
import queue
import pygame
import sys

import values

If I launch simulation.py this error ocures:

    Traceback (most recent call last):
  File "...\simulation.py", line 5, in <module>
    from stuff import main
  File "...\stuff\main.py", line 7, in <module>
    import values
ImportError: No module named 'values'

I think it's obvious what I want to do but anyway. In the file main.py I want to import the file values.py which is in the same folder.

Upvotes: 1

Views: 120

Answers (1)

Anand S Kumar
Anand S Kumar

Reputation: 90909

In Python 3.x , from documentation -

When packages are structured into subpackages (as with the sound package in the example), you can use absolute imports to refer to submodules of siblings packages. For example, if the module sound.filters.vocoder needs to use the echo module in the sound.effects package, it can use from sound.effects import echo .

In the same way, you need to use absolute package name , instead of relative names , so in your main.py do -

from stuff import values 

Upvotes: 4

Related Questions