EXE created with PyInstaller: "Cannot copy tree 'Y': not a directory"

128 Views Asked by At

I converted a file, main.py, into main.exe, using PyInstaller. main.py doesn't throw any error, but main.exe does.

The script didn't execute fully, so I used a try-catch and got this:

Cannot copy tree 'Y': not a directory

It looks like it's a problem with how I converted it into an EXE with PyInstaller.

This is the PyInstaller command I used to convert it into an EXE:

pyinstaller --onefile --windowed --icon "X/logo.ico"
--name "H" --add-data "X/Y;Y/" --add-data 
"X/dmode.png;." --add-data "X/lu.png;." --add-data "X/toggle.png;." 
--add-data "X/white.png;." --paths "C:/Users/Username/venv/Lib/site-packages" 
--hidden-import "tkinter" --hidden-import "os" --hidden-import "sys" 
--hidden-import "distutils" --hidden-import "shutil" 
--hidden-import "win32gui"  "X/main.py"

And this is the file structure:

X

  • build, dist and other folders created by PyInstaller and PyCharm (IDE)

  • Y

    • JS
      • cS.js
    • i.jpeg
    • M.json
  • Z (the directory in which Y will be copied)

  • dmode.png

  • logo.ico

  • lu.png

  • main.py

  • toggle.png

  • white.png

X is the directory in which main.py is. This is apparently the code which causes the error:

from os.path import join
from distutils.dir_util import copy_tree

if getattr(sys, 'frozen', False):
    application_path = os.path.dirname(sys.executable)
elif __file__:
    application_path = os.path.dirname(__file__)

if not os.path.exists(join(application_path, "Z")):
    copy_tree("Y", join(application_path, "Z"))

But I don't get any errors when I run the PyInstaller command in CMD. So, where have I gone wrong? Have I used --add-data incorrectly? Is application_path wrong (copied from this)? If yes, please mention where I've used it incorrectly and where I have referenced the files/directories incorrectly. I prefer changing the command instead of the .spec file, so a PyInstaller (CMD) command would be helpful.

0

There are 0 best solutions below