importlib.import_module with python
Question:
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:PythonPython310libimportlib__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'
Answers:
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!
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:PythonPython310libimportlib__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'
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!