Py2exe and selenium - IOError: [Errno 2] There is no such file or directory: '\\ dist \\ main.exe \\ selenium \\ webdriver \\ firefox \\ webdriver_prefs.json'

I wrote a simple application that uses selenium for nagivate through pages and downloads their source code. Now I would like to make my application executable Windows.

My setup.py file:

 from distutils.core import setup import py2exe, sys, os sys.argv.append('py2exe') setup( options = {'py2exe': {'bundle_files': 1, "dll_excludes": ['w9xpopen.exe', 'MSVCP90.dll', 'mswsock.dll', 'powrprof.dll', 'MPR.dll', 'MSVCR100.dll', 'mfc90.dll'], 'compressed': True,"includes":["selenium"], } }, windows = [{'script': "main.py", "icon_resources": [(1, "hacker.ico")]}], zipfile = None ) 

My program ( main.py ) (with setup.py ) is located in C:\Documents and Settings\student\Desktop . Py2exe builds my exe in C:\Documents and Settings\student\Desktop\dist .

I copied the webdriver.xpi and webdriver_prefs.json to C:\Documents and Settings\student\Desktop\dist\selenium\webdriver\firefox\ , but I get an error when I try to start the application:

 Traceback (most recent call last): File "main.py", line 73, in <module> File "main.py", line 58, in check_file File "main.py", line 25, in try_to_log_in File "selenium\webdriver\firefox\webdriver.pyo", line 47, in __init__ File "selenium\webdriver\firefox\firefox_profile.pyo", line 63, in __init__ IOError: [Errno 2] No such file or directory: 'C:\\Documents and Settings\\student\\Desktop\\dist\\main.exe\\selenium\\webdriver\\firefox\\webdriver_prefs.json' 

How to solve this?

Actually, he worked with the setup.py :

 from distutils.core import setup import py2exe, sys, os sys.argv.append('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( windows = [{'script': "main.py", "icon_resources": [(1, "hacker.ico")]}], data_files = required_data_files, options = { "py2exe":{ "skip_archive": True, } } ) 

But the problem is that I need to create a SINGLE executable.

+8
json python firefox selenium
source share
4 answers

Have you tried to look at this answer to the problems "bundle_files = 1"? This helped me solve this particular problem.

+2
source share

TL; DR - Please check out this tool I built: https://github.com/douglasmccormickjr/PyInstaller-Assistance-Tools--PAT

Perhaps I suggest using PyInstaller instead of py2exe, or something else in this case , since PyInstaller does a much better job in terms of combining a single executable. I am on Windows in about 90% of cases (no complaints here) with my python coding. PyInstaller is a better option than py2exe (for me, at least I used / tested a large number of Windows compilers in the past with varied success). Perhaps other people suffering from compilation problems can use this method.

PyInstaller Prerequisites:

  • Install PyInstaller from: http://www.pyinstaller.org/
  • After installing PyInstaller - confirm that "pyi-makepec.exe" and "pyi-build.exe" are located in the "C: \ Python ## \ Scripts" directory on your computer.
  • Download my PyInstaller-Assitance-Tools - PAT (these are just 2 batch files and 1 executable with an executable piston) - for a paranoid file ... The file is listed above:

    • Create_Single_Executable_with_NO_CONSOLE.bat
    • Create_Single_Executable_with_CONSOLE.bat
    • Pyi-fixspec.exe
    • pyi-fixpec.py (optional - this is the source file for the executable file - not required)
  • Put the exectuable file called "pyi-fixspec.exe" in the previous "Scripts" folder that I mentioned above ... it makes compilation easier in the end!

let it work now ... some small code changes in your python application

  • I am using a standard function that refers to the location of the applications / scripts that my python application should use to work at runtime / work. This function works when the application is a stand-alone python script or when it is fully compiled using pyinstaller.

    Here is the code snippet I am using ...

     def resource_path(relative_path): """ Get absolute path to resource, works for dev and for PyInstaller """ if hasattr(sys, '_MEIPASS'): return os.path.join(sys._MEIPASS, relative_path) return os.path.join(os.path.abspath("."), relative_path) 

    and here my application uses it.

     source = resource_path("data\my_archive_file.zip") 

    this means the application / files look something like this: directory structure:

     C:\Path\To\Application\myscript_001.py <--- main application/script intended to be compiled ... C:\Path\To\Application\data\my_archive_file.zip <---| C:\Path\To\Application\data\images\my_picture.jpg <---| supporting files in the bundled app C:\Path\To\Application\info\other_stuff.json <---| ... 

    Please note that the data / files / folders that I collect for my application are below the main executable / script that I will compile ... the "_MEIPASS" part in this function lets the pyinstaller developer know that this works as a compiled application ... VERY IMPORTANT NOTE: Please use the function "resource_path" , since the application "pyi-fixspec.exe" will look for this phrase / function when parsing / fixing python application path

  • Change to the directory containing the two batch files mentioned above and enter either:

    Option 1
     C:\MyComputer\Downloads\PAT> Create_Single_Executable_with_NO_CONSOLE.bat C:\Path\to\the\python\file.py 

    Double-clicked output executable leads to GUI application

    Option number 2
     C:\MyComputer\Downloads\PAT> Create_Single_Executable_with_CONSOLE.bat C:\Path\to\the\python\file.py 

    The output executable causes the WINDOWS CONSOLE application to be double-clicked - waiting for activity on the command line

  • Your new single-file executable file has been executed! Check it out at this place.

     C:\Original\Directory\ApplicationDistribution64bit\NameOfPythonFile\dist 

    If you are editing / modifying the python source file that you just compiled earlier, delete the folder / contents ** \ NameOfPythonFile ** until the next compilation starts (you will want to delete the history / artifact files)

  • Coffee break - or if you want to edit / add ICONS to the executable (and other elements too), look at the generated ".spec" file and the PyInstaller documentation for configuration details. You just need to run this again in the Windows console:

     pyi-build.exe C:\path\to\the\pythonfile.spec 
+2
source share

You can create one executable file that will be launched initially using Nuitka. It converts Python code to C ++ and then compiles it.

http://nuitka.net/

However, this requires that you have a compiler installed. Relevant versions of Microsoft Visual C ++ or GCC. Microsoft has released the "Microsoft Visual C ++ Compiler for Python 2.7," which can be obtained here at https://www.microsoft.com/en-us/download/details.aspx?id=44266 .

A good MinGW GCC installer for Windows can be found at https://github.com/develersrl/gccwinbinaries with detailed instructions, including the version of MSVCRTXX.dll to communicate with which version of Python.

Things you will get from this executable generation method:

+2
source share

The problem is that selenium is trying to open the file in a way that is incompatible with py2exe.

As you can see on line 63 here , selenium should open the settings file, which is usually sent with the package. It uses the __file__ variable to find it, which does not work very well with py2exe . As you have found, you can get around this by packing the prefs file in your distribution . However, now it is more than one file.

The py2exe wiki file is then a recipe for using NSIS , which will create a self-extracting executable file of your full distribution.

+2
source share

All Articles