I'm trying to package a script using pyinstaller to run as a windows service. When trying to run it I get the ModuleNotFoundError: No module named 'urllib3.packages.six.moves'
error. I'm using the command
pyinstaller.exe --onefile --runtime-tmpdir=. --hidden-import win32timezone --hidden-import urllib3 laps_service.py
in a virtual environment. You'll see there that I added it as a --hidden-import
but that didn't make any difference.
After some googling I found that uninstalling and reinstalling urllib3 and requests could help, so I've done that - I explicitly installed requests==2.24.0
because servicemanager
required 2.24.0; when this installed, pip automatically uninstalled urllib3-2.1.0
and installed the compatible version urllib3-1.25.11
, but I'm still getting the same error. (Edit: I also installed six)
From memory this script worked absolutely fine as a pyinstaller exe in a venv previously (on a laptop I no longer have), so I don't think there's any problem with the code and instead probably something with the versions of some package. I've run pip check
but it found no broken dependencies.
Full Traceback is below and I can give a list of imported packages if that's helpful.
(Edit: I also have a working (but older and slightly different version) of the compiled .exe so if there's a way of getting into that to see the versions, that might help)
PS C:\LAPS> .\laps_service.exe --startup delayed install
Traceback (most recent call last):
File "laps_service.py", line 29, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "requests\__init__.py", line 43, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "urllib3\__init__.py", line 7, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "urllib3\connectionpool.py", line 11, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "urllib3\exceptions.py", line 2, in <module>
ModuleNotFoundError: No module named 'urllib3.packages.six.moves'
[14276] Failed to execute script 'laps_service' due to unhandled exception!
In the end this was some kind of dependency conflict - I uninstalled urllib3
and requests
, and then reinstalled requests
- this triggered a reinstall of some urllib3
stuff. I don't quite know why, so here's the traceback for posterity:
pip install requests
Collecting requests
Using cached requests-2.31.0-py3-none-any.whl.metadata (4.6 kB)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\nosync\projects\laps\.venv\lib\site-packages (from requests) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in c:\nosync\projects\laps\.venv\lib\site-packages (from requests) (2.10)
Collecting urllib3<3,>=1.21.1 (from requests)
Using cached urllib3-2.1.0-py3-none-any.whl.metadata (6.4 kB)
Requirement already satisfied: certifi>=2017.4.17 in c:\nosync\projects\laps\.venv\lib\site-packages (from requests) (2023.11.17)
Using cached requests-2.31.0-py3-none-any.whl (62 kB)
Using cached urllib3-2.1.0-py3-none-any.whl (104 kB)
Installing collected packages: urllib3, requests
Successfully installed requests-2.31.0 urllib3-2.1.0
I then tested it before freezing with pyinstaller, and it couldn't find a module of servicemanager
- turns out I had also installed a stand-alone servicemanager
when I didn't need to because it's included with pywin32
, and there was a conflict there. So I uninstalled servicemanager
.
At some point in all this it fixed itself, so my advice would be to either start fresh or systematically uninstall everything and reinstall one by one, running the script for errors. Not a completely satisfying answer, but an answer nonetheless.