Search code examples
pythonpython-3.xmodulenotfounderror

ModuleNotFoundError no module named in Terminal on Mac


I am very new to python and still learning how it all works. I'm building a tiny card game for a python class and decided to build it using Sublime Text and Github to teach myself how it all works.

The (very small) directory looks like this:

/GitHub
├── python-practice
└── war
    ├── __init_.py
    ├── __main__.py
    ├── config
    │   ├── __init__.py
    │   ├── __pycache__
    │   │   ├── card.cpython-38.pyc
    │   │   ├── deck.cpython-38.pyc
    │   │   └── player.cpython-38.pyc
    │   ├── card.py
    │   ├── card.pyc
    │   ├── deck.py
    │   ├── deck.pyc
    │   ├── deck_test.py
    │   └── player.py
    └── game
        ├── __init__.py
        └── logic.py

But no matter if I use the compiler in terminal or the system interpreter in sublime to run logic.py it always comes back:

Traceback (most recent call last):
  File "game/logic.py", line 1, in <module>
    import card, deck, player
ModuleNotFoundError: No module named 'card'

...even though it's clearly there. I've tried from config import card, deck, player and I've also tried import config.

It would be easy and simple to just put all my modules in one folder but I'm trying to teach myself directories so it feels important that I figure this out. How do I get my script to correctly import modules from another folder adjacent to it in the same directory? What am I missing?

edit: adding my code from the modules below.

card.py

values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, 'Nine':9, 'Ten':10, 'Jack':11, 'Queen':12, 'King':13, 'Ace':14}
suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
ranks = ('Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Jack','Queen','King','Ace')

class Card():
    def __init__(self,suit,rank):
        self.suit = suit
        self.rank = rank
        self.value = values[rank]

    def __str__(self):
        return self.rank+" of "+ self.suit

logic.py (unfinished, WIP)

from config.card import Card
from config.deck import Deck
from config.player import Player

#Game setup

player_one = player.Player("One")
player_two = player.Player("Two")

new_deck = deck.Deck()
new_deck.shuffle()


for x in range(26):
    player_one.add_cards(new_deck.deal_one())
    player_two.add_cards(new_deck.deal_one())

game_on = True 

round_num = 0 

while game_on:

    

    if len(player_one.all_cards) == 0:
        print('Player One, out of cards! Player Two Wins!')
        game_on = False
        break
    elif len(player_two.all_cards) == 0:
        print('Player Two, out of cards! Player One Wins!')
        game_on = False
        break
    else:
        round_num+=1
    print(f'Round {round_num}')

    #NEW ROUND
    player_one_cards = []
    player_one_cards.append(player_one.remove_one())
    
    player_two_cards = []
    player_two_cards.append(player_two.remove_one())



    pass

Solution

  • Something like this should work:

    import sys
    from pathlib import Path
    sys.path.append(str(Path('.').absolute().parent))
    
    from config.card import Card
    from config.deck import Deck
    from config.player import Player
    

    This basically allows you to import stuff from the parent directory, which is what you need.