Background:
I am trying to convert a python script to an executable file that can be used on other computers. I decided to use PyInstaller. I am using Python 2.7.13/Anaconda 2.2.0 (64-bit). I have of course seen many examples and I can achieve this for basic examples. However, the script I am now working on uses environmental variables. The following block of code appears at the start of my python script:
import os
# force qt4
os.environ['ETS_TOOLKIT'] = 'qt4'
os.environ['QT_API'] = 'pyqt'
from traits.api import HasTraits, Range, Instance, Button, on_trait_change, Bool, Str, Enum, Float, Int
from traitsui.api import View, Item, Group, HGroup, spring, Handler, Action, InstanceEditor, Menu, MenuBar, message, \
Tabbed
from mayavi.core.ui.api import MayaviScene, SceneEditor, MlabSceneModel
from pyface.api import FileDialog, OK
import yaml
from mayavi import mlab
import numpy as np
from collections import namedtuple
import gdal
Problem:
Running pyinstaller --onefile filename.py
on the command line produces an .exe
, but it doesn't run because of the error: ImportError: No module named qt4
."qt4" isn't a module, so I am assuming the problem is with the line " os.environ['ETS_TOOLKIT'] = 'qt4
'".
After looking at various questions related to PyInstaller, I know how to use 'hiddenimports', but I have no idea how to handle environmental variables. Obviously something like pyinstaller --onefile --hidden-import qt4 filename.py
didn't work.
There are multiple solutions: you ask each developer to set the value in their environment before launching the application. you add some logic at the application's initialization to use the API key environment variable value if it exists, otherwise, fall back to the plain configuration file.
To set an environment variable, use the command " export varname=value ", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces. To set a local variable, use the command " varname =value " (or " set varname =value ").
PyInstaller can bundle your script and all its dependencies into a single executable named myscript ( myscript.exe in Windows). The advantage is that your users get something they understand, a single executable to launch.
You should try pyinstaller --onefile --hidden-import "os" filename.py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With