6

There are 10+ SO posts about this already, none of the answers works for me and I still haven't seen an example of someone importing something from a sibling directory.

src
    __init__.py
    test.py
    package1
        __init__.py
        module1.py
    package2
        __init__.py
        module2.py

(_init_.py should not be necessary on python versions greater than 3.3 but I still have them there as they make no difference)

in test.py I have

import package1.module2

and it works fine however the problem is when I want to import something from package2 to package1, and vice versa. I have tried different import methods in module2.py and I receive these different error messages:

import src.package1.module1.py

with the error:

ModuleNotFoundError: No module named 'src'

and

from .. import package1

with the error:

ImportError: attempted relative import with no known parent package

The top answer here: How do I import a Python script from a sibling directory? also give me the exact error message as I showed above. The answers here: How to import a Python module from a sibling folder? changes nothing. Am I missing something or should it not be possible to import stuff between different folders/packages? Do I need the "sys.path hack"?

lapurita
  • 945
  • 1
  • 5
  • 11
  • How are you running your code? – Brian61354270 Dec 17 '21 at 15:33
  • 1
    Does this answer your question? [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) – Brian61354270 Dec 17 '21 at 15:33
  • I guess... Not really a solution but it explains why it is not possible to structure my code this way. From what I understand I can't run scripts directly in a subdirectory that uses something from another subdirectory – lapurita Dec 17 '21 at 19:05
  • 1
    Yes and no. Directories and subdirectories are meaningless to Python. All Python cares about are modules and packages, which are searched for exclusively on the Python path. You can certainly run modules from wherever you want inside of a package, but you need to remember to tell Python where in the package that module is / where the package(s) you want to import are. – Brian61354270 Dec 17 '21 at 19:09

2 Answers2

2

I gave almost all of the solutions given for similar questions a try, but none of them worked for me! However, after banging my head to the wall, I found out that this solution does work by just removing one dot:

import sys
sys.path.append('..')

Just remove one dot from string within append method, i.e.:

sys.path.append('.')

or your can use:

sys.path.insert(0, '.')

Then you can import any module from a sibling folder. I tried this using python 3.9.13 and it worked well.

Hadi F
  • 31
  • 3
0

I just spent several hours struggling with this same problem, trying various solutions from answers to the linked questions, and none of them worked. It was only when I cleared out the cruft from my various failed attempts and tried the stupidly simple solution from package1 import module1 (NB no dots, i.e. not a relative import) that it worked!

Note that your IDE may complain about this (PyCharm does in my current main project, but doesn't in a dummy project I created to test this solution separately). Nonetheless, when you run the code it should work (tested with Python 3.10 on Windows 11).

tok3rat0r
  • 141
  • 6
  • I've added a git repo with a minimal working example at https://github.com/tok3rat0r/dummyProj – tok3rat0r Oct 05 '22 at 03:32
  • 1
    Or you could use my library https://github.com/ronny-rentner/ultraimport which gives you more control over your imports and allows file system based imports from any directory. –  Oct 12 '22 at 02:39