1

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.

Brambor
  • 604
  • 1
  • 8
  • 25
  • 2
    You need an `__init__.py` file in `stuff` to [mark it as a package](http://stackoverflow.com/questions/448271/what-is-init-py-for). – approxiblue Aug 06 '15 at 23:45
  • I added `__init__.py` which is a empty file into stuff folder but it raises the same error – Brambor Aug 06 '15 at 23:53
  • there are also difirent files in `Dogs&Sheeps` folder and `stuff` folder does it matter? – Brambor Aug 07 '15 at 00:02
  • Ok that solution didn't work @Anand S Kumar solved it and it works even without the `__init__.py` thingie. – Brambor Aug 07 '15 at 13:09

1 Answers1

4

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 
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • Thanks, it works! But why? Is what you say in non-programing laguage than it importing the main.py so it is imported in simulation.py so it is out of the stuff folder so I have to say that I need search for it in the stuff folder? – Brambor Aug 07 '15 at 13:07