Reputation: 43
I've looked all over for a day or so and am about ready to pull my hair out. Help me, SO, you're my only hope.
I have found countless solutions to this issue that boil down to "make sure you have __init__.py in appropriate places and it should just work."
I am sure that I am missing something obvious, this is a noob question.
Anyways -
I have added what __init__ files I believe should exist.
Here is the code for my unit test:
import unittest
from pyrum.src import Card
class TestCard(unittest.TestCase):
def setUp(self):
pass
def test_Card_Initalization(self):
#Tests that numeric position is set properly for various options.
self.assertTrue(Card("S","A").position==1)
self.assertTrue(Card("S","2").position==2)
self.assertTrue(Card("S","J").position==11)
self.assertTrue(Card("S","Q").position==12)
self.assertTrue(Card("S","K").position==13)
#Tests that an exception is raised when an invalid suit is passed.
with self.assertRaises(Exception) as context:
Card("Z","Z")
self.assertTrue('Suit Z provided does not match recongized suit code. Options are: [S: Spades, H: Hearts, C: Clubs, D: Diamonds]' in str(context.exception))
def test_attack(self):
pass
def tearDown(self):
pass
It is under construction, but this is it.
Whatever way I call test_Card.py (have tried a few different ways/contexts), I still get
Traceback (most recent call last):
File "/workspaces/VSCodeDockerTest/pyrum/test/test_Card.py", line 2, in <module>
from pyrum.src import Card
ModuleNotFoundError: No module named 'pyrum'
But to my understanding, it should be able to find pyrum no problem. When I place the card and test modules in the same folder (and change the import accordingly) it works fine.
Can someone please explain to me what I am missing? I am at my wits end here, and don't know what else I could search or look at to find a solution.
Thanks!
Upvotes: 0
Views: 197
Reputation: 43
Here is my own solution. If someone has a better answer as to what is going on, I am all ears.
From this related Q&A:
My module has __init__.py and still Python can't import it
It seems like the issue was that python doesn't handle paths how I think it does? I was able to fix it by running
cd ..
export PYTHONPATH=.
and then running
python ./pyrum/tests/runner.py
(runner.py runs all my tests in turn)
I also removed the __init__.py files from src and test, but I am not sure that that, specifically, was necessary or helpful.
I am curious how to avoid having to do that each time I want to run my tests without permanently modifying my path. I am developing on Docker so I am less concerned if that's the best solution (I can just write a bash script to do those 3 things if I really have to...) but I'd like a clean and portable solution, if anyone has something better. I am assuming using a virtual environment would alleviate some of this issue, so I will probably try that in the future as my next random thing to try.
Now it's working everywhere except the debugger.
Upvotes: 1
Reputation: 677
As you are importing pyrum.src
, you are considering it as a module. Therefore, you have to use -m option.
Example: if your pyrum
folder is at a/b/c/pyrum
, stand on a/b/c
and call
python -m pyrum.test.test_Card # without .py extension
Upvotes: 0