I am trying to complete Exercise 47 of Learn Python The Hard Way, 2nd Edition, but I am receiving this error when I run tests/ex47_tests.py
:
我正在努力完成学习Python的第47题,第2版,但是当我运行测试/ex47_test .py时,我正在接收这个错误。
File "tests/ex47_tests.py", line 3, in <module>
from ex47.game import Room
ImportError: No module named ex47.game
I thought it was something I was doing wrong within my code because I am very new at this, so I cloned this repo from a GitHub user who seems to have successfully completed the exercise.
我认为我在代码中做错了一些事情,因为我在这方面很新,所以我从一个GitHub用户那里克隆了这个repo,他似乎已经成功地完成了这个练习。
Not only are the relevant parts of our code identical, but I receive the same error when I try to run the tests/ex47_tests.py
that I cloned from him. So now I am lost and hoping that someone has a solution for me. Any ideas?
不仅我们代码的相关部分是相同的,而且当我尝试运行测试/ex47_tests时也会收到相同的错误。是我从他身上克隆出来的。所以现在我迷路了,希望有人能帮我解决这个问题。什么好主意吗?
6 个解决方案
#1
3
from the repository directory :
从存储库目录:
PYTHONPATH=. python tests/ex47_tests.py
Make sure there are no other ex47.py files/packages in your path.
确保没有其他的ex47。路径中的py文件/包。
#2
5
fabrizioM's answer should get it to work. Here is a little explanation.
法布里齐奥的答案应该能奏效。这里有一个小解释。
When Python loads a file, it searches on the filesystem. So here, we have the import statement:
当Python装载一个文件时,它在文件系统上进行搜索。这里,我们有导入语句:
from ex47.game import Room
It looks for the file ex47.py on the modules search path (accessible as sys.path in Python code). The modules search path contains some directories based on the installation details of Python, the directories listed in the PYTHONPATH environment variable, and contains the parent directory of the script you’re executing. It doesn't find ex47.py on the path, but it sees there is a directory named ex47 with __init__.py
inside of it. It then finds game.py in that folder.
它查找文件ex47。py在模块搜索路径上(可作为sys访问)。在Python代码路径)。模块搜索路径包含一些基于Python安装细节的目录,这些目录在PYTHONPATH环境变量中列出,并且包含您正在执行的脚本的父目录。它没有找到ex47。在路径上的py,但是它看到有一个名为ex47的目录和__init__。py里面。然后发现游戏。py文件夹。
The problem is that your current folder is not on the modules search path. Because ex47_tests.py was run, it has $cwd/tests on the path. You need $cwd on the path.
问题是当前文件夹不在模块搜索路径上。因为ex47_tests。py被运行,它在路径上有$cwd/test。您需要$cwd在路径上。
PYTHONPATH=. python tests/ex47_tests.py
does exactly that. It puts the $cwd on the modules search path so Python can find the source files.
就是这么做的。它将$cwd放在模块搜索路径上,以便Python能够找到源文件。
You can also do:
你也可以做的事:
python -m tests.ex47_tests
This will run it as a module instead of a file, while it will use the current directory as path it adds automatically to the the modules search path instead of the directory the file is located inside.
它将作为一个模块而不是文件运行,而它将使用当前目录作为自动添加到模块搜索路径的路径,而不是文件所在的目录。
#3
3
The book asks you to copy the 'skeleton' directory and then use it for the game room exercise (#47). The skeleton directory has a "NAME" directory.
这本书要求你复制“骨架”目录,然后将它用于游戏室练习(第47条)。骨架目录有一个“NAME”目录。
So if you copied the skeleton directory and named it ex47, you'll have another ex47 directory inside that.
如果你复制了骨架目录并将它命名为ex47,你会在其中有另一个ex47目录。
lpthw > ex47 > ls
bin docs ex47 setup.py tests
So when the book says "Next, create a simple file ex47/game.py where you can put the code to test", you assume that it is the top level ex47. Not correct! That's why the import won't resolve.
所以当书说“下一步,创建一个简单的文件ex47/游戏”。您可以将代码放在哪里测试“,您假定它是*别的ex47。不正确的!这就是为什么导入不会解析。
I hit the same problem too until I realized that the book calls the outer directory (the one we got by copying 'skeleton') as 'simplegame' evident from this line in the book -
我也遇到了同样的问题,直到我意识到这本书把外层目录(我们复制“骨架”得到的目录)称为“简单游戏”,从书中的这一行可以看出
~/projects/simplegame $ nosetests
So all the answers about PYTHONPATH are valid, I just wanted to explain why the import wouldn't work for you!
所有关于PYTHONPATH的答案都是有效的,我只是想解释为什么这个导入不能为你工作!
#4
1
There is something wrong with your directory structure. Here is my directory structure:
目录结构有问题。以下是我的目录结构:
bin docs ex47 setup.py tests
./bin:
./docs:
./ex47:
game.py game.pyc __init__.py __init__.pyc
./tests:
ex47_tests.py ex47_tests.pyc __init__.py __init__.pyc
#5
0
The project is named ex47 and there is a folder named ex47 within it. (provided you have named your files according to his plan laid out before game.py file's code in this exercise) When the author says create a simple file ex47/game.py he means the folder within is where the game.py file should be created and stored.
项目名为ex47,其中有一个名为ex47的文件夹。(前提是你已经按照游戏前他的计划命名了你的文件。当作者说创建一个简单的文件ex47/游戏时,py文件的代码。他的意思是里面的文件夹就是游戏的地方。应该创建并存储py文件。
#6
-1
I too had the same error; being new to python, I followed the book example and after copying the skeleton directory over, renamed any files containing 'NAME' - one existed in the test/ directory and once removed the unable to load error disappeared - check the answer/contents in Microivan directory post above. So within the project directory only two python files should exist, game.py and ex47_tests.py - hope you got there, on to the next exercise.!!
我也有同样的错误;作为python的新手,我遵循了本书的示例,在复制了骨架目录之后,重新命名了任何包含“NAME”的文件——一个存在于测试/目录中,一旦删除了无法加载的错误就消失了——检查上面Microivan directory post中的答案/内容。所以在项目目录中应该只有两个python文件存在,game。py和ex47_tests。但愿你能做到,开始下一个练习。
#1
3
from the repository directory :
从存储库目录:
PYTHONPATH=. python tests/ex47_tests.py
Make sure there are no other ex47.py files/packages in your path.
确保没有其他的ex47。路径中的py文件/包。
#2
5
fabrizioM's answer should get it to work. Here is a little explanation.
法布里齐奥的答案应该能奏效。这里有一个小解释。
When Python loads a file, it searches on the filesystem. So here, we have the import statement:
当Python装载一个文件时,它在文件系统上进行搜索。这里,我们有导入语句:
from ex47.game import Room
It looks for the file ex47.py on the modules search path (accessible as sys.path in Python code). The modules search path contains some directories based on the installation details of Python, the directories listed in the PYTHONPATH environment variable, and contains the parent directory of the script you’re executing. It doesn't find ex47.py on the path, but it sees there is a directory named ex47 with __init__.py
inside of it. It then finds game.py in that folder.
它查找文件ex47。py在模块搜索路径上(可作为sys访问)。在Python代码路径)。模块搜索路径包含一些基于Python安装细节的目录,这些目录在PYTHONPATH环境变量中列出,并且包含您正在执行的脚本的父目录。它没有找到ex47。在路径上的py,但是它看到有一个名为ex47的目录和__init__。py里面。然后发现游戏。py文件夹。
The problem is that your current folder is not on the modules search path. Because ex47_tests.py was run, it has $cwd/tests on the path. You need $cwd on the path.
问题是当前文件夹不在模块搜索路径上。因为ex47_tests。py被运行,它在路径上有$cwd/test。您需要$cwd在路径上。
PYTHONPATH=. python tests/ex47_tests.py
does exactly that. It puts the $cwd on the modules search path so Python can find the source files.
就是这么做的。它将$cwd放在模块搜索路径上,以便Python能够找到源文件。
You can also do:
你也可以做的事:
python -m tests.ex47_tests
This will run it as a module instead of a file, while it will use the current directory as path it adds automatically to the the modules search path instead of the directory the file is located inside.
它将作为一个模块而不是文件运行,而它将使用当前目录作为自动添加到模块搜索路径的路径,而不是文件所在的目录。
#3
3
The book asks you to copy the 'skeleton' directory and then use it for the game room exercise (#47). The skeleton directory has a "NAME" directory.
这本书要求你复制“骨架”目录,然后将它用于游戏室练习(第47条)。骨架目录有一个“NAME”目录。
So if you copied the skeleton directory and named it ex47, you'll have another ex47 directory inside that.
如果你复制了骨架目录并将它命名为ex47,你会在其中有另一个ex47目录。
lpthw > ex47 > ls
bin docs ex47 setup.py tests
So when the book says "Next, create a simple file ex47/game.py where you can put the code to test", you assume that it is the top level ex47. Not correct! That's why the import won't resolve.
所以当书说“下一步,创建一个简单的文件ex47/游戏”。您可以将代码放在哪里测试“,您假定它是*别的ex47。不正确的!这就是为什么导入不会解析。
I hit the same problem too until I realized that the book calls the outer directory (the one we got by copying 'skeleton') as 'simplegame' evident from this line in the book -
我也遇到了同样的问题,直到我意识到这本书把外层目录(我们复制“骨架”得到的目录)称为“简单游戏”,从书中的这一行可以看出
~/projects/simplegame $ nosetests
So all the answers about PYTHONPATH are valid, I just wanted to explain why the import wouldn't work for you!
所有关于PYTHONPATH的答案都是有效的,我只是想解释为什么这个导入不能为你工作!
#4
1
There is something wrong with your directory structure. Here is my directory structure:
目录结构有问题。以下是我的目录结构:
bin docs ex47 setup.py tests
./bin:
./docs:
./ex47:
game.py game.pyc __init__.py __init__.pyc
./tests:
ex47_tests.py ex47_tests.pyc __init__.py __init__.pyc
#5
0
The project is named ex47 and there is a folder named ex47 within it. (provided you have named your files according to his plan laid out before game.py file's code in this exercise) When the author says create a simple file ex47/game.py he means the folder within is where the game.py file should be created and stored.
项目名为ex47,其中有一个名为ex47的文件夹。(前提是你已经按照游戏前他的计划命名了你的文件。当作者说创建一个简单的文件ex47/游戏时,py文件的代码。他的意思是里面的文件夹就是游戏的地方。应该创建并存储py文件。
#6
-1
I too had the same error; being new to python, I followed the book example and after copying the skeleton directory over, renamed any files containing 'NAME' - one existed in the test/ directory and once removed the unable to load error disappeared - check the answer/contents in Microivan directory post above. So within the project directory only two python files should exist, game.py and ex47_tests.py - hope you got there, on to the next exercise.!!
我也有同样的错误;作为python的新手,我遵循了本书的示例,在复制了骨架目录之后,重新命名了任何包含“NAME”的文件——一个存在于测试/目录中,一旦删除了无法加载的错误就消失了——检查上面Microivan directory post中的答案/内容。所以在项目目录中应该只有两个python文件存在,game。py和ex47_tests。但愿你能做到,开始下一个练习。