raul
raul

Reputation: 1269

Importing external modules to python executable

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.

Upvotes: 0

Views: 2458

Answers (2)

user7385666
user7385666

Reputation: 11

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!

Upvotes: 1

Depado
Depado

Reputation: 4929

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 :)

Upvotes: 0

Related Questions