01/10/2018, 15:25

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.

HK boy viết 17:30 ngày 01/10/2018

Up code của bạn lên di.

Trần Nho Đức viết 17:35 ngày 01/10/2018

Code của mình y chang trong sách luôn á bạn

Trần Nho Đức viết 17:25 ngày 01/10/2018

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

Directory: C:\Python27\Exercise\Ex47\skeleton

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

Directory: C:\Python27\Exercise\Ex47\skeleton\bin

Mode LastWriteTime Length Name


-a---- 4/12/2018 4:58 PM 0 init.py

Directory: C:\Python27\Exercise\Ex47\skeleton\Ex47

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

Directory: C:\Python27\Exercise\Ex47\skeleton\tests

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

HK boy viết 17:33 ngày 01/10/2018

ex47.game

Không có module nào là ex47.game cả, bạn chỉ có file game.py thì trong file ex47_tests.py phải gọi

from game import ...
Trần Nho Đức viết 17:31 ngày 01/10/2018

Nó 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

HK boy viết 17:40 ngày 01/10/2018

Sửa thành

from Ex47.game import ...

xem sao. Có vẻ như tên folder là Ex47, phải import đúng code mới chịu chạy.

Trần Nho Đức viết 17:37 ngày 01/10/2018

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)

HK boy viết 17:34 ngày 01/10/2018

AttributeError: ‘list’ object has no attribute ‘update’

Không có attribute update với kiểu list. Lỗi này do ban đầu bạn gán

self.paths = []

Sửa thành

self.paths = {}
Trần Nho Đức viết 17:28 ngày 01/10/2018

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

HK boy viết 17:29 ngày 01/10/2018

{‘up’, start}

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:

Giới thiệu về Dict trong Python Dict(Dictionary) cũng là một container như LIST, TUPLE. Có điều khác biệt là những container như List, Tuple có các index để phân biệt các phần tử thì Dict dùng các key để phân biệt. Chắc bạn cũng dùng từ điển tiếng Anh để tra từ vựng rồi nhỉ? Có rất nhiều từ vựng trong đó nhưng mà không từ vựng nào giống nhau. Có chăng chúng chỉ giống nhau về nghĩa? Và đó cũng như Dict(Dictionary) hoạt động trong Python Một Dict gồm các yếu tố sau: Được giới hạn bởi cặp n…
Giới thiệu về phương thức của kiểu dữ liệu Dict trong Python Kiểu dữ liệu Dict có hỗ trợ một số phương thức và đa số là xử lí các dữ liệu có trong Dict. Mình mong các bạn sẽ hiểu rõ được các phương thức để sau này áp dụng vào giải quyết các vấn đề với việc viết ít code nhất, hạn chế lỗi nhất. Các phương thức tiện ích Phương thức copy Cú pháp: <Dict>.copy() Giống với phương thức copy trong LIST. Để làm gì thì chắc các bạn cũng có thể suy nghĩ ra. Ví dụ: >>> d = {'team': 'Kteam',…
Trần Nho Đức viết 17:33 ngày 01/10/2018

Đượ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 ?

HK boy viết 17:39 ngày 01/10/2018

Vậy là nó import hàm * từ nose.tool hay sao vậy ?

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à nếu import file thì mình “tên folder . tên file” hả bạn ?

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.

Bài liên quan
0