1

I'm trying to make an .exe from a python script, using pyinstaller, but having issues when trying to make as onefile.

When running just below, everything works fine

pyinstaller myscript.py

When trying to make onefile, it cannot find packages.

pyinstaller --onefile myscript.py

When running the resulting .exe, I see errors such as: No module named 'mylib'

Trying to explicitly include doesn't work either.

pyinstaller --onefile --hiden-import=mylib myscript.py

You can reproduce using the example below:

My file structure is exactly this:

__init__.py
myscript.py
Compile.bat
.\mylib
--| __init__.py
--| mypackage.py

myscript.py:

from mylib.mypackage import myfunc

x = myfunc()

mypackage.py:

def myfunc():
input('Successfully Run')

Compile.bat:

cd /D "%~dp0"
pyinstaller --onefile myscript.py
set library="mylib"
set libdest="dist\mylib"
robocopy "%library%" "%libdest%" /E

All init files are empty.

After running the batch file, the dist folder structure is:

.\dist
--| myscript.exe
--| .\mylib
------| __init__.py
------| mypackage.py

Any suggestions or feedback is appreciated.

qoou
  • 155
  • 8
  • @john-hen You're right. Copying the files was acting as a work-around only for the normal method without onefile. I thought that was always required, and it's not. Your comments helped me look in the right place. – qoou Nov 15 '21 at 22:40

1 Answers1

0

I found the issue.

Leaving the __init__.py file in the root messes with pyinstaller in some way.

When using a normal build, copying the package into the folder is a work around. This made me think it was normally required, but it's not.

When using --onefile, copying the packages into the folder does not act as a work around for some reason.

Resolved folder structure:

myscript.py
Compile.bat
.\mylib
--| __init__.py
--| mypackage.py

And the batch no longer needs any copies.

pyinstaller --onefile myscript.py
qoou
  • 155
  • 8