简体   繁体   English

ImportError:没有名为ex47.game的模块(是的,我有一个__init__.py文件*并且*我尝试了所有PYTHONPATH的东西

[英]ImportError: No module named ex47.game (and yes, I have an __init__.py file *AND* I tried all that PYTHONPATH stuff

So I'm trying to finish the "Learn Python the Hard Way" exercise 47, and I created the skeleton directory and created the files in the exercise to do the automated tests. 因此,我尝试完成“学习硬道法Python”练习47,并创建了骨架目录并在练习中创建了文件以进行自动化测试。 However, when I try to run the ex47_tests.py, I get this little gem: 但是,当我尝试运行ex47_tests.py时,我得到了这个小宝石:

Traceback (most recent call last):
  File "ex47_tests.py", line 2, in <module>
ImportError: No module named ex47.game

So I went back and checked my directories to see if I had an init .py file in the same directory that the "game.py" file was in (it's there), and discarded the possibility of a missing init file. 因此,我回过头来检查目录,以查看是否存在与“ game.py”文件所在的目录相同的init .py文件(位于该目录中),并丢弃了缺少该init文件的可能性。 Tried to run it again, and no doing. 试图再次运行它,但无济于事。 Then, I went online and looked up the problem (By the way, that dude who wrote that LPTHW book is, umm, quite lacking when it comes to teaching details that you really need to know, and his teaching really starts to fall apart from Chapter 42 onwards), and I read about using the various PYTHONPATH methods to get PYTHONPATH to look for the file in the ex47 folder. 然后,我上网查看了问题所在(顺便说一句,那位写了LPTHW书的老兄,嗯,在您真正需要了解的教学细节方面非常缺乏,他的教学真正开始与众不同从第42章起),我了解了如何使用各种PYTHONPATH方法获取PYTHONPATH来查找ex47文件夹中的文件。 I tried putting in "PYTHONPATH=. python tests/ex47_tests.py", and I still got that error. 我尝试放入“ PYTHONPATH =。python tests / ex47_tests.py”,但仍然出现该错误。 Then I tried putting in the alternate "python -m tests.ex47_tests" line. 然后,我尝试放入备用“ python -m tests.ex47_tests”行。 Still, no dice. 不过,没有骰子。 As a final resort, I tried to look at code from people who completed the exercise, and looked over the code in the book, and double/triple-checked it against my code, and I saw no differences. 作为最后的选择,我尝试从完成练习的人员那里查看代码,然后查看书中的代码,并与我的代码进行两次/三次检查,但没有发现任何不同。

So my question is, how exactly do I get Python to actually take the blindfold off and see that there actually IS a file called "game.py" in the directory "ex47" and get it to import it when I run the ex47_tests file? 所以我的问题是,究竟如何,我得到的Python采取实际眼罩关闭,看到有实际上一个在目录“ex47”称为“game.py”文件,并把它导入它,当我运行ex47_tests文件? I've tried just about every answer related to the subject, and nothing. 我已经尝试了几乎所有与该主题相关的答案,但没有尝试。

Here's my directory structure, for reference: 这是我的目录结构,以供参考:

bin docs ex47 setup.py tests

./bin:

./docs:

./ex47:
game.py  game.pyc  __init__.py  __init__.pyc

./tests:
ex47_tests.py  ex47_tests.py~  ex47_tests.pyc  __init__.py  __init__.pyc

And here is the snippet of code calling the game file: 这是调用游戏文件的代码片段:

from nose.tools import *
from ex47.game import Room

If anyone can help me get past this (and maybe understand why this error keeps popping up when it seems like it has no reason to), I would really appreciate it. 如果有人可以帮助我解决这个问题(也许可以理解为什么似乎没有理由要继续出现此错误),我将不胜感激。

PS: I also tried running the "nosetests" from the project directory, and sometimes it would work (but in less time than the exercise specified), and sometimes it would give an error saying it couldn't import the game.py file. PS:我还尝试从项目目录运行“ nosetests”,有时它可以工作(但比指定的练习时间短),有时它会报错,说它无法导入game.py文件。 But no matter what the nosetest, I still can't get the ex47_tests.py file to import the game file. 但是,无论测试如何,我仍然无法获取ex47_tests.py文件来导入游戏文件。

By request, I'm going to re-post this in the form of an answer. 根据要求,我将以答案的形式重新发布。

SO I solved my problem. 所以我解决了我的问题。 Apparently, the way to use PYTHONPATH to get it to read everything in the directory is to be in the directory above where all the files are (the project directory, in this case), and type in "export PYTHONPATH=." 显然,使用PYTHONPATH来读取目录中所有内容的方法是在所有文件所在的目录中(在本例中为项目目录),然后键入“ export PYTHONPATH =”。 Basically, set the PYTHONPATH to "." 基本上,将PYTHONPATH设置为“”。

The solution suggested for Windows did not work for me, so I replaced the top two lines of ex47_tests.py with the following four lines: 针对Windows建议的解决方案对我不起作用,因此我用以下四行替换了ex47_tests.py的前两行:

  • import sys 导入系统
  • sys.path.insert(0, 'C:\\Users\\User\\Test\\projects\\ex47\\ex47') sys.path.insert(0,'C:\\ Users \\ User \\ Test \\ projects \\ ex47 \\ ex47')
  • import game 导入游戏
  • from nose.tools import * 从鼻子。工具进口*
  • from game import Room 来自游戏导入室

This appears to have resolved any issues with the nosetests etc. 这似乎解决了鼻子测试等任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ImportError:没有名为…的模块(但是我有__init__.py文件并设置了PYTHONPATH) - ImportError: No module named … (but I have __init__.py files and have set PYTHONPATH) ImportError:__init__.py文件中没有名为…的模块 - ImportError: No module named… in __init__.py file ImportError:我也制作了 __init__.py 文件 - ImportError: I have made __init__.py file as well Python3错误“ImportError:没有名为的模块”有__init__.py - Python3 error “ImportError: No module named” have __init__.py ImportError:当包含___init__.py时,没有名为test的模块 - ImportError: No module named test when ___init__.py is included ImportError:在__init__.py文件Python中导入类时没有名为''的模块 - ImportError: No module named ' ' while Import class in the __init__.py file Python Python - 没有名为错误的模块。 无论我将 __init__.py 文件放在哪里,摄取器文件似乎都无法访问它 - Python- no module named error. wherever i place the __init__.py file the ingestor file just cant seem to get at it LPTHW Ex:由于PythonPath导致47 ImportError - LPTHW Ex: 47 ImportError due to PythonPath __init__.py文件是模块吗? - Is the __init__.py file a module? py2exe ImportError:没有命名的模块<package I have impemented> - py2exe ImportError: no module named <package I have impemented>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM