[Python] Pyinstaller does not import python files and also not import packages called from...

Discussão em 'Python' iniciado por Stack, Março 22, 2025.

  1. Stack

    Stack Membro Participativo

    I see this type of question has been asked, however, in my case pyinstaller seems to be importing my other module (readxml.py) into my app.py, but it does not import the packages such as 'from bs4 import BeautifulSoup', I am importing in the readxml.py.

    So to give a brief background, I have an app.py that simply displays all the contents of an xml in an html.Div if it is opened using dcc.Upload dialog box. From app.py, I call another module that I coded (readxml.py) that simply parses the xml and sends the string back to app.py for display. So call from app.py is:

    soup1 = xmlread.readSoupVar(loaddata)


    Both xmlread.py and app.py are in the same directory at the same level:

    C:\Desktop\ReadXMLTest


    Now, everything works fine if I am running it locally, however, when I try to create an exe, the following issues occur:


    1. It does not recognize xmlread.py and does not build along with app.py. I have tried resolving this issue by hard-coding the path into app.py (three lines I have numbered below), but I think there is a better approach of doing this:
      1. import sys
      2. sys.path.append('C:/Desktop/ReadXMLTest')
      3. import xmlread

    Then in the app.spec file, I edit hiddenimports and pathex to the following:

    import sys;
    sys.setrecursionlimit(sys.getrecursionlimit() * 5)

    a = Analysis(
    ['app.py'],
    pathex=['C:\\Desktop\\ReadingXML'],
    binaries=[],
    datas=[],
    hiddenimports=['xmlread.py'],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
    optimize=0,
    )
    pyz = PYZ(a.pure)

    exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.datas,
    [],
    name='app',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
    )


    It appears to "see" the xmlread.py, but when it gets to the parsing part of app.py, I get the following error:

    FROM readSoupVar:
    [2025-03-22 09:20:36,671] ERROR in app: Exception on /_dash-update-component [POST]
    Traceback (most recent call last):
    File "flask\app.py", line 1473, in wsgi_app
    File "flask\app.py", line 882, in full_dispatch_request
    File "flask\app.py", line 880, in full_dispatch_request
    File "flask\app.py", line 865, in dispatch_request
    File "dash\dash.py", line 1405, in dispatch
    File "dash\_callback.py", line 529, in add_context
    File "dash\_callback.py", line 518, in add_context
    File "dash\_callback.py", line 47, in _invoke_callback
    File "app.py", line 414, in update_output1
    if xmlread.readSoupVar(loaddata):
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "xmlread.py", line 195, in readSoupVar
    File "bs4\__init__.py", line 250, in __init__
    bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: xml. Do you need to install a parser library?


    1. That leads to my second question, how do I build beautifulsoup or any other package which is being imported by the xmlread.py into my app.exe.


    2. I have an image that I display as a header in my app.py. It is at the same level as both python files in the same folder, but I get the error from app.exe that it could not find the image. But, if I place the image in dist folder afterwards at the same level as the app.exe, the error is not thrown.

      image_filename = 'maglassedited.png' encoded_image = base64.b64encode(open(image_filename, 'rb').read())

    How do I resolve that? should I just add 'maglassedited.png' in the app.spec as follows:

    hiddenimports=['xmlread.py', 'maglassedited.png'],

    1. I have an assets folder with html files that I read and display showing instructions in the app.py. That assets folder (which contains html files) is at the same directory level of app.py and readxml.py. How do I include this folder?

    Lastly, here are the commands I am using to create the exe:

    C:\Desktop\ReadingXML>C:\anaconda3\Scripts\pyinstaller.exe --onefile app.py

    C:\Desktop\ReadingXML>C:\anaconda3\Scripts\pyinstaller.exe app.spec


    Thank you for your help in advance!

    Continue reading...

Compartilhe esta Página