Search code examples
pythonpython-3.xpyinstallerback-testing

Pyinstaller comes with errors when importing backtesting.py module


I am using Windows 11 for Python code development. I have a large Python program that uses the backteting.py module. The program works fine when running it using Visual Studio Code or executing it in the console. However, when I create an EXE file using the Pyinstaller, the EXE file does not work. I managed to reduce the code to two instructions (see below) and yet the EXE program does not work.

import backtesting
print ("Hello") 

I am getting the following warnings when I run the Pyinstaller.

115289 WARNING: Library user32 required via ctypes not found
115301 WARNING: Library msvcrt required via ctypes not found 

In addition, I am also getting the following errors when I run the EXE file.

C:\Users\menb\Documents\tests\test3\test3>test3
Traceback (most recent call last):
  File "test3.py", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "backtesting\__init__.py", line 60, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "backtesting\backtesting.py", line 32, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "backtesting\_plotting.py", line 43, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\menb\\Documents\\tests\\test3\\test3\\backtesting\\autoscale_cb.js'
[33336] Failed to execute script 'test3' due to unhandled exception!

Any help to solve this problem is highly appreciated.


Solution

  • I was able to successfully compile and run the two lines from your example using pyinstaller.

    import backtesting
    print ("Hello") 
    

    Steps to reproduce.

    1. create a new virtual enviornment and install pyinstaller and backtesting
    2. create main.py with your example code.
    3. run pyinstaller -F --collect-all backtesting main.py

    or

    1. pyinstaller -F --collect-all backtesting --collect-all bokeh --collect-all xyzservices main.py

    then run: dist/main.exe

    OUTPUT:

    Hello