Pyinstaller with Plotly, Orca and Psutils not working


#1

I created a python scritp that uses the principle of the plot.ly’s static image export using orca and psutil. The script works with orca and psutil. Now my goal is to create a standalone exectuable using Pyinstaller. Note that:

  • I installed orca using the standalone binaries and their installer. See the third method of their README-file

  • The pyinstaller creates a dist folder in which an executable can be ran, using some packages (such as plotly in this example) to supply the .exe file with its needed resources

  • I have ran a test without static image export, which worked!

Now, when trying this with orca and psutil I ran into a couple of problems. I edited my .spec file so that

             datas=[('C:/My/Path/to/Python/Python37/Lib/site-packages/plotly/', './plotly/'),
                    ('C:/My/Path/to/Python/Python37/Lib/site-packages/psutil/', './psutil/'),
                    ('C:/My/Path/to/orca/', './orca/')],
             hiddenimports=['plotly', 'orca', 'psutil']

This did move the folders I expected it to move to the dist/main folder, the problem is that it still can’t seem to use orca properly since it gives the standard traceback error where it tells you to install orca.


#2

Hi @ShooterBoyX,

I’m not very familiar with PyInstaller, but here are some general things to look at.

By default, plotly.py is looking for an executable name orca on the system PATH. The error message you mentioned above should have printed out the current directories in the PATH, but if not you can examine these paths by looking at the output of:

import os
os.environ['PATH']

Can you confirm that this list of paths does not include the directory where you placed the orca executable?

One option is to manually add your orca directory to this path at program startup.

os.environ['PATH'] = os.pathsep.join([os.environ['PATH'], '/path/to/pyinstalled/orca/'])

(Note: '/path/to/pyinstalled/orca_dir/' should be the directory containing the orca.exe file, and you might need to flip the slash direction on windows)

Another option is the tell plotly.py exactly where to find orca with:

import plotly.io as pio
pio.orca.config.executable = '/path/to/pyinstalled/orca/orca.exe'

See https://plot.ly/python/orca-management/#configuring-the-executable for more information.

Hope that helps!
-Jon


#3

Thank you for your quick reply @jmmease !

Since I also would like to run the script using python main.py, the secondary option editing the pio.orca.config.executable wasn’t a good option.

Therefore I decided to stick with adding the orca directory to the path variable at the program setup, which worked!

For that, I used the relative path of the executable created by python as described here, which made the final code look like this

# Relative path location of orca for usage with pyinstaller
import sys
import os 
orca_folder = 'orca'
# Determine if application is a script file or frozen exe
if getattr(sys, 'frozen', False):
    application_path = os.path.dirname(sys.executable)
elif __file__:
    application_path = os.path.dirname(__file__)
orca_path = os.path.join(application_path, orca_folder)
os.environ['PATH'] = os.pathsep.join([os.environ['PATH'], orca_path])

Using this code and pyinstaller with the above given edited .spec file the build worked when testing on other devices without python or orca installed.