0

Hello there I would like some to address me properlly on how to use importlib.import_module

I am attempting to import a module using the lib: but what I am getting is:

this is the example of my class:

class Main:
    def __init__:
        self.appFileName=None
        self.appFileToPass=None


    def decrypt(self, string):
        return cryptocode.decrypt(string, localKey)

    def decrypFile(self, file):

        # decrpts the file 
        newFile=""
        with open(file) as f:
            line = f.readline()
            while line:
                line = f.readline()
                newFile+=str(self.decrypt(line))


        # creats a random name 
        alphaName=f"{''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase) for _ in range(random.randint(9,12)))}"
        self.appFileName=f"{alphaName}.py"
        self.appFileToPass=alphaName

        # save file locally 
        with open(self.appFileName, 'w') as myprints:
            myprints.write(newFile)
        myprints.close()
        

    def run(self):
        self.decrypFile("fileToDecrypt")
        app=importlib.import_module(self.appFileName)

if __name__ == "__main__":
    main=Main()
    main.run()

the error :

Exception ignored in thread started by: <bound method runApp of <__main__.my class>
Traceback (most recent call last):
  File "C:\Myfile.py", line 4747, in runApp
    app=importlib.import_module(self.appFileName))
  File "C:Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'appR'

1 Answer 1

1

This is an excerpt from the error I received:

ModuleNotFoundError: No module named 'eWtZWhAbv.py'; 'eWtZWhAbv' is not a package

So, it looks for a module eWtZWhAbv.py.
From the file system perspective, this module corresponds to the ./eWtZWhAbv/py.py file and not ./eWtZWhAbv.py.

This happens because the dot is a delimiter between package members.
The solution then would be to swap eWtZWhAbv.py with eWtZWhAbv (without extension).

To fix this, remove the .py extension:

--- app=importlib.import_module(self.appFileName)
+++ appModule = os.path.splitext(self.appFileName)[0]
+++ app = importlib.import_module(appModule)

This should make the trick!

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

3 Comments

Thank you Pavel, do you have another approch in mind, by any chance?, I have tried with self.appFileToPass and with your suggestiong and still getting ane error now this time is different: SyntaxError: invalid syntax (audfZzjFhC.py, line 526)
In this case, the target file seems to be incorrect. You can check it by manually importing it into an app or interactive session - and you should see the same error.
you are correct, I will fix the target file. and apply your solution

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.