简体   繁体   English

为什么找不到我的python模块?

[英]Why is my python module not found?

I am learning python currently. 我目前正在学习python。

I am running pydev in eclipse. 我在Eclipse中运行pydev。

Eclipse has the following folder in the PYTHONPATH: /Users/peter/Documents/workspace/python Eclipse在PYTHONPATH中具有以下文件夹:/ Users / peter / Documents / workspace / python

In that folder, there is a python package of: com.peter.minesweeper 在该文件夹中,有一个python包:com.peter.minesweeper

in that folder i have 2 python modules: mineFileReader.py minesweeper_practice.py 在那个文件夹中,我有2个python模块:mineFileReader.py minesweeper_practice.py

in the latter file i have the following code: 在后一个文件中,我有以下代码:

from com.peter.minesweeper import mineFileReader

reader = mineFileReader(pathToMinesFile = "minesweeper.mines.txt")

but i am getting an error: 但我收到一个错误:

Traceback (most recent call last):
File "/Users/peter/Documents/workspace/java/python/com/peter/minesweeper/minesweeper_practice.py", line 6, in <module>
    from com.peter.minesweeper import mineFileReader
ImportError: No module named minesweeper

This doesn't make sense because the 'python' folder is in the PYTHONPATH and the modules appear where they are declared. 这没有任何意义,因为“ python”文件夹位于PYTHONPATH中,并且模块在声明它们的位置出现。 What am i missing here? 我在这里想念什么? thanks 谢谢

I suspect * that you're missing the __init__.py files in one or more of your directories. 我怀疑*您丢失了一个或多个目录中的__init__.py文件。 This file is what lets Python know that a directory should be considered a package. 该文件使Python知道目录应视为包。 So your directory structure should look like this: 因此,您的目录结构应如下所示:

python
  |-- com
      |-- __init__.py
      |-- peter
          |-- __init__.py
          |-- minesweeper
              |-- __init__.py
              |-- mineFileReader.py
              |-- minesweeper_practice.py

Also worth elaborating on my comment: needlessly long package names are generally avoided in Python. 我的评论也值得详述:在Python中通常避免使用不必要的长软件包名称。 Probably you could put all this in one minesweeper package. 可能您可以将所有这些放到一个minesweeper包中。


*Well, technically I know , since you told me so in the comments ;-) *从技术上讲,我知道 ,因为您在评论中是这样告诉我的;-)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM