簡體   English   中英

為什么找不到我的python模塊?

[英]Why is my python module not found?

我目前正在學習python。

我在Eclipse中運行pydev。

Eclipse在PYTHONPATH中具有以下文件夾:/ Users / peter / Documents / workspace / python

在該文件夾中,有一個python包:com.peter.minesweeper

在那個文件夾中,我有2個python模塊:mineFileReader.py minesweeper_practice.py

在后一個文件中,我有以下代碼:

from com.peter.minesweeper import mineFileReader

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

但我收到一個錯誤:

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

這沒有任何意義,因為“ python”文件夾位於PYTHONPATH中,並且模塊在聲明它們的位置出現。 我在這里想念什么? 謝謝

我懷疑*您丟失了一個或多個目錄中的__init__.py文件。 該文件使Python知道目錄應視為包。 因此,您的目錄結構應如下所示:

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

我的評論也值得詳述:在Python中通常避免使用不必要的長軟件包名稱。 可能您可以將所有這些放到一個minesweeper包中。


*從技術上講,我知道 ,因為您在評論中是這樣告訴我的;-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM