Search code examples
pythonimagetkinterexepy2exe

python 3.4 py2exe image.open cannot find the images


I am trying to create an executable file of my GUI (which uses 12 images from my computer) but when I create it, the .exe does not run in another computer because it cannot find those 12 images

I have a tree like this

project (folder)
--mycode.py (python file)
--setup.py (python file)
--images (folder)
  --image1 (image .jpg)
  --image2 (image .jpg)
  --image3 (image .jpg)

then I use py2exe to create the .exe and my tree is now

project (folder)
--__pycache__ (folder)
  --mycode.cpython-34.pyc
--dist (folder)
  --mycode.exe (executable file for windows)
  --(and other 22 elements)
--mycode.py (python file)
--setup.py (python file)
--images (folder)
  --Im_0.jpg
  --Im_1.jpg
  --Im_2.jpg
  --...

now

I am using these to find my images

import os
scriptDir = os.path.dirname(os.path.realpath(__file__))
IM0 = os.path.join(scriptDir, 'Images\\Im_0.jpg')
....

then I create the .exe

however when I run the executable it it says

"NameError: name '__file__' is not defined" 

then I try to go to http://www.py2exe.org/index.cgi/WhereAmI because py2exe doesn't have a file attribute

they say use

import os
import jpath

if hasattr(sys,"frozen") and sys.frozen in ("windows_exe", "console_exe"):
  p=jpath.path(os.path.abspath(sys.executable)).dirname()

some other people say

import JPath class
import JPath.engine #@UnresolvedImport    
import jpath.syntax
from jpath.data import Boolean, Item, List, Map, Null, Number, Pair, String
import jpath.errors as errors
from jpath.utils.messages import JPATH_INTERNAL_ERROR_MESSAGE
from jpath.utils.text import trimTo
import jpath.evaluators
import re
from linearclassification.lib.utils import jpath,chunk,contains,has_subsequence

but python show this message

ImportError: No module named 'JPath'

now I do not know what to do to run my code importing the images that are in the same folder where the .exe is as my tree shows


Solution

  • done, the answer is this one

    first in mycode.py I put

    p=os.path.abspath(sys.executable)
    IM0 = os.path.join('Images\\Im_0.jpg')
    IM1 = os.path.join('Images\\Im_1.jpg')
    IM2 = os.path.join('Images\\Im_2.jpg')
    ...
    

    then I create the .exe by calling mycode.py with setup.py like this

    from distutils.core import setup
    import py2exe
    setup(
     name="mycode",
     windows =['mycode.py'],
     options = {"py2exe": {"packages": ["encodings"]}}
     )
    

    then I go to command prompt change the directory to the folder where setup.py and mycode.py are and latter I write

    python setup.py py2exe
    

    which create 2 folders

    --__pycache__ (folder)
      --mycode.cpython-34.pyc
    --dist (folder)
      --mycode.exe (executable file for windows)
      --(and other 22 elements)
    

    now inside dist (folder) I paste the folder Images (folder) where all my images are and

    --dist (folder)
      --mycode.exe (executable file for windows)
      --(and other 22 elements)
      --Images (folder)
        --Im_0.jpg
        --Im_1.jpg
        --Im_2.jpg
        --....
    

    Done

    if somebody has a better method please tell me

    thanks anyway