Gabriel Dante
Gabriel Dante

Reputation: 412

How do I indicate a specific module to be imported to PyInstaller?

I'm trying to create an application using PyInstaller, and it works if I copy and paste the plotly folder from the python libraries source to the folder where my application is located. However, if I don't do so, the program doesn't work.

I've already tried using hidden imports, as shown below in my .spec file.

# -*- mode: python ; coding: utf-8 -*-

import sys
sys.setrecursionlimit(5000)

block_cipher = None

options = []


a = Analysis(['application.py'],
             pathex=['C:\\path_to_application', \
             'C:\\path_to_python_libs'],
             binaries=[],
             datas=[ ('logo.png', '.') ],
             hiddenimports=['plotly.offline', 'plotly.graph_objs', 'datetime', 'time'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)

pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          options,
          [],
          exclude_binaries=True,
          name='application',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=False,
          console=True)
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=False,
               upx_exclude=[],
               name='application')

I get the following error (because the plotly folder hasn't been created):

Traceback (most recent call last):
  File "vibration_analysis.py", line 255, in <module>
  File "waterfall_FFT2.py", line 249, in waterfall_plot
  File "site-packages\plotly\offline\offline.py", line 558, in plot
  File "site-packages\plotly\io\_html.py", line 482, in write_html
  File "site-packages\plotly\io\_html.py", line 279, in to_html
  File "site-packages\plotly\offline\offline.py", line 87, in get_plotlyjs
  File "pkgutil.py", line 637, in get_data
  File "c:\users\bergon julien\anaconda3\lib\site-packages\PyInstaller\loader\py
imod03_importers.py", line 475, in get_data
    with open(path, 'rb') as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\path_to_application\\plotly\\package_data\\plotly.min.js'

Upvotes: 0

Views: 337

Answers (1)

Masoud Rahimi
Masoud Rahimi

Reputation: 6031

You can add your App dependencies as data files with add-data flag. Also if you want to add a whole directory, you can use Tree class. So edit your spec file and add the Tree class for ploty:

# -*- mode: python ; coding: utf-8 -*-

import sys
sys.setrecursionlimit(5000)

block_cipher = None

options = []


a = Analysis(['application.py'],
            ...
             noarchive=False)
a.datas += Tree('<path_to_ploty_dir>', prefix='./ploty')
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
...

And generate your executable with pyinstaller application.spec.

Upvotes: 1

Related Questions