I have installed lean, created a test project, and successfully ran a local backtest. However, running backtesting locally fails for some of my projects pulled from the cloud. The error is:
(lean.1) C:\_git_c\inv\lean.ws\1>lean backtest "My\DumbExp3"
[…]
20210829 08:24:34.971 TRACE:: PythonInitializer.Initialize(): ended
20210829 08:24:34.974 TRACE:: AlgorithmPythonWrapper(): Python version 3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:25:33)
[GCC 7.3.0]: Importing python module main
20210829 08:24:35.173 ERROR:: Loader.TryCreatePythonAlgorithm(): System.Exception: AlgorithmPythonWrapper(): NameError : name 'QCAlgorithm' is not defined
at <module>
class JumpingVioletDonkey(QCAlgorithm):
===
at Python.Runtime.PythonException.ThrowIfIsNull(IntPtr ob)
at Python.Runtime.PythonEngine.ImportModule(String name)
at Python.Runtime.Py.Import(String name)
at QuantConnect.AlgorithmFactory.Python.Wrappers.AlgorithmPythonWrapper..ctor(String moduleName) at /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs:line 74 in main.py:
line 4
NameError : name 'QCAlgorithm' is not defined
at QuantConnect.AlgorithmFactory.Python.Wrappers.AlgorithmPythonWrapper..ctor(String moduleName) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs:line 146
at QuantConnect.AlgorithmFactory.Loader.TryCreatePythonAlgorithm(String assemblyPath, IAlgorithm& algorithmInstance, String& errorMessage) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/AlgorithmFactory/Loader.cs:line 173
20210829 08:24:35.174 ERROR:: Engine.Run(): QuantConnect.Lean.Engine.Setup.AlgorithmSetupException: During the algorithm initialization, the following exception has occurred: Loader.TryCreatePythonAlgorithm(): Unable
to import python module /LeanCLI/main.py. AlgorithmPythonWrapper(): NameError : name 'QCAlgorithm' is not defined
at <module>
class JumpingVioletDonkey(QCAlgorithm):
===
at Python.Runtime.PythonException.ThrowIfIsNull(IntPtr ob)
at Python.Runtime.PythonEngine.ImportModule(String name)
at Python.Runtime.Py.Import(String name)
at QuantConnect.AlgorithmFactory.Python.Wrappers.AlgorithmPythonWrapper..ctor(String moduleName) at /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs:line 74 in main.py:
line 4
NameError : name 'QCAlgorithm' is not defined
at QuantConnect.Lean.Engine.Setup.BacktestingSetupHandler.CreateAlgorithmInstance(AlgorithmNodePacket algorithmNodePacket, String assemblyPath) in
/LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/Engine/Setup/BacktestingSetupHandler.cs:line 104
at QuantConnect.Lean.Engine.Engine.Run(AlgorithmNodePacket job, AlgorithmManager manager, String assemblyPath, WorkerThread workerThread) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/Engine/Engine.cs:line 116
Any idea how to workaround/fix this issue? The project backtest runs just fine in the cloud.
AdiOltean
Nevermind, fixed the issue. Apparently the main.py code needs this extra line at the beginning of the file
from AlgorithmImports import *
Would be good to add this to either fix local Lean execution or fix tutorials, etc. as running QC Python code in the cloud env does not need this extra line.
Thanks, Adi
AdiOltean
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!