When I run my code from Pyinstaller the tiff reader works fine. After freezing using Pyinstaller I get the following warning:
UserWarning: ImportErr
Installation:
->If using conda, conda install tifffile -c conda-forge
-> Otherwise, pip install tifffile
I actually seen this via upwork whilst I was just browsing around the net and decided to have a play around.
kazemakase was on the right track at the very start, when you run normally, the __package__ is None, and when its packaged __package__ is set to tifffile and the first condition is executed, and it becomes relative to the module tifffile.
if __package__:
from . import _tifffile
else:
import _tifffile
I just converted tifffile to a module manually, by creating in site-packages a tifffile folder, creating an empty __init__.py file in the new folder, placing tifffile.py, _tifffile.pyd from site-packages into the tifffile folder and changing the import statement, admittedly in a simple skeleton.
import tifffile.tifffile as tiff
If this helps across your whole project I don't know. And should note I used the wheel from http://www.lfd.uci.edu/~gohlke/pythonlibs/ to install originally to save the compilation step so your mileage may vary. I did the above initially on 2.7 but it also appears to work fine on 3.5 from some testing I was able to do. And didn't need to put anything in .spec files when I tested from the one originally generated.
From inspecting the code, tifffile.py
seems to be looking for a module called _tifffile
, which presumably is the expected name of the compiled C extension:
try:
if __package__:
from . import _tifffile
else:
import _tifffile
except ImportError:
warnings.warn(
"ImportError: No module named '_tifffile'. "
"Loading of some compressed images will be very slow. "
"Tifffile.c can be obtained at http://www.lfd.uci.edu/~gohlke/")
The tifffile.cpython-35.pyc
is just the bytecode generated from tiffile.py
. You don't need to bother with that one.
The .c
file alone won't help you, either. It needs to be compiled to to create a usable Python extension, which needs to be named something like _tiffile.cp35-win_amd64.pyd
(can vary depending on your system and python version/installation) so it can be used by import _tifffile
.
Compiling can be a daunting task if you have not done this before. If you feel you are up to it, the Python documentation can help you get started. You will need to have the same Compiler and settings that your Python version was compiled with.
However, there may be a simpler solution. If your code works fine before freezing, chances are you have the compiled extension correctly installed on your system. Pyinstaller probably misses it because it can find tifffile.py
and is satisfied. Look for the correct .pyd
file in your Python directories and see if you can modify the .spec file Pyinstaller created for your project, where you specify to include the .pyd
file.
I think muggy is right about he weirdness with __package__
causing the issue here. I haven't tracked down the exact reason for the fix, but this seems to be resolved using the latest update to pyinstaller
. Check your version with:
→ pyinstaller --version
3.2.1
and upgrade with
→ pip3 install --upgrade pyinstaller
The update was only made on January 15, 2017 so this wouldn't have helped when you originally asked, but it sure helps now.