16

I'm trying to make an executable file (.exe file for windows) for the code here. The main file to run is src/GUI.py. I found that pyinstaller is a better option to create the exe file.

I tried both one folder and single executable file options. I tried creating the exe from root directory and as well as in src directory.

pyinstaller src/GUI.py
pyinstaller src/GUI.py -F
cd src
pyinstaller GUI.py
pyinstaller GUI.py -F

GUI.exe gets created with all the above methods. But whenever I tried to run the GUI.exe file, I get the error no module named pkg_resources.py2_warn pyinstaller. I tried running GUI.exe in the dist directory where it is created, in the root directory and in the src directory as well. Everywhere, I get the same error. How can I fix this?

PS: Ideally I would like to have a single .exe file which I can distribute and they can run it standalone, without any need to install dependencies or recreating the folder structure. But I got to know that pyinstaller only packages the code files and I've to share the images separately and when running the exe file, the same structure has to be recreated. I'm okay with this as well. I'm even okay to share the one folder exe as well. I just want to share a file or a folder, which users can run without installing any dependencies. Is it possible at all?

PPS: I'm open to using tools other than pyinstaller as well.

Nagabhushan S N
  • 6,407
  • 8
  • 44
  • 87
  • 1
    The answer you selected is out of date, please see the alternatives. @Nagabhushan S N – rfii Jan 16 '21 at 04:09
  • 1
    @rfii thanks for bringing this to my attention. I think I'll let it be. Coz at that time, Vikramaditya's answer helped me. Anyway, he has also added a comment to redirect users to your answer and since your answer is top voted one, it'll be there right next to it. Now, if there is a stack overflow guideline to change the accepted answer to the one that works currently, please point me to that. I'll be happy to change the accepted answer. – Nagabhushan S N Jan 16 '21 at 08:41
  • oh ok cool im a noob and dont know how it works thanks for explaining – rfii Jan 16 '21 at 16:27

6 Answers6

24

As of 7/16/2020, upgrading setuptools now resolves this error. Downgrading setuptools like the other answers prescribe is not necessary anymore. See this discussion

On Win10, upgrade with

pip3 install setuptools --upgrade

However as Vikramaditya said, downgrading below ver 45.0.0 works too.

rfii
  • 562
  • 3
  • 14
  • 4
    I still get `25416 WARNING: Hidden import "pkg_resources.py2_warn" not found!` and `25673 WARNING: Hidden import "pkg_resources.markers" not found!` when trying to compile with `--onefile` – LukeSavefrogs Jan 26 '21 at 08:24
  • @LukeSavefrogs Did you solve the problem? I'm having the same issue – FizzleDizzle Sep 03 '21 at 21:46
10

Use PyInstaller --hidden-import=pkg_resources.py2_warn my_script.py to fix this. Same applies to most ModuleNotFoundErrors.

That hidden module was added in setuptools version 45.0.0 and removed in 49.0.0 so either downgrading below or upgrading above will also fix it.

Please do not fix this by putting import pkg_resources.py2_warn at the top of your code as others have done as this will obviously break if you change your setuptools version to be outside the above range.

This issue will be fixed permanently in version 4.0 of PyInstaller once we eventually get round to releasing it.

pullmyteeth
  • 462
  • 5
  • 12
  • 1
    I am getting the same error even after upgrading setuptools to 49.2.0 – Mohammed Raqeeb Aug 01 '20 at 11:17
  • 1
    Does adding the `--clean` option to `PyInstaller` buld command not fix it? – pullmyteeth Aug 02 '20 at 13:58
  • I just re-ran the command and now it worked, so upvoting the answer. More details of the issue and the resolutions can be found on https://github.com/pypa/setuptools/issues/1963 – Mohammed Raqeeb Aug 03 '20 at 04:19
  • upgrading actually added more warnings of similar fashion (it's not a bad thing to get more warnings) - pyinstaller 4.3, python 3.8, setuptools 57.0.0 - – Elysiumplain Jun 16 '21 at 17:18
  • PyInstaller still expects to have to explicitly add `pkg_resources.py2_warn` but, now that setuptools have removed it, it can't be found so you get a harmless warning. (In fact most of PyInstaller's warnings are harmless.) – pullmyteeth Jun 17 '21 at 21:39
9

This is an issue with setuptools as explained in this github ticket. Consider downgrading your setuptools to 44.0 or below with the command

pip install --upgrade 'setuptools<45.0.0'
1

Uninstall setuptools before you downgrade tips: Some functions may be affected

123wyf
  • 13
  • 2
0

None of these fixes worked for me, and I noticed my issue was that the error message actually begins:

File "c:\path\to\python36\lib\site-packages\PyInstaller\ loader\pyimod03_importers.py", line 627, in exec_module" when I am using python3.7 (py3.7 virtual environment, Win10). I uninstalled Py3.6 and it fixed it.

schoon
  • 2,858
  • 3
  • 46
  • 78
0

I like to run pyinstaller from a .bat file. It makes it easier to for me to see all the options laid out. I got the same error you did building something else but including hidden imports worked. here is an example of the bat file

del /S /F /Q .\dist\*
del /S /F /Q .\build\*

pyinstaller -F -y --clean^
    --hidden-import="pkg_resources.py2_warn"^
    --hidden-import=h5py ^
    code_to_package.py -n NameOFApp