Lỗi 'ImportError (No module named ex47.game)' khi chạy code Ex47 (LPTHW)
Mình đang học theo cuốn “Learn Python The Hard Way” thì làm đến Ex47 thì vướng lỗi này ?
"
PS C:Python27ExerciseEx47> nosetests
E
ERROR: Failure: ImportError (No module named ex47.game)
Traceback (most recent call last):
File “c:python27libsite-packages
oseloader.py”, line 418, in loadTestsFromName
addr.filename, addr.module)
File “c:python27libsite-packages
oseimporter.py”, line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File “c:python27libsite-packages
oseimporter.py”, line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File “C:Python27ExerciseEx47 estsex47_tests.py”, line 2, in
from ex47.game import Room
ImportError: No module named ex47.game
Ran 1 test in 0.032s
FAILED (errors=1)
"
Mong mọi người giúp đỡ
Mình đã tham khảo trong topic này
[image] folder của mình đầy đủ theo hướng dẫn của bài tập ex47 rồi bạn à, nhờ bạn chỉ dẫn giúp mình với, cảm ơn nhiều
và một số topic khác nhưng vẫn chưa khắc phục được.
Up code của bạn lên di.
Code của mình y chang trong sách luôn á bạn
file game.py
1 class Room(object):
2
3 def init(self, name, description):
4 self.name = name
5 self.description = description
6 self.paths = []
7
8 def go(self, direction):
9 return self.paths.get(direction, None)
10
11 def add_paths(self, paths):
12 self.paths.update(paths)
file ex47_tests.py
1 from nose.tools import *
2 from ex47.game import Room
3
4
5 def test_room():
6 gold = Room(“GoldRoom”,
7 “”“This room has gold in it you can grab. There’s a
8 door to the north.”"")
9 assert_equal(gold.name, “GoldRoom”)
10 assert_equal(gold.paths, [])
11
12 def test_room_paths():
13 center = Room(“Center”, “Test room in the center.”)
14 north = Room(“North”, “Test room in the north.”)
15 south = Room(“South”, “Test room in the south.”)
16
17 center.add_paths([‘north’: north, ‘south’: south])
18 assert_equal(center.go(‘north’), north)
19 assert_equal(center.go(‘south’), south)
20
21 def test_map():
22 start = Room(“Start”, “You can go west and down a hole.”)
23 west = Room(“Trees”, “There are trees here, you can go east.”)
24 down = Room(“Dungeon”, “It’s dark down here, you can go up.”)
Sơ đồ là :
Ex47\skeleton
Directory: C:\Python27\Exercise\Ex47\skeleton
PS C:\Python27\Exercise\Ex47\skeleton> ls -R
Mode LastWriteTime Length Name
d----- 4/16/2018 8:41 PM bin
d----- 4/12/2018 4:56 PM docs
d----- 4/16/2018 8:40 PM Ex47
d----- 4/16/2018 8:40 PM tests
-a---- 4/13/2018 2:29 PM 405 setup.py
Mode LastWriteTime Length Name
-a---- 4/12/2018 4:58 PM 0 init.py
Mode LastWriteTime Length Name
-a---- 4/16/2018 11:35 AM 305 game.py
-a---- 4/12/2018 4:58 PM 0 init.py
-a---- 4/16/2018 8:40 PM 138 init.pyc
Mode LastWriteTime Length Name
-a---- 4/16/2018 12:05 PM 1045 ex47_tests.py
-a---- 4/16/2018 8:40 PM 1638 ex47_tests.pyc
-a---- 4/12/2018 4:58 PM 0 init.py
-a---- 4/16/2018 8:40 PM 139 init.pyc
Không có module nào là ex47.game cả, bạn chỉ có file
game.py
thì trong fileex47_tests.py
phải gọiNó báo lỗi tương tự bạn ơi
Mình có tham khảo một số topic thì nó vẫn để như vậy vẫn được
Sửa thành
xem sao. Có vẻ như tên folder là
Ex47
, phải import đúng code mới chịu chạy.Mình mới sửa tên folder lại thành “ex47”, thì nó lại ra lỗi này
ERROR: tests.ex47_tests.test_room_paths
Traceback (most recent call last):
File “c:\python27\lib\site-packages\nose\case.py”, line 197, in runTest
self.test(*self.arg)
File “C:\Python27\Exercise\ex47\skeleton\tests\ex47_tests.py”, line 17, in test_room_paths
center.add_paths({‘north’: north, ‘south’: south})
File “C:\Python27\Exercise\ex47\skeleton\ex47\game.py”, line 12, in add_paths
self.paths.update(paths)
AttributeError: ‘list’ object has no attribute ‘update’
======================================================================
ERROR: tests.ex47_tests.test_map
Traceback (most recent call last):
File “c:\python27\lib\site-packages\nose\case.py”, line 197, in runTest
self.test(*self.arg)
File “C:\Python27\Exercise\ex47\skeleton\tests\ex47_tests.py”, line 25, in test_map
down.add_paths({‘up’, start})
File “C:\Python27\Exercise\ex47\skeleton\ex47\game.py”, line 12, in add_paths
self.paths.update(paths)
AttributeError: ‘list’ object has no attribute ‘update’
======================================================================
FAIL: tests.ex47_tests.test_room
Traceback (most recent call last):
File “c:\python27\lib\site-packages\nose\case.py”, line 197, in runTest
self.test(*self.arg)
File “C:\Python27\Exercise\ex47\skeleton\tests\ex47_tests.py”, line 10, in test_room
assert_equal(gold.paths, {})
AssertionError: [] != {}
Ran 3 tests in 0.023s
FAILED (errors=2, failures=1)
Không có attribute update với kiểu list. Lỗi này do ban đầu bạn gán
Sửa thành
Cảm ơn bạn nha
ERROR: tests.ex47_tests.test_map
Traceback (most recent call last):
File “c:\python27\lib\site-packages\nose\case.py”, line 197, in runTest
self.test(*self.arg)
File “C:\Python27\Exercise\ex47\skeleton\tests\ex47_tests.py”, line 27, in test_map
down.add_paths({‘up’, start})
File “C:\Python27\Exercise\ex47\skeleton\ex47\game.py”, line 12, in add_paths
self.paths.update(paths)
TypeError: cannot convert dictionary update sequence element #0 to a sequence
Ran 3 tests in 0.024s
FAILED (errors=1)
Giờ còn duy nhất lỗi này
Phải là
:
chứ không phải là,
.Xem ra kiến thức về kiểu dữ liệu, đặc biệt là dict của bạn còn chưa được tốt lắm. Đọc thêm:
Được rồi, mình cãm ơn bạn nhiều nha
Tại mình cũng mới học nên hơi mông lung :)))
Sẵn tiện cho mình hỏi luôn
from nose.tools import *
from ex47.game import Room
Vậy là nó import hàm * từ nose.tool hay sao vậy ?
Và nếu import file thì mình “tên folder . tên file” hả bạn ?
Bạn cũng thấy chẳng có hàm nào có tên là * (theo quy tắc đặt tên)
from <tên thư viện> import *
có nghĩa là bạn sẽ gọi ra tất cả các thư viện con và hàm trong thư viện<tên thư viện>
với<tên thư viện>
là 1 thư viện bất kì nhưmath
,os
,sys
,…Về cơ bản là vậy.
P/s: Bạn có vẻ chưa học cơ bản kĩ đã nhảy đến ex47 rồi thì phải? Nếu bạn đã học kĩ thì đến bài này bạn phải khá chắc về kiến thức cơ bản rồi.