簡體   English   中英

Python + PyCharm 文件結構問題:AttributeError: 'module' object 沒有屬性 'X'

[英]Python + PyCharm File Structure issue: AttributeError: 'module' object has no attribute 'X'

我有以下文件結構:

  • 主程序
  • 加密貨幣
    • 獲取生成器.py
  • 工具
    • RecHash.py
    • ToInteger.py
    • 工具.py

GetGenerators.py 看起來像這樣:

import unittest
import os, sys
import gmpy2
from gmpy2 import mpz

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from Utils.Utils           import AssertInt, AssertClass
from Utils.ToInteger       import ToInteger
from Utils.RecHash         import RecHash    

def GetGenerators(n):
    AssertInt(n)
    assert n >= 0, "n must be greater than or equal 0"

    generators = []

    # ... irrelevant code...

    return generators


class GetGeneratorsTest(unittest.TestCase):
    def testGetGenerators(self):
        self.assertEqual(len(GetGenerators(50)), 50)


if __name__ == '__main__':
    unittest.main()

當我在 main.py 中使用 function GetGenerators時,它工作正常。 但是,當我通過右鍵單擊文件“在 GetGenerators.py 中運行單元測試”來運行GetGenerators.py單元測試時,出現以下錯誤:

文件“C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pycharm\nose_helper\util.py”,第 70 行,在 resolve_name obj = getattr(obj, part)

AttributeError: 'module' object 沒有屬性 'GetGenerators'

我想這與我的文件結構有關,但我沒有看到問題。

我以前沒有遇到過您的確切問題,但我想我遇到過類似的問題。 當我使用 PyCharm 時,我發現如果打開並使用我在 PyCharm 中的項目中創建的文件,則一切正常。 我可以導入它們,可以運行它們; 沒問題。 我遇到的問題(與您的類似)是當我打開一個不是在 PyCharm 項目中創建的文件時。 我無法導入它們,有時甚至無法正確運行它們。 也許這只是我的愚蠢或者 PyCharm 的真正錯誤,但無論如何。 可能值得(如果您還沒有的話),在 PyCharm 中創建一個項目並將文件內容復制並粘貼到您在 PyCharm 中創建的文件中。出於某種原因,這在過去對我有用。

所以我遇到了與 PyCharm 2022.2.2 類似的問題,這個解決方案對我沒有幫助。 相反,有效的是檢查我的代碼以確保我沒有在任何地方定義任何名為“module”的 object,另外我將一些文件如“face_landmarks.py”和“face_recognition.py”更改為“landmarks.py”避免在 python 中調用與 face_recognition package 類似的線路時混淆。

我還嘗試將項目文件夾標記為命名空間 package。但是,由於我同時做了幾件事,我不確定這是否有任何影響。 問題已解決,但即使在 6 年后,PyCharm 的文件結構問題仍然存在。

暫無
暫無

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

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