marcelotduarte / cx_Freeze

cx_Freeze creates standalone executables from Python scripts, with the same performance, is cross-platform and should work on any platform that Python itself works on.
https://marcelotduarte.github.io/cx_Freeze/
Other
1.28k stars 210 forks source link

module 'abc' has no attribute '__file__' #2287

Closed MarceloUnlibreProjects closed 3 months ago

MarceloUnlibreProjects commented 3 months ago

Erro: module 'abc' has no attribute 'file'

script:

import time
try:
    import easyocr
    print('importou')

    reader = easyocr.Reader(['ch_sim','en'], gpu=False)
    result = reader.readtext('chinese.jpg')
    print(result)
    time.sleep(10)
except Exception as e:
    print(e)
    time.sleep(10)

setup teste:

from cx_Freeze import setup, Executable

build_exe_options = {
    "includes": ["easyocr"]
}

executables = [
    Executable(script=r".\asd.py", base=None)
]

setup(  #name = "teste",
        version = "0.8",
        description = "test",
        options = {"build_exe": build_exe_options},
        executables = executables

comando setup: python .\a.py build

comando freeze: cxfreeze --target-name=teste --script=asd.py

image

MarceloUnlibreProjects commented 3 months ago

rodei o exec pelo terminal do pycharm, e deu isso

image

marcelotduarte commented 3 months ago

Eu acabei de testar. Ontem eu havia testado no linux e windows com py 3.10 e rodou aquele sample q usei. Agora testei no py 3.11 e deu erro parecido.

MarceloUnlibreProjects commented 3 months ago

Estranho. Vou tentar usar o 3.10 por enquanto, então. obg.

marcelotduarte commented 3 months ago

No python 3.11 o modulo abc.py é embutido, então não é necessário compilá-lo. Mas já encontrei uma solução, amanhã cedo estará disponível na dev68.

MarceloUnlibreProjects commented 3 months ago

Entendi, vlws

marcelotduarte commented 3 months ago

You can test the patch in the latest development build (dev68): pip install --force --no-cache --pre --extra-index-url https://marcelotduarte.github.io/packages/ cx_Freeze

MarceloUnlibreProjects commented 3 months ago

Quando inicio o exec em outro sistema, fica aparecendo isso, como se n achasse o arquivo do sistema que transformei em exec. Sabe resolver isso?

image

marcelotduarte commented 3 months ago

Se vc me der um exemplo, um pedaço de código em q eu possa reproduzir esse erro, eu consigo resolver sim.

MarceloUnlibreProjects commented 3 months ago

na verdade, é assim que importa o módulo do easyocr, mas em outro sistema (usando VM ou outro pc)

MarceloUnlibreProjects commented 3 months ago

parece que ele tenta procurar pelo arquivo de outro pc, onde eu transformei em EXE

MarceloUnlibreProjects commented 3 months ago

será que da para fazer ele procurar pelos arquivos que estão numa pasta personalizada? No caso, eu botaria dentro da pasta lib, do exe

marcelotduarte commented 3 months ago

parece que ele tenta procurar pelo arquivo de outro pc, onde eu transformei em EXE

A descrição de pasta no pc em que foi compilado é só para referencia de onde estava o original[1]. Digamos q vc tem um um modulo teste (teste.py) q tem a função upsample, esse modulo será copiado como bytecode para lib/teste.pyc, assim o codigo fonte não será encontrado, mas se vc colocar o teste.py ao lado dele, pode ser q o torch o encontre. Pelo q vejo, o torch procuro codigo fonte (.py) e a nossa intenção é NÃO dsitribuí-los.

[1] Para mudar isso tem a opção replace_paths.

marcelotduarte commented 3 months ago

será que da para fazer ele procurar pelos arquivos que estão numa pasta personalizada? No caso, eu botaria dentro da pasta lib, do exe

Sim, usando o include_files

MarceloUnlibreProjects commented 3 months ago

Não entendi bem como funciona o replace_paths, mas vou tentar fazer o que vi em outros lugares, como botando "*", ""

Você acha que o include_files ja resolve esse problema?

no caso, seria só levar o "_jit_internal.py" para o "torch_jit_internal.py", eu acho. Mas eu fiz isso manualmente, agora, e deu na mesma, mas com o caminho atualizado: image

marcelotduarte commented 3 months ago

Então, veja se copiando o _jit_internal.py para lib/torch/_jit_internal.py resolve.... Assim posso fazer automático no hook.

EDIT: Acho q precisa do source code da função procurado, de onde está essa função _DenseLayer.forward

MarceloUnlibreProjects commented 3 months ago

pior que tentei copiar o _jit_internal.py para lá, mas apareceu a mesma mensagem, porem, com o caminho novo: "lib\torch_jit_internal.py:868"

aí tentei procurar o "_DenseLayer" e os outros, mas essas funções não parecem existir

marcelotduarte commented 3 months ago
grep '_DenseLayer' `find . -name '*.py' -print`
./torchvision/models/densenet.py:class _DenseLayer(nn.Module):
./torchvision/models/densenet.py:            layer = _DenseLayer(
./torchvision/models/densenet.py:    # '.'s are no longer allowed in module names, but previous _DenseLayer
MarceloUnlibreProjects commented 3 months ago

eu botei esse .py agora na pasta "lib/torchvision/models/" e parece que a mensagem de "_DenseLayer" parou, porem ainda tem as outras. Uma pena não ter "grep" no windows ;-;

marcelotduarte commented 3 months ago

Eu acho q vc pode copiar todos os .py da pasta "lib/torchvision/models/", veja se resolve....

Quando ao grep: https://pt.stackoverflow.com/questions/215881/qual-%C3%A9-o-equivalente-ao-comando-grep-no-windows

MarceloUnlibreProjects commented 3 months ago

copiei tudo do torchvision, mas ainda tava dando os mesmos erros. Aí copiei a pasta inteira do torch e torchgen sem substituir o que tava lá, e deu esse maldito erro de novo :')

image

marcelotduarte commented 3 months ago

Eu modifiquei o hook, teste novamente. E se der erro, me mostre um traceback com mais linhas.

You can test the patch in the latest development build (dev70): pip install --force --no-cache --pre --extra-index-url https://marcelotduarte.github.io/packages/ cx_Freeze

marcelotduarte commented 3 months ago

@MarceloUnlibreProjects Do you get success?

MarceloUnlibreProjects commented 3 months ago

Na verdade, ainda não

image

MarceloUnlibreProjects commented 3 months ago

esse é todo o traceback:

C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function upsample at 0x0000028D68A6F2E0>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function upsample at 0x0000028D68A6F380>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function interpolate at 0x0000028D68A6F6A0>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function interpolate at 0x0000028D68A6F740>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function interpolate at 0x0000028D68A6F880>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function interpolate at 0x0000028D68A6F9C0>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function upsample_nearest at 0x0000028D68A6FC40>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function upsample_nearest at 0x0000028D68A6FCE0>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function upsample_bilinear at 0x0000028D68A6FF60>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function upsample_bilinear at 0x0000028D68A7C0E0>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function upsample_bilinear at 0x0000028D68A7C180>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function upsample_bilinear at 0x0000028D68A7C220>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function RNN.forward at 0x0000028D68C3B100>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function RNN.forward at 0x0000028D68C3B380>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function LSTM.forward at 0x0000028D68C3BA60>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function LSTM.forward at 0x0000028D68C3BC40>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function GRU.forward at 0x0000028D68C400E0>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function GRU.forward at 0x0000028D68C402C0>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function tensordot at 0x0000028D68CB9760>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function tensordot at 0x0000028D68CB9800>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function tensordot at 0x0000028D68CB9940>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function tensordot at 0x0000028D68CB9A80>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function norm at 0x0000028D68CBA0C0>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function norm at 0x0000028D68CBA160>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function norm at 0x0000028D68CBA2A0>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function norm at 0x0000028D68CBA3E0>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function _DenseLayer.forward at 0x0000028D6DD05580>. C:\Users\a\PycharmProjects\cxfreezeteste.venv\Lib\site-packages\torch_jit_internal.py:868: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function _DenseLayer.forward at 0x0000028D68CBA5C0>. Nem CUDA nem MPS estão disponíveis - o padrão é CPU. Nota: processar imagens é muito mais rápido com uma GPU.

MarceloUnlibreProjects commented 3 months ago

quando botei as pastas do torch, torchgen e torchvision na pasta lib (sem substituir) continuou com isso :/

image

MarceloUnlibreProjects commented 3 months ago

vou tentando outros jeitos até conseguir

marcelotduarte commented 3 months ago

Vc ta usando o build mais recente? Veja com cxfreeze --version se é cxfreeze 6.16.0-dev72

MarceloUnlibreProjects commented 3 months ago

ah não, tava na 69, foi mau

MarceloUnlibreProjects commented 3 months ago

é certo usar assim ou precisa botar mais coisas no package/include?

image

marcelotduarte commented 3 months ago

Nos meus testes, que alias sempre foram bem simples, não precisei usar nem includes, nem packages, uma vez que já trato deles no hooks. Na verdade tem hooks para torch, easyocr e triton (que é uma dependencia do torch). Para o torchvision e para o torchgen não houve ainda problemas relacionados que necessitassem fazer hooks. Mas, vc atualizando para dev72 continua dando o mesmo erro?

MarceloUnlibreProjects commented 3 months ago

fiz os testes aqui, mas ainda ta igual a antes :/

marcelotduarte commented 3 months ago

Tente expandir o exemplo que utilizei ou encontre um exemplo do easyocr que possa ser usado para reproduzir o erro que vou dar mais uma olhada....

MarceloUnlibreProjects commented 3 months ago

Assim, eu só to tentando importar o easyocr mesmo, e da esse problema. Mas eu to tentando rodar o EXEC num sistema diferente do meu, com Virtual Box e outro pc. Usando no meu sistema, funciona normalmente. Só acontece o problema em outro lugar

marcelotduarte commented 3 months ago

Hum... veja isso: https://cx-freeze.readthedocs.io/en/stable/faq.html#microsoft-visual-c-redistributable-package

MarceloUnlibreProjects commented 3 months ago

não funcionou também :/, realmente não faço a minima ideia do que pode ser

MarceloUnlibreProjects commented 3 months ago

ja tentou fazer o script simples com easyocr e executar em outro sistema?

marcelotduarte commented 3 months ago

Oi, eu uso vm para testar também, mas pude fazer 2 testes numa maquina real, e num dos testes realmente apresenta varios 'UserWarning' sim, mas depois processa. O problema é q os arquivos ficam enormes, demora e nem sempre tenho acesso a maquina windows, mas vou tentar verificar mais alguma coisa.

marcelotduarte commented 3 months ago

Vamos lá, usei o seguinte sample: ocr.py

import easyocr
reader = easyocr.Reader(['ch_sim','en'])
result = reader.readtext('chinese.jpg')
print(result)

O arquivo https://github.com/JaidedAI/EasyOCR/blob/master/examples/chinese.jpg

Testei com 2 versões do pytorch (compilei na vm e testei na maquina real q apresentou erros anteriormente): Para instalar pytorch cpu usei: pip install torch torchvision easyocr Para instalar pytorch gpu usei: pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118 pip install easyocr

Para compilar: cxfreeze ocr.py --include-files=chinese.jpg

Mas também pode usar setup.py bem simples.

You can test the patch in the latest development build (dev76): pip install --force --no-cache --pre --extra-index-url https://marcelotduarte.github.io/packages/ cx_Freeze

Me informe se deu certo para vc.

marcelotduarte commented 3 months ago

Release 6.15.16 is out!