1

I'm having the following file structure:

  • main.py
  • Crypto
    • GetGenerators.py
  • Utils
    • RecHash.py
    • ToInteger.py
    • Utils.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()

When I'm using the function GetGenerators from inside main.py, it works fine. However, when I'm running the GetGenerators.py UnitTests by rightclicking the file, "Run Unittests in GetGenerators.py", I'm getting the following error:

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)

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.

5
  • I was sloppy reading your question, it's not the Utils you can't import, it's that it doesn't find the GetGenerators method, but you have not included where you call it? Commented May 16, 2017 at 20:57
  • And I've removed relevant code in the unittest, sorry. I'm calling GetGenerators() from the UnitTest method testGetGenerators() Commented May 16, 2017 at 21:01
  • And they're defined in this order as well in your file (and of course in the same file)? Commented May 16, 2017 at 21:03
  • The functions are defined in this exact order yes. Commented May 16, 2017 at 21:04
  • It makes no sense, but then it seems to have been a very random issue with pycharm rather than the code. Commented May 16, 2017 at 21:24

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That could very well be the problem, because I could swear this exact setup has worked once on another computer maybe running a different version of PyCharm. I'm going to recreate the project and test it. Thanks
Creating a new project and copying the files from the old one into it actually solved the problem!
I solved this after reading your comment. However, the folder where my test file is in was "Mark directory as -> Source Folder" by right-clicking on the folder. Thanks.
0

So I've ran into a similar problem with PyCharm 2022.2.2 and this solution didn't help me. 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.

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. The problem was resolved, but the issue with file structure is there for PyCharm even 6 years later.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.