0

I've following structure where I want to access module from data package inside relative scraping package. There is common betterhire package, however I am getting following error. Please see attached pic of sturcture in VS Code

File "~/better-hire/src/betterhire/scraping/indeed.py", line 7, in <module>
    from betterhire.data.models import Company
ModuleNotFoundError: No module named 'data'

enter image description here

I expect that all submodules under betterhire package are cross importable. Please suggest the correct structure for Python projects that includes src folder Poetry.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
Aren Hovsepyan
  • 1,947
  • 2
  • 17
  • 45
  • Is the library installed? Which command(s) did you run? What is the full output? – sinoroc Jun 30 '23 at 21:28
  • @sinoroc It is a relative module in the parent folder – OneCricketeer Jun 30 '23 at 21:37
  • @OneCricketeer I do not understand what you mean. -- @Aren The project directory structure seems perfectly fine. The issue might be in the way you run the code. Let's assume you have an entry point in `src/betterhire/scraping/indeed.py`, then you should start this entry point like this: `python -m betterhire.scraping.indeed` (dots `.` instead of slashes `/` and no `.py`). Maybe this helps. – sinoroc Jul 01 '23 at 07:19
  • @sinoroc You asked if something is installed, when the error in the question is trying to load a local file, not any installed one – OneCricketeer Jul 01 '23 at 09:16
  • 1
    @OneCricketeer Ah... Because of the tilde `~` in the path in the error output? I see. -- I think we still need to know what command was run, what was the current working directory, and so on. – sinoroc Jul 01 '23 at 09:21
  • @OneCricketeer I've run it via `python src/betterhire/scraping/indeed.py`, because it's script. Can you elaborate why we should use `python -m betterhire.scraping.indeed` instead? – Aren Hovsepyan Jul 01 '23 at 23:08
  • Did you try relative import? https://docs.python.org/3/reference/import.html#package-relative-imports – OneCricketeer Jul 02 '23 at 02:29
  • @ArenHovsepyan See the answers on this post [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time/14132912#14132912). If you run indeed.py as a script, then it not loaded as part of a package, and therefore it cannot do relative package imports, but relative imports will instead be directory based. If you instead run it by using `python -m betterhire.scraping.indeed` it will be loaded as a module and it can then use the relative package imports as you are trying to do. – nigh_anxiety Jul 02 '23 at 18:13
  • @nigh_anxiety, got it, so what if I want to run `indeed.py` as a script, but just include betterhire? Should it mandatorily be from pip installed packages? – Aren Hovsepyan Jul 02 '23 at 23:47
  • @OneCricketeer, FYI I've tried `python -m`, but got following error `python -m betterhire.scraping.craigslist /Users/arenhovsepyan/.pyenv/versions/3.10.7/bin/python: Error while finding module specification for 'betterhire.scraping.craigslist' (ModuleNotFoundError: No module named 'betterhire')`. Also `betterhire` is part of my package so it's not pip installed – Aren Hovsepyan Jul 02 '23 at 23:48
  • 1
    Ok, I think I got how it works. I need to be in `src` folder then run as a module via `python -m`. thanks! – Aren Hovsepyan Jul 03 '23 at 00:03

0 Answers0