0

My project structure looks like this

pythonProject
|
|-__init__.py
|
|- a 
|  |- file_a.py 
|  |-__init__.py
|
|- b 
   |- file_b.py 
   |-__init__.py

I've also attached the image of my project's structure below.

enter image description here

My file_a.py looks like:

class class_a:
    def __init__(self):
        pass
    def show(self):
        print('class A')

if __name__ == "__main__":
    pass

My file_b.py looks like:

from pythonProject.a.file_a import class_a

class class_b:
    def __init__(self):
        pass

    def show(self):
        a = class_a()
        a.show()

if __name__ == "__main__":
    b = class_b()

But I keep getting an error No module named 'pythonProject'. All the folders do have __init__.py file. What am I doing wrong?

On the terminal, I run the command python b/file_b.py from inside the pythonProject.

Also, I would not like to add project path using sys library.

Thanks.

Things I've tried.

  1. I added all the __init__.py file in all the folders. But all of them are empty. Should they have any specific references?
  2. I tried the import line to ..a.file_a import class_a. Then I get the error: ImportError: attempted relative import with no known parent package
  3. I tried the import line to a.file_a import class_a but then the error is ModuleNotFoundError: No module named 'a'.
ayo
  • 1
  • 1
  • 1
    Does this answer your question? [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time). While this question isn't directly about relative imports, it seems that your design is suffering from the same issues with the relationship between packages and directories and the differences between modules and scripts that are addressed in that question. – Brian61354270 Mar 01 '23 at 00:48
  • In order for Python to be able to import the package `pythonProject`, the directory _containing_ that directory needs to be on the Python path. This is usually done either by installing your project as a distribution package or by ensuring that the directory containing `pythonProject` is on your Python path. – Brian61354270 Mar 01 '23 at 00:52
  • Also, as described in the dupe, using `python ./dir/file.py` runs that file as a top-level script that _is not part of a package_. You want to run a module inside of a package, so you should use `python -m pythonProject.subpkg.module`. – Brian61354270 Mar 01 '23 at 00:53
  • @Brian I think the linked question has the answer to my question. I still have to go through the complete comment. But essentially these is a mismatch between a module and how I'm running it as a script. Thanks for the link. – ayo Mar 01 '23 at 01:01

1 Answers1

1

I think the problem appears to be with your if __name__ == "main" condition in files a.py and b.py. Instead of "main", the correct string is "__main__" (double underscores on each side)

Change the condition in both files to if __name__ == "__main__": and try running file b.py again. This should resolve the error

No module named 'pythonProject'.

In terms of imports, you can import class a from file a.py using the absolute import syntax in file b.py

from pythonProject.a.file_a import class_a

As long as you run file b.py from within the pythonProject directory, this should work.

Brian61354270
  • 8,690
  • 4
  • 21
  • 43
KNTY
  • 351
  • 3
  • 11
  • that doesn't resolve the problem. My understanding is also that it should work but it gives me the same error: `from pythonProject.a.file_a import class_a` followed by `ModuleNotFoundError: No module named 'pythonProject'` – ayo Mar 01 '23 at 00:48
  • 1
    _"As long as you run file `b.py` from within the `pythonProject` directory, this should work."_ Shouldn't that be from the directory _containing_ `pythonProject`? If their CWD is `pythonProject`, then `pythonProject` won't be discoverable on the Python path. Instead, `a` and `b` will be discovered as top-level packages. In any case, they likely want to invoke `file_b.py` as a module (`python -m pythonProject.b.file_b`) instead of as a top-level script – Brian61354270 Mar 01 '23 at 00:58