2

I tried to solve this using this answer, but I just can't seem to get it to work.

Here is my structure, essentially I'm just writing some basic scripts to help me manage some board game stats I keep.

<workspace>
  +-- win_loss/
    +-- KeyForgeWL.py
    +-- DB/
      +-- KeyForgeData.txt

Easy enough. I'm trying to run KeyForgeWL.py, and I want to import data from its sub directory. With the previous answer, I tried this:

import importlib.resources as pkg_resources
from . import DB

I then get: ImportError: attempted relative import with no known parent package

Fine. I convert it to:

import importlib.resources as pkg_resources
from win_loss import DB

That gets me: ModuleNotFoundError: No module named 'win_loss'.

So... I'm at a loss. What am I missing?

Steve Pow
  • 21
  • 1
  • You need a file called `__init__.py` to make a directory a module. An empty one will do, but it has to be there. – joanis Jun 01 '23 at 00:56
  • I have added a `__init__.py` in both the win_loss directory, and the DB directory, and it doesn't seem to change anything – Steve Pow Jun 01 '23 at 00:59
  • 1
    I have to admit, this is one of the most infuriating question in Python: how to get started with a project? I now systematically go the next step and create a `setup.py` and install my project using `pip install -e .` on the project. Why there isn't a simpler solution, I don't know... – joanis Jun 01 '23 at 01:02
  • This should help: https://stackoverflow.com/questions/1471994/what-is-setup-py – joanis Jun 01 '23 at 01:03
  • 1
    While it may seem hard to figure out how to create your setup.py now, it's worth it, it'll save you a lot of headaches in the future. – joanis Jun 01 '23 at 01:04
  • That honestly feels like overkill for a bunch of random scripts, isn't? That's going to be annoying as heck if I have to do that just for something like this. – Steve Pow Jun 01 '23 at 01:05
  • Agreed, it is. There might be a simpler solution, but I don't know it. Hopefully someone else here does. – joanis Jun 01 '23 at 01:09
  • 1
    But I believe part of the reason there are so many questions about this on SO is precisely that there is no really simple solution. – joanis Jun 01 '23 at 01:10
  • Fair. Well, no harm in learning setup if that will actually solve this, too – Steve Pow Jun 01 '23 at 01:12

1 Answers1

0

If you don't want to deal with setup.py the easiest thing to do is to set $PYTHONPATH to the current directory. Using your above example if the current working directory is <workspace> you should run

export PYTHONPATH=.

followed by

python win_loss/KeyForgeWL.py.

Edit: to clarify the import within the python script should look like this

import importlib.resources as pkg_resources
from win_loss import DB

as opposed to using the relative import.

  • Be aware: `PYTHONPATH` is an egregious hack only for testing, don't use it for real code. Write an actual `setup.py`. – ShadowRanger Jun 01 '23 at 01:50
  • @ShadowRanger You're right but I don't think its an issue for just running some random scripts locally which is the use case OP is interested in – Fady Nakhla Jun 02 '23 at 20:23