Home > database >  ModuleNotFoundError: No module named 'createCredentialsXML'
ModuleNotFoundError: No module named 'createCredentialsXML'

Time:12-28

I have a question about my new python project. It is the first time that I use different folders for my project.

I have the following structure:

project
  src
    securityFunc
      __init__.py
      createCredentialsXML.py
    main.py

I work in PyCharm environment. After pressing Play i get the error message:

Traceback (most recent call last):
  File "C:\project\src\main.py", line 1, in <module>
    from securityFunc import *
  File "C:\project\src\securityFunc\__init__.py", line 1, in <module>
    from createCredentialsXML import *
ModuleNotFoundError: No module named 'createCredentialsXML'

My main function looks like this:

from securityFunc import *

if __name__ == '__main__':
    generate_key()

__init__.py:

from createCredentialsXML import *

createCredentialsXML.py:

def generate_key():
    key = base64.urlsafe_b64encode(os.urandom(2048))

    with open("../key/secret.key", "wb") as key_file:
        key_file.write(key)

I tried using Path or sys.path to fix the problem. But it does not work.

Can you please tell me how to fix the problem?

CodePudding user response:

createCredentialsXML.py works in the module securityFunc; you must specify the scope of the import. Using

from securityFunc.createCredentialsXML import *

in securityFunc.__init__.py should work.

CodePudding user response:

Since you're doing a relative import, you need to add a . before your module name:

from .createCredentialsXML import *

The dot means the module is found in the same directory as the code importing the code.

You can read more about it here

  • Related