I've been reorganizing my project because there was an issue somewhere, but, as programming always goes, the problem is now 10 times worse and everything is broken.
My current file tree that I am satisfied with is:
Amazons AI
- .git
- Game_Code
- __pycache__
- game.py
- lib
- __pycache__
- __init__.py (empty)
- motion.py
- pieceManagement.py
- tests
- __pychache__
- test_game.py
- README.md
My issue is that in game.py
(in the Game_Code
folder, I need to import motion.py
and pieceManagement.py
(both in the lib
directory).
I've tried multiple ways to go up a level in the directory, import lib and then everything from that, largely using the suggestions in Import a file from a subdirectory?, but nothing has worked. For reference, I am using Python 3.7.3.
To access libs
in parent directory of the current file, one can do this:
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "/libs")
It adds parent dictory + /libs
to the sys path where python will know to look for it as described in Python - what is the libs subfolder for?. However, I don't like this solution as it leads to ugly code like this:
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "/libs")
from pieceManagement import piece
import motion
So I'd still like to find a pythonic way to do it, maybe inline with the import statements. But I know this works (on my machine).