I'm using py2exe to create a python executable (following this link). When run from cmd, the .exe file shows an error saying "ImportError: No module named mechanize", although I have installed mechanize and placed it in the site-packages folder. How do I fix this? The aim is create a package that can be run on other Windows computers without needing to install Python.
-
1Did you try PyInstaller ? http://www.pyinstaller.org/ – Depado Mar 03 '14 at 10:48
-
PyInstaller works! Is it generally preferred to py2exe? – raul Mar 04 '14 at 05:48
-
I added an answer to your post so you can accept it, and added a link for differences between Py2Exe vs PyInstaller. – Depado Mar 04 '14 at 07:46
2 Answers
I recently ran into this issue and thought I'd leave my solution here in order to help others wanting to use py2exe.
In the example setup.py file created by following the tutorial found on py2exe's website
from distutils.core import setup
import py2exe
setup(console=['main.py'])
change the signature of the setup() method to:
from distutils.core import setup
import py2exe
setup(
console=['main.py'],
options = {
'py2exe': {
'packages': ['packageName']
}
}
)
Now update packageName to the name of the module you are importing in your main.py file. Then run python setup.py py2exe
and it should build with the external module included with it. I tested this on my machine using plyer and had installed my module using pip.
See this link to the original article where I found this py2exe-python-to-exe-introduction beware though there is a lot of ads!

- 11
- 2
As said in the comments, you can use PyInstaller
To get a list of differences between Py2Exe and PyInstaller there is a question on SO that could help you here. Though I consider that turning into an executable is kind of random (I mean sometimes it will work, sometimes it won't and most of the time you can't understand why...)
Please consider accepting my answer as, I guess, it solved your problem :)