Thursday, July 24, 2014

How to make py2exe include external files (such as Selenium)

If you read the full documentation of py2exe you might still not have completely clear how to perform some tasks with it.

The snippet to put on the "setup.py" file to generate the py2exe executable from the python source "run.py" is:

from distutils.core import setup
import py2exe

wd_path = 'C:\\Python27\\Lib\\site-packages\\selenium\\webdriver'
required_data_files = [('selenium/webdriver/firefox',
                        ['{}\\firefox\\webdriver.xpi'.format(wd_path), '{}\\firefox\\webdriver_prefs.json'.format(wd_path)])]

setup(
    console = ['run.py'],
    data_files = required_data_files,
    options = {
               "py2exe":{
                         "skip_archive": True,
                        }
               }
)

5 comments:

  1. Using PyQt4 / Python 3.4, and wanting a single executable (not a folder of files), I think that limits me to using py2exe instead of some of the other newbie-friendly gui-based tools (cx_freeze , pyInstaller, etc).

    Unfortunately, while my .py file runs fine when I execute it in tge IDLE GUI, it doesn't run from the console due to 'no module named xxxx' errors.

    I assume I have to get that to work first before running py2exe... but what makes it work from the IDLE gui and not via a command prompt?

    ReplyDelete
    Replies
    1. I've seen "No module named x" errors before, while using py2exe.

      Evidently, it is not finding a module being used within your code (or at least it thinks it is a module when it is just an unresolved / unassigned object).

      One case that comes to mind is when using the "exit()" function. For it to work within a py2exe generated executable, you need to "import sys; sys.exit()". This, makes py2exe resolve and include the dependencies it requires.

      So a possibility is that it is using something that exists on the IDLE scope but not in the Python interpreter scope.

      Let me know if you solve this behavior you are getting.

      Delete
  2. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    https://www.emexotechnologies.com/courses/software-testing-training/selenium-with-python-training/ Selenium with python Training in Electronic City

    ReplyDelete
  3. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    https://www.emexotechnologies.com/courses/software-testing-training/selenium-with-python-training/ Selenium with python Training in Electronic City

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete