Pyinstaller在导入backtesting.py模块时出现错误。

huangapple go评论54阅读模式
英文:

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 backtesting.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 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 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 an unhandled exception!

Any help to solve this problem is highly appreciated.

英文:

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 (&quot;Hello&quot;) 

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

Any help to solve this problem is highly appreciated.

答案1

得分: 1

我能够成功编译和运行你的示例中的两行代码,使用 pyinstaller。

import backtesting
print("Hello")

重现步骤:

  1. 创建一个新的虚拟环境并安装 pyinstaller 和 backtesting。
  2. 创建一个名为 main.py 的文件,其中包含你的示例代码。
  3. 运行以下命令:
    pyinstaller -F --collect-all backtesting main.py
    

或者

  1. 运行以下命令:
    pyinstaller -F --collect-all backtesting --collect-all bokeh --collect-all xyzservices main.py
    

然后运行:dist/main.exe

输出:

Hello
英文:

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

import backtesting
print (&quot;Hello&quot;) 

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

huangapple
  • 本文由 发表于 2023年3月1日 12:43:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定