howto instantiate certain class from string in python project packed by pyinstaller? -


i using getattr, cant make working pyinstaller

short summary:

  • python 3.5.1 |anaconda 4.0.0 (64-bit)| (default, feb 16 2016, 09:49:46) [msc v.1900 64 bit (amd64)] on win32
  • i have project, packed pyinstaller single file
  • i have external text file script (subs.py)

my project cant instantiate class subs in subs.py ..

before pack project pyinstaller, can create instance of subs subs.py.. can please advise, whats wrong on example below ?

in order show problem, prepared simple example. folder structure: root- #folder -to #folder -__init__.py #file -subs.py #file -main.py #file

main.py:

import importlib  myclass = getattr(importlib.import_module("to.subs"), "subs") instance = myclass() instance.test() 

subs.py:

class subs():     def test(self):         print("test") 

at moment, execution return expected:

python main.py test 

but if pack project pyinstaller ( latest installed via pip ) pyinstaller main.py -f moving main.exe root , executing:

main.exe traceback (most recent call last):   file "main.py", line 3, in <module>     myclass = getattr(importlib.import_module("to.subs"), "subs")   file "importlib\__init__.py", line 126, in import_module   file "<frozen importlib._bootstrap>", line 986, in _gcd_import   file "<frozen importlib._bootstrap>", line 969, in _find_and_load   file "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked   file "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed   file "<frozen importlib._bootstrap>", line 986, in _gcd_import   file "<frozen importlib._bootstrap>", line 969, in _find_and_load   file "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked importerror: no module named 'to' failed execute script main 

any idea, missing/doing wrogly ? ?

"you right. using --hidden-import=subs won't work. have use -p dir option , point folder includes modules. if fails, can try bundle files additional data. – aug 8 @ 14:31 " marking answer, successfuly instan.class using --hidden-import & -b...

credit goes ->repiklis


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -