0

I'm working with selenium. The script is in :

C:\Users\User\Desktop\Data Analytics Arg\Proyectos\datademia\Py_install\py_ejemplo.py . Venv is activated and chromedriver.exe is  in C:\Users\User\Desktop\Data Analytics Arg\Proyectos\datademia\Py_install\chromedriver.exe

The script runs perfectly. Then I created an only .exe-file via terminal :

pyinstaller --add-data "chromedriver.exe;." --windowed --onefile py_ejemplo.py

Folders are created correctly (build and dist). The .exe file (py_ejemplo.exe) was created, but when I try to run it, I get this message:

enter image description here I've been looking and still can't solve it... I've tried these solutions :

filenotfound

but didn't work for me...Could someone help me? I don“t know what's wrong...

Thanks in advance

Promila Ghosh
  • 389
  • 4
  • 12
Maximiliano Vazquez
  • 196
  • 1
  • 2
  • 12

2 Answers2

0

I got the same problem but I was working with Firefox and geckodriver.

In my case, I copied the selenium folder from the virtual environment to the dist folder and it worked.

0

There are a few things you should ensure when packing a script with pyinstaller build with selenium web driver.

  1. It may require to add driver executable when building. I.e. chromedriver.exe
  2. It may also require to add some package files related to selenium such as getattributes.js file when building. It was required at my project.
  3. pyinstaller will extract those files to temp folder in AppData for windows users. So in your code, your relative paths may require to be resolved with a sample function as below (if you are running your code in vs code or you are running through pyinstaller executable the paths should be resolved by function).

For item 1 and 2, you can use --add-binary and --add-data features of pyinstaller for each of them. It is also possible to do this in *.spec file with add-files list, following your first running of pyinstaller (see this explanation) I preferred command-line option as below.

pyinstaller ./app.py  --onefile --noconsole --add-binary "./driver/chromedriver.exe;./driver" --add-data "C:\Users\YOUR_USER_NAME\.conda\pkgs\selenium-3.141.0-py38h2bbff1b_1000\Lib\site-packages\selenium\webdriver\remote;selenium\webdriver\remote"

For item 3, to resolve relative paths in your source code, you can use below function in related places (for example when accessing chromedriver.exe)

 def resource_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.dirname(__file__)
    return os.path.join(base_path, relative_path)

Use above function once you need to access packaged executables and files in your source code. In below example, my chromedriver is inside driver folder in my workspace. But when it is accessed through pyinstaller executable, it will be extracted to temp folder in AppData, yet function will access it through sys._MEIPASS variable set by pyinstaller.

driver = webdriver.Chrome(executable_path = resource_path('./driver/chromedriver.exe'))

Hope it works.