pythonpy2exepython-docx

packageError py2exe and python-docx


I have a program written in python that reads certain datafiles and produces a docx file and a pdf file. Now I need to distribute this, so I created a GUI and found that with py2exe I can create the .exe that I should then be able to distribute.

The .exe is created in the dist folder as it should be, and the GUI looks good too, but when I actually let the program do what it is supposed to do, I get the following error:

Package not found at 'C:\Users\heinreca\PycharmProjects\RaTeExe\dist\library.zip\docx\templates\default.docx'

And yes indeed there is no templates folder. I've been reading several stackoverflow posts and been googeling around, but have not found a solution yet.

One suggested solution was, to specify the data_files in my setup.py file like this:

data_files = [('template', 'C:/Users/heinreca/PycharmProjects/RaTeExe/venv/docx-template/*'), ]

setup(windows=['RaTe.py'],
      options={
          'py2exe':
              {
                  'includes': ['lxml.etree', 'lxml._elementpath', 'gzip'],
              }
                },
        data_files=data_files
      )

But I get "No such file or directory: 'C'"

I tried doing this as a relative path with just \venv\docx-template* but this also doesn't work.

Another solution I found was to change a line about the path in the api.py file, however I can't find the api in dist, so I am not sure if I am misunderstanding something, or if this solution does not apply to my problem.

I also found the accepted solution here, but I don't understand it.

Another attempt was to already specify a path when creating the document with

document = Document('whatever/path/you/choose/to/some.docx')

as suggested here, but this also didn't work.

I am running out of ideas, and since I am always slightly overwhelmed when it comes to specifying paths, because I always seem to do something wrong, I would very much appreciate help with detailed explanations.

If you need more info about the program itself, let me know. Additionally: I work on windows 10, with pyCharm and python3.

Thanks!


Solution

  • I found a workaround with pyinstaller. Here are the steps I followed in order to get the .exe to work. Remember I am using PyCharm.

    1.) Install pyinstaller in PyCharms terminal:

     pip install -U pyinstaller
    

    2.) Create a specfile also with PyCharms terminal and specify the path of the packages used. In my case all packages were saved in a subfolder of \venv

    pyi-makespec --paths=C:\Users\Username\PycharmProjects\ProjectName\venv\Lib\site-packages myprogram.py
    

    3.) Run the pyinstaller command. If your program has a GUI you need the -w flag! If you want one exefile containing all files you additionally need the --onefile flag. Combined this is how it looks like:

    pyinstaller --onefile -w myprogram.py
    

    4.) To this point three things should have appeared: The .spec file (which is not needed anymore and can be deleted), a build folder (which is also not needed anymore and can also be deleted), and a dist folder. The latter contains your exe. If your program consists of multiple python files (for example if you store functions in a sparate file) this guide advises to move the .exe out of the dist folder and to the .py files. However I was able to move the .exe around in any other folder without copying the .py files, so I assume this is not always needed.

    5.) Try running your .exe, it should work! If you still get package or missing module errors the packages might be located in other folders as well, in this case you need to specify multiple paths when creating your spec file(this guide helped me):

    pyi-makespec --paths=/path/to/thisdir \
             --paths=/path/to/otherdir myscript.py
    

    I hope this helps other people who are stuck on a similar problem!