简体   繁体   English

Python + PyCharm 文件结构问题:AttributeError: 'module' object 没有属性 'X'

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

I'm having the following file structure:我有以下文件结构:

  • main.py主程序
  • Crypto加密货币
    • GetGenerators.py获取生成器.py
  • Utils工具
    • RecHash.py RecHash.py
    • ToInteger.py ToInteger.py
    • Utils.py工具.py

GetGenerators.py looks like this: 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()

When I'm using the function GetGenerators from inside main.py, it works fine.当我在 main.py 中使用 function GetGenerators时,它工作正常。 However, when I'm running the GetGenerators.py UnitTests by rightclicking the file, "Run Unittests in GetGenerators.py", I'm getting the following error:但是,当我通过右键单击文件“在 GetGenerators.py 中运行单元测试”来运行GetGenerators.py单元测试时,出现以下错误:

File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pycharm\nose_helper\util.py", line 70, in resolve_name obj = getattr(obj, part)文件“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 has no attribute 'GetGenerators' AttributeError: 'module' object 没有属性 'GetGenerators'

I suppose it has something to do with the structure of my files, but I don't see the problem.我想这与我的文件结构有关,但我没有看到问题。

I haven't had your exact problem before, but I think I've had one like it.我以前没有遇到过您的确切问题,但我想我遇到过类似的问题。 When I use PyCharm, I find that if open and use files that I've created in a project in PyCharm, then everything works fine.当我使用 PyCharm 时,我发现如果打开并使用我在 PyCharm 中的项目中创建的文件,则一切正常。 I can import them, can run them;我可以导入它们,可以运行它们; no problems.没问题。 The problems I run into (which are similar to yours) are when I open a file that was not created within a PyCharm project.我遇到的问题(与您的类似)是当我打开一个不是在 PyCharm 项目中创建的文件时。 I can't import them, and sometimes can't even run them correctly.我无法导入它们,有时甚至无法正确运行它们。 Maybe it's just me being stupid or maybe a real bug with PyCharm, but whatever the case is.也许这只是我的愚蠢或者 PyCharm 的真正错误,但无论如何。 It might be worth (if you haven't already), create a project in PyCharm and copy and paste the file contents into files you create within PyCharm. For some reason, that has worked for me in the past.可能值得(如果您还没有的话),在 PyCharm 中创建一个项目并将文件内容复制并粘贴到您在 PyCharm 中创建的文件中。出于某种原因,这在过去对我有用。

So I've ran into a similar problem with PyCharm 2022.2.2 and this solution didn't help me.所以我遇到了与 PyCharm 2022.2.2 类似的问题,这个解决方案对我没有帮助。 Instead, what worked was checking my code to make sure I didn't have any object named 'module' defined anywhere, plus I changed some of the documents like "face_landmarks.py" and "face_recognition.py" into "landmarks.py" to avoid confusion when calling a similar line with face_recognition package in python.相反,有效的是检查我的代码以确保我没有在任何地方定义任何名为“module”的 object,另外我将一些文件如“face_landmarks.py”和“face_recognition.py”更改为“landmarks.py”避免在 python 中调用与 face_recognition package 类似的线路时混淆。

I've also tried marking the project folder as a Namespace package. However, as I've done several things at once, I'm not sure if this had any impact.我还尝试将项目文件夹标记为命名空间 package。但是,由于我同时做了几件事,我不确定这是否有任何影响。 The problem was resolved, but the issue with file structure is there for PyCharm even 6 years later.问题已解决,但即使在 6 年后,PyCharm 的文件结构问题仍然存在。

暂无
暂无

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

相关问题 site.py:AttributeError:在PyCharm中运行任何python文件时,“模块”对象没有属性“ ModuleType” - site.py: AttributeError: 'module' object has no attribute 'ModuleType' upon running any python file in PyCharm AttributeError:“模块”对象没有属性“ maketrans”-PyCharm - AttributeError: 'module' object has no attribute 'maketrans' - PyCharm Python安装问题? AttributeError:“模块”对象没有属性“ MINYEAR” - Python installation issue? AttributeError: 'module' object has no attribute 'MINYEAR' AttributeError:'module'对象没有属性'[x]' - AttributeError: 'module' object has no attribute '[x]' Python:AttributeError模块x没有属性y - Python: AttributeError module x has no attribute y 无法在PyCharm 5.0中调试Python脚本:AttributeError:“模块”对象没有属性“ default_getpass” - Can't debug Python script in PyCharm 5.0: `AttributeError: 'module' object has no attribute 'default_getpass'` Pycharm python AttributeError:模块'urllib3'没有属性'Request' - Pycharm python AttributeError: module 'urllib3' has no attribute 'Request' Python:AttributeError:'module'对象没有属性'socketpair' - Python: AttributeError: 'module' object has no attribute 'socketpair' Python AttributeError:“模块”对象没有属性“获取” - Python AttributeError: 'module' object has no attribute 'get' python - AttributeError:'module'对象没有属性'lock' - python - AttributeError: 'module' object has no attribute 'lock'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM