I've spent hours researching this problem, and I'm still baffled. Please find my ignorance charming.
I'm building a python program that will allow me to pit two AIs against each other in a game of battleship.
Here's my directory structure:
.
├── ais_play_battleship
│ ├── game.py
│ ├── __init__.py
│ ├── player.py
│ └── ship.py
├── LICENSE
├── README.md
└── tests
└── ship_test.py
2 directories, 7 files
Currently, I'm trying to write ship_test.py, but I cannot seem to import ais_play_battleship.ship. I get the dreaded "ModuleNotFoundError"
Here's what my research has taught me about my problem:
- If you want to import python code from another directory, you should make that directory a package instead of a module. Therefore, I've placed an
__init__.pyfile in the root ofais_play_battleship. - Python will only search the directory python is launched from as well as the directory of the script you're running. Therefore, I've been trying to launch my tests by running
python3 tests/ship_tests.pyfrom the root directory.
Here are my specific questions:
- Why is the error a "ModuleNotFound" error? Shouldn't it be "PackageNotFound"?
- Am I correct to make ais_play_battleship a package?
- How can I keep my tests in a separate directory and still use the code in ais_play_battleship?
Please forgive me, as I'm not very good at asking questions on StackOverflow. Please tell me how I can improve.
pathto includeais_play_battleship.import syssys.path.insert(0, '/path/to/application/app/folder')Also, I thought that if I was running the program from the parent directory, I didn't need to. Is this wrong?