0

Python import * from string

so i need to import a module from a string. here is the situation. i have a program, that need import a module. but the module is encrypted. so i have to open the module as read binary and decrypt it. than??? after i decrypt it, how can i make its a real module? the answer i know: using exec or eval but that is a bad practice, i think.

24th Feb 2018, 1:00 PM
Kevin AS
Kevin AS - avatar
4 Réponses
+ 10
Ok, then after you've decrypted it, you can save it to a file, import it and delete it every time when it is not needed anymore. Although probably a better way is to use meta import hooks, described here: https://docs.python.org/3/reference/import.html You can build your own class to import strings (from a read, decrypted file) as modules: https://stackoverflow.com/questions/14191900/pythonimport-module-from-memory
25th Feb 2018, 6:07 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
What do you mean the module is 'encrypted'? In any case - since you have a file which is the Python syntax module, you can save it to a file and then import it normally. Just I don't think I get what the problem is here... :)
24th Feb 2018, 5:05 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
@kuba after following those instruction, finally i must use exec. because we cant use "from module import * " in a class. i dont know why, the error is SyntaxError: import * is only allowed in module level. so i thinking about to use vars function strr = "module" vars() [strr] = __import__("my_module") but it cant be done in a method. i think its a bugs. or i missing something? my try: #in a class def yay(self, strr): name = strr.rstrip(".py") vars() [name] = __import__(name) self.mod = name #error name is not declared
26th Feb 2018, 1:59 AM
Kevin AS
Kevin AS - avatar
0
@kuba i encrypt it. its a big project and i want to hide it. so i encrypt it :v
25th Feb 2018, 12:50 AM
Kevin AS
Kevin AS - avatar