ModuleNotFoundError Mac 终端中没有命名模块
ModuleNotFoundError no module named in Terminal on Mac
我是 python 的新手,仍在学习它的工作原理。我正在为 python class 构建一个小型纸牌游戏,并决定使用 Sublime Text 和 Github 构建它来自学它是如何工作的。
(非常小的)目录如下所示:
/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
但无论我是在终端中使用编译器还是在 sublime 中使用系统解释器 运行 logic.py 它总是会返回:
Traceback (most recent call last):
File "game/logic.py", line 1, in <module>
import card, deck, player
ModuleNotFoundError: No module named 'card'
...即使它显然存在。我试过 from config import card, deck, player
我也试过 import config
.
将我所有的模块放在一个文件夹中会很容易和简单,但我正在努力自学目录,所以我觉得弄清楚这一点很重要。 如何让我的脚本从同一目录中与其相邻的另一个文件夹中正确导入模块?我错过了什么?
编辑:从以下模块添加我的代码。
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(未完成,开发中)
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
像这样的东西应该可以工作:
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
这基本上允许您从父目录导入内容,这正是您所需要的。
我是 python 的新手,仍在学习它的工作原理。我正在为 python class 构建一个小型纸牌游戏,并决定使用 Sublime Text 和 Github 构建它来自学它是如何工作的。
(非常小的)目录如下所示:
/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
但无论我是在终端中使用编译器还是在 sublime 中使用系统解释器 运行 logic.py 它总是会返回:
Traceback (most recent call last):
File "game/logic.py", line 1, in <module>
import card, deck, player
ModuleNotFoundError: No module named 'card'
...即使它显然存在。我试过 from config import card, deck, player
我也试过 import config
.
将我所有的模块放在一个文件夹中会很容易和简单,但我正在努力自学目录,所以我觉得弄清楚这一点很重要。 如何让我的脚本从同一目录中与其相邻的另一个文件夹中正确导入模块?我错过了什么?
编辑:从以下模块添加我的代码。
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(未完成,开发中)
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
像这样的东西应该可以工作:
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
这基本上允许您从父目录导入内容,这正是您所需要的。