Wednesday, July 2, 2014

Py2Exe Troubleshooting

So I had this Python script that I wanted to bundle up in a binary to distribute to Windows systems. It worked fine when run with the Python interpreter, but was throwing a strange error after being compiled by Py2Exe:

Traceback (most recent call last):
  File "download_random_files.py", line 2, in <module>
  File "requests\__init__.pyc", line 58, in <module>
  File "requests\utils.pyc", line 25, in <module>
  File "requests\compat.pyc", line 7, in <module>
ImportError: cannot import name chardet

Which I thought was interesting, cause I had no clue what chardet was.

If you're an astute observer, you'll read the rest of the error message.. :P 


Ok, so it's related to the requests package, so what? That was pretty much where the trail ended for me, all the troubleshooting I found online was only *loosely* related to my error.


Basically, the issue is in the py2exe setup.py file.

My initial setup.py file was basically just...

from distutils.core ipmort setup
import py2exe
console=['controller.py']


Which isn't taking advantage of any of the features of py2exe. After some digging, I found a quick and dirty solution. For some reason, py2exe was having problems locating the requests package, which was necessary for the script to run. I found that by explicitly specifying the requests package in my setup file, the issue corrected itself.

from distutils.core import setup
import py2exe

setup(
  console=['controller.py'],
  options = {'py2exe': {'packages': ['requests']}})


With that said, there are some really cool options/features you can add when you convert your python to a binary. Check them all out at: http://www.py2exe.org/index.cgi/ListOfOptions


No comments:

Post a Comment