Python + PyCharm File Structure issue: AttributeError: #39;module#39; object has no attribute #39;X#39;(Python + PyCharm 文件结构问题:AttributeError:“模块对象没有属性“X)
问题描述
我的文件结构如下:
- main.py
- 加密货币
- GetGenerators.py
- RecHash.py
- ToInteger.py
- Utils.py
GetGenerators.py 看起来像这样:
GetGenerators.py looks like this:
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 中使用函数
GetGenerators
时,它工作正常.但是,当我通过右键单击文件在 GetGenerators.py 中运行单元测试"来运行GetGenerators.py
UnitTests 时,我收到以下错误:When I'm using the function
GetGenerators
from inside main.py, it works fine. However, when I'm running theGetGenerators.py
UnitTests by rightclicking the file, "Run Unittests in GetGenerators.py", I'm getting the following error:文件C:Program Files (x86)JetBrainsPyCharm 2016.3.2helperspycharm ose_helperutil.py",第 70 行,在 resolve_nameobj = getattr(obj, part)
File "C:Program Files (x86)JetBrainsPyCharm 2016.3.2helperspycharm ose_helperutil.py", line 70, in resolve_name obj = getattr(obj, part)
AttributeError: 'module' 对象没有属性 'GetGenerators'
AttributeError: 'module' object has no attribute 'GetGenerators'
我想这与我的文件结构有关,但我没有发现问题.
I suppose it has something to do with the structure of my files, but I don't see the problem.
推荐答案
我以前没有遇到过您的确切问题,但我想我遇到过类似的问题.当我使用 PyCharm 时,我发现如果打开并使用我在 PyCharm 的项目中创建的文件,那么一切正常.我可以导入它们,可以运行它们;没问题.我遇到的问题(与您的问题相似)是当我打开一个不是在 PyCharm 项目中创建的文件时.我无法导入它们,有时甚至无法正确运行它们.也许只是我很愚蠢,或者可能是 PyCharm 的一个真正的错误,但无论如何.在 PyCharm 中创建一个项目并将文件内容复制并粘贴到您在 PyCharm 中创建的文件中可能值得(如果您还没有的话).出于某种原因,这在过去对我有用.
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. 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. 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. 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.
这篇关于Python + PyCharm 文件结构问题:AttributeError:“模块"对象没有属性“X"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python + PyCharm 文件结构问题:AttributeError:“模块&
基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01