-1

I've got this structure bellow, I want to import data-parser/service.py classes from api/server.py how can I do it?

- project
    __init__.py    
    main.py
    
    - api
      __init__.py
      server.py

    - data-parser
      __init__.py
      service.py
    
yehezkel horoviz
  • 198
  • 3
  • 10
  • 2
    Prerequisite reading before any relative import question: [Relative imports for the billionth time](https://stackoverflow.com/q/14132789/11082165) – Brian61354270 Dec 08 '21 at 21:03
  • `from ..app import submodule1`. – 9769953 Dec 08 '21 at 21:05
  • One way is to ensure `sys.path` has the path to the project’s root directory, the import relative to that. – S3DEV Dec 08 '21 at 21:07
  • I'm always getting this exception: ImportError: attempted relative import with no known parent package. @9769953 this is not working as excpected – yehezkel horoviz Dec 08 '21 at 21:10
  • @S3DEV - im running the script now from submodule correctly – yehezkel horoviz Dec 08 '21 at 21:12
  • Then you're probably executing a submodule from within the package, and not from outside the package. Don't do that; that's not what a package is for. – 9769953 Dec 09 '21 at 05:45
  • @9769953 can you tell me more about that? best practices, and why not? – yehezkel horoviz Dec 09 '21 at 11:02
  • No, not best practices. Correct practice. Packages are not supposed to be run/executed, they are supposed to be imported. From *outside* their directory. That ignores inter- and intra package imports, but I'm talking from a user perspective. A user script should run *outside* the package(s) directory, then import the necessary packages. – 9769953 Dec 09 '21 at 19:39
  • I wrote an answer, with comments about modules, packages, relative imports and how to run things, to a related question here: https://stackoverflow.com/questions/55102788/module-not-found-running-on-command-line/55102872#55102872 . It's still a summary, but hopefully helpful. – 9769953 Dec 09 '21 at 19:40
  • @9769953 I red your example and did not excactly got it, so according to my example how can I use app in api? – yehezkel horoviz Dec 10 '21 at 12:08
  • First edit your question to show how you are importing/using your module? – 9769953 Dec 10 '21 at 13:03
  • @9769953 ok I did it – yehezkel horoviz Dec 11 '21 at 18:39
  • Where? You state what you want, not what you do / how you run things currently. – 9769953 Dec 11 '21 at 21:52
  • Also, you've completely changed the names of the modules. That is confusing. – 9769953 Dec 11 '21 at 21:52

1 Answers1

0

I'm confused by your file structure as a whole but from what I can see you have one project folder, your main script in one folder in the project folder, and your modules in a seperate folder in the project folder. If this is the case, I don't understand to why you don't just have your main.py file in the same directory as a folder with your modules in it (I only mention in case you can do this instead as it makes this a whole lot easier) but here is the solution if you're certain you need your file structure to be the same as it is currently:

import sys
from os import path
from pathlib import Path

sys.path.insert(0, path.join(Path(__file__).parent.absolute().parent.absolute(), "app"))
import submodule1

This, essentially, gets the path of the file, it's parent (the folder it's in), and then the parent's parent (the project folder) to add it to sys.path (you can use modules in the paths of this list). I'm almost 100% certain there's a better, more efficient way of doing this, but this is what I had off the top of my head due to the uniqueness of the question. I hope it's somewhat helpful.

  • 1
    @S3DEV I knew something looked off! Thanks for pointing it out to me. – TheAngriestCrusader Dec 08 '21 at 21:46
  • any best practice of how to seperate project structure? I hvae the actuall api and the backend of it, in the future I will also have a front (that will be wrriten in Vue)? – yehezkel horoviz Dec 09 '21 at 11:56
  • Hacking a script or module by patching the path this way, is generally a bad idea, unless you really know what you are doing. Given the OPs mistake, I think this is a bad suggestion to use. – 9769953 Dec 09 '21 at 19:41
  • This is the only answer I had for this strange scenario, and it doesn't take much brain power to not completely screw up this code, it's very basic. Overall, I really think OP should just have his files together like a normal project. I asked if there was something stopping this from being a possibility but I got no response so I assume there's no reason to not have scripts in the same directory. – TheAngriestCrusader Dec 10 '21 at 12:09
  • 1
    There is no strange scenario, afaict. There is a package called `project`, with two subpackages called `app` and `api`, each with their modules. – 9769953 Dec 10 '21 at 17:03