Issues in Running Python Algorithms on Lean Locally on VS 2019 , i have anaconda 4.8.3 and python 3.7
i have already modified my config.json for python as per instructions.
{
// Algorithm language selector - options CSharp, Python
"algorithm-language": "Python",
//Physical DLL location
"algorithm-location": "C:\\Users\\Ishant\\source\\repos\\Lean\\Algorithm.Python\\BasicTemplateAlgorithm.py",
// algorithm class selector
"algorithm-type-name": "BasicTemplateAlgorithm",
"alpha-handler": "QuantConnect.Lean.Engine.Alphas.DefaultAlphaHandler",
"api-access-token": "",
"api-handler": "QuantConnect.Api.Api",
// arteyu configuration
"atreyu-host": "",
"atreyu-password": "",
"atreyu-req-port": "",
"atreyu-sub-port": "",
"atreyu-username": "",
"binance-api-key": "",
// binance configuration
"binance-api-secret": "",
"bitfinex-api-key": "",
// bitfinex configuration
"bitfinex-api-secret": "",
// Required for market data from Coin API
"coinapi-api-key": "",
"coinapi-product": "free", // free, startup, streamer, professional, enterprise
"data-aggregator": "QuantConnect.Lean.Engine.DataFeeds.AggregationManager",
"data-channel-provider": "DataChannelProvider",
//"algorithm-location": "../../../Algorithm.Python/BasicTemplateFrameworkAlgorithm.py",
//Research notebook
//"composer-dll-directory": ".",
// engine
"data-folder": "../../../Data/",
"data-provider": "QuantConnect.Lean.Engine.DataFeeds.DefaultDataProvider",
// debugging configuration - options for debugging-method LocalCmdLine, VisualStudio, PTVSD, PyCharm
"debugging": false,
"debugging-method": "LocalCmdline",
// this configuration file works by first loading all top-level
// configuration items and then will load the specified environment
// on top, this provides a layering affect. environment names can be
// anything, and just require definition in this file. There's
// two predefined environments, 'backtesting' and 'live', feel free
// to add more!
"environment": "backtesting", // "live-paper", "backtesting", "live-interactive", "live-interactive-iqfeed"
"environments": {
// defines the 'backtesting' environment
"backtesting": {
"live-mode": false,
"setup-handler": "QuantConnect.Lean.Engine.Setup.ConsoleSetupHandler",
"result-handler": "QuantConnect.Lean.Engine.Results.BacktestingResultHandler",
"data-feed-handler": "QuantConnect.Lean.Engine.DataFeeds.FileSystemDataFeed",
"real-time-handler": "QuantConnect.Lean.Engine.RealTime.BacktestingRealTimeHandler",
"history-provider": "QuantConnect.Lean.Engine.HistoricalData.SubscriptionDataReaderHistoryProvider",
"transaction-handler": "QuantConnect.Lean.Engine.TransactionHandlers.BacktestingTransactionHandler"
},
// defines the 'live-paper' environment
"live-paper": {
"live-mode": true,
// the paper brokerage requires the BacktestingTransactionHandler
"live-mode-brokerage": "PaperBrokerage",
"setup-handler": "QuantConnect.Lean.Engine.Setup.BrokerageSetupHandler",
"result-handler": "QuantConnect.Lean.Engine.Results.LiveTradingResultHandler",
"data-feed-handler": "QuantConnect.Lean.Engine.DataFeeds.LiveTradingDataFeed",
"data-queue-handler": "QuantConnect.Lean.Engine.DataFeeds.Queues.LiveDataQueue",
"real-time-handler": "QuantConnect.Lean.Engine.RealTime.LiveTradingRealTimeHandler",
"transaction-handler": "QuantConnect.Lean.Engine.TransactionHandlers.BacktestingTransactionHandler"
},
// defines the 'live-tradier' environment
"live-tradier": {
"live-mode": true,
// this setting will save tradier access/refresh tokens to a tradier-tokens.txt file
// that can be read in next time, this makes it easier to start/stop a tradier algorithm
"tradier-save-tokens": true,
// real brokerage implementations require the BrokerageTransactionHandler
"live-mode-brokerage": "TradierBrokerage",
"data-queue-handler": "TradierBrokerage",
"setup-handler": "QuantConnect.Lean.Engine.Setup.BrokerageSetupHandler",
"result-handler": "QuantConnect.Lean.Engine.Results.LiveTradingResultHandler",
"data-feed-handler": "QuantConnect.Lean.Engine.DataFeeds.LiveTradingDataFeed",
"real-time-handler": "QuantConnect.Lean.Engine.RealTime.LiveTradingRealTimeHandler",
"transaction-handler": "QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler"
},
// defines the 'live-interactive' environment
"live-interactive": {
"live-mode": true,
// real brokerage implementations require the BrokerageTransactionHandler
"live-mode-brokerage": "InteractiveBrokersBrokerage",
"setup-handler": "QuantConnect.Lean.Engine.Setup.BrokerageSetupHandler",
"result-handler": "QuantConnect.Lean.Engine.Results.LiveTradingResultHandler",
"data-feed-handler": "QuantConnect.Lean.Engine.DataFeeds.LiveTradingDataFeed",
"data-queue-handler": "QuantConnect.Brokerages.InteractiveBrokers.InteractiveBrokersBrokerage",
"real-time-handler": "QuantConnect.Lean.Engine.RealTime.LiveTradingRealTimeHandler",
"transaction-handler": "QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler",
"history-provider": "BrokerageHistoryProvider"
},
// defines the 'live-interactive-iqfeed' environment
"live-interactive-iqfeed": {
"live-mode": true,
// real brokerage implementations require the BrokerageTransactionHandler
"live-mode-brokerage": "InteractiveBrokersBrokerage",
"setup-handler": "QuantConnect.Lean.Engine.Setup.BrokerageSetupHandler",
"result-handler": "QuantConnect.Lean.Engine.Results.LiveTradingResultHandler",
"data-feed-handler": "QuantConnect.Lean.Engine.DataFeeds.LiveTradingDataFeed",
"data-queue-handler": "QuantConnect.ToolBox.IQFeed.IQFeedDataQueueHandler",
"real-time-handler": "QuantConnect.Lean.Engine.RealTime.LiveTradingRealTimeHandler",
"transaction-handler": "QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler",
"history-provider": "QuantConnect.ToolBox.IQFeed.IQFeedDataQueueHandler"
},
// defines the 'live-fxcm' environment
"live-fxcm": {
"live-mode": true,
// real brokerage implementations require the BrokerageTransactionHandler
"live-mode-brokerage": "FxcmBrokerage",
"data-queue-handler": "FxcmBrokerage",
"setup-handler": "QuantConnect.Lean.Engine.Setup.BrokerageSetupHandler",
"result-handler": "QuantConnect.Lean.Engine.Results.LiveTradingResultHandler",
"data-feed-handler": "QuantConnect.Lean.Engine.DataFeeds.LiveTradingDataFeed",
"real-time-handler": "QuantConnect.Lean.Engine.RealTime.LiveTradingRealTimeHandler",
"transaction-handler": "QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler",
"history-provider": "BrokerageHistoryProvider"
},
// defines the 'live-oanda' environment
"live-oanda": {
"live-mode": true,
// real brokerage implementations require the BrokerageTransactionHandler
"live-mode-brokerage": "OandaBrokerage",
"data-queue-handler": "OandaBrokerage",
"setup-handler": "QuantConnect.Lean.Engine.Setup.BrokerageSetupHandler",
"result-handler": "QuantConnect.Lean.Engine.Results.LiveTradingResultHandler",
"data-feed-handler": "QuantConnect.Lean.Engine.DataFeeds.LiveTradingDataFeed",
"real-time-handler": "QuantConnect.Lean.Engine.RealTime.LiveTradingRealTimeHandler",
"transaction-handler": "QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler",
"history-provider": "BrokerageHistoryProvider"
},
"live-gdax": {
"live-mode": true,
// real brokerage implementations require the BrokerageTransactionHandler
"live-mode-brokerage": "GDAXBrokerage",
"data-queue-handler": "GDAXDataQueueHandler",
"setup-handler": "QuantConnect.Lean.Engine.Setup.BrokerageSetupHandler",
"result-handler": "QuantConnect.Lean.Engine.Results.LiveTradingResultHandler",
"data-feed-handler": "QuantConnect.Lean.Engine.DataFeeds.LiveTradingDataFeed",
"real-time-handler": "QuantConnect.Lean.Engine.RealTime.LiveTradingRealTimeHandler",
"transaction-handler": "QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler",
"history-provider": "BrokerageHistoryProvider"
},
"live-bitfinex": {
"live-mode": true,
// real brokerage implementations require the BrokerageTransactionHandler
"live-mode-brokerage": "BitfinexBrokerage",
"data-queue-handler": "BitfinexBrokerage",
"setup-handler": "QuantConnect.Lean.Engine.Setup.BrokerageSetupHandler",
"result-handler": "QuantConnect.Lean.Engine.Results.LiveTradingResultHandler",
"data-feed-handler": "QuantConnect.Lean.Engine.DataFeeds.LiveTradingDataFeed",
"real-time-handler": "QuantConnect.Lean.Engine.RealTime.LiveTradingRealTimeHandler",
"transaction-handler": "QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler",
"history-provider": "BrokerageHistoryProvider"
},
"live-binance": {
"live-mode": true,
// real brokerage implementations require the BrokerageTransactionHandler
"live-mode-brokerage": "BinanceBrokerage",
"data-queue-handler": "BinanceBrokerage",
"setup-handler": "QuantConnect.Lean.Engine.Setup.BrokerageSetupHandler",
"result-handler": "QuantConnect.Lean.Engine.Results.LiveTradingResultHandler",
"data-feed-handler": "QuantConnect.Lean.Engine.DataFeeds.LiveTradingDataFeed",
"real-time-handler": "QuantConnect.Lean.Engine.RealTime.LiveTradingRealTimeHandler",
"transaction-handler": "QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler",
"history-provider": "BrokerageHistoryProvider"
},
// defines the 'live-atreyu' environment
"live-atreyu": {
"live-mode": true,
// real brokerage implementations require the BrokerageTransactionHandler
"live-mode-brokerage": "QuantConnect.Atreyu.AtreyuBrokerage",
"data-queue-handler": "QuantConnect.Lean.Engine.DataFeeds.Queues.LiveDataQueue",
"setup-handler": "QuantConnect.Lean.Engine.Setup.BrokerageSetupHandler",
"result-handler": "QuantConnect.Lean.Engine.Results.LiveTradingResultHandler",
"data-feed-handler": "QuantConnect.Lean.Engine.DataFeeds.LiveTradingDataFeed",
"real-time-handler": "QuantConnect.Lean.Engine.RealTime.LiveTradingRealTimeHandler",
"transaction-handler": "QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler"
},
// defines the 'live-trading-technologies' environment
"live-trading-technologies": {
"live-mode": true,
// real brokerage implementations require the BrokerageTransactionHandler
"live-mode-brokerage": "TradingTechnologiesBrokerage",
"data-queue-handler": "TradingTechnologiesBrokerage",
"setup-handler": "QuantConnect.Lean.Engine.Setup.BrokerageSetupHandler",
"result-handler": "QuantConnect.Lean.Engine.Results.LiveTradingResultHandler",
"data-feed-handler": "QuantConnect.Lean.Engine.DataFeeds.LiveTradingDataFeed",
"real-time-handler": "QuantConnect.Lean.Engine.RealTime.LiveTradingRealTimeHandler",
"transaction-handler": "QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler"
}
},
"factor-file-provider": "QuantConnect.Data.Auxiliary.LocalDiskFactorFileProvider",
// if one uses true in following token, market hours will remain open all hours and all days.
// if one uses false will make lean operate only during regular market hours.
"force-exchange-always-open": false,
"fxcm-account-id": "",
"fxcm-password": "",
// fxcm configuration
"fxcm-server": "http://www.fxcorporate.com/Hosts.jsp",
"fxcm-terminal": "Demo", //Real or Demo
"fxcm-user-name": "",
"gdax-api-key": "",
// gdax configuration
"gdax-api-secret": "",
"gdax-passphrase": "",
// interactive brokers configuration
"ib-account": "",
"ib-agent-description": "Individual",
"ib-enable-delayed-streaming-data": false,
"ib-host": "127.0.0.1",
"ib-password": "",
"ib-port": "4002",
"ib-trading-mode": "paper",
"ib-tws-dir": "C:\\Jts",
"ib-user-name": "",
"ib-version": "974",
// Required for IEX history requests
"iex-cloud-api-key": "",
// iqfeed configuration
"iqfeed-host": "127.0.0.1",
"iqfeed-password": "",
"iqfeed-productName": "",
"iqfeed-username": "",
"iqfeed-version": "1.0",
"job-queue-handler": "QuantConnect.Queues.JobQueue",
// To get your api access token go to quantconnect.com/account
"job-user-id": "0",
"live-data-port": 8020,
// live data configuration
"live-data-url": "ws://www.quantconnect.com/api/v2/live/data/",
// handlers
"log-handler": "QuantConnect.Logging.CompositeLogHandler",
"map-file-provider": "QuantConnect.Data.Auxiliary.LocalDiskMapFileProvider",
// limits the amount of data points per chart series. Applies only for backtesting
"maximum-data-points-per-chart-series": 4000,
"messaging-handler": "QuantConnect.Messaging.Messaging",
"oanda-access-token": "",
"oanda-account-id": "",
// oanda configuration
"oanda-environment": "Practice",
"object-store": "QuantConnect.Lean.Engine.Storage.LocalObjectStore",
// parameters to set in the algorithm (the below are just samples)
"parameters": {
// Intrinio account user and password
"intrinio-username": "",
"intrinio-password": "",
"ema-fast": 10,
"ema-slow": 20
},
// Required for streaming Polygon.io data
// To get your access token go to https://polygon.io
"polygon-api-key": "",
// Required to access data from Quandl
// To get your access token go to https://www.quandl.com/account/api
"quandl-auth-token": "",
// Required to access data from Quiver Quantitative
// To get your access token go to https://api.quiverquant.com
"quiver-auth-token": "",
// limits on number of symbols to allow
"symbol-minute-limit": 10000,
"symbol-second-limit": 10000,
"symbol-tick-limit": 10000,
// Required to access data from Tiingo
// To get your access token go to https://www.tiingo.com
"tiingo-auth-token": "",
"tradier-access-token": "",
"tradier-account-id": "",
// tradier configuration
"tradier-use-sandbox": true,
// save list of transactions to the specified csv file
"transaction-log": "",
"tt-account-name": "",
"tt-log-fix-messages": false,
"tt-market-data-host": "",
"tt-market-data-port": "",
"tt-market-data-sender-comp-id": "",
"tt-market-data-target-comp-id": "",
"tt-order-routing-host": "",
"tt-order-routing-port": "",
"tt-order-routing-sender-comp-id": "",
"tt-order-routing-target-comp-id": "",
"tt-rest-app-key": "",
"tt-rest-app-secret": "",
"tt-rest-environment": "",
"tt-session-password": "",
// Trading Technologies configuration
"tt-user-name": "",
// Required to access data from US Energy Information Administration
// To get your access token go to https://www.eia.gov/opendata
"us-energy-information-auth-token": ""
}
Folowing error pops,
Please Help !!
20210222 14:30:37.390 ERROR:: Loader.TryCreatePythonAlgorithm(): System.DllNotFoundException: Unable to load DLL 'python36': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
at QuantConnect.Python.PythonInitializer.Initialize() in C:\Users\Ishant\Source\Repos\Lean\Common\Python\PythonInitializer.cs:line 42
at QuantConnect.AlgorithmFactory.Loader.TryCreatePythonAlgorithm(String assemblyPath, IAlgorithm& algorithmInstance, String& errorMessage) in C:\Users\Ishant\Source\Repos\Lean\AlgorithmFactory\Loader.cs:line 171
20210222 14:30:37.424 ERROR:: Engine.Run(): QuantConnect.Lean.Engine.Setup.AlgorithmSetupException: During the algorithm initialization, the following exception has occurred: Loader.TryCreatePythonAlgorithm(): Unable to import python module C:\Users\Ishant\source\repos\Lean\Algorithm.Python\BasicTemplateAlgorithm.py. Unable to load DLL 'python36': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
at QuantConnect.Lean.Engine.Setup.ConsoleSetupHandler.CreateAlgorithmInstance(AlgorithmNodePacket algorithmNodePacket, String assemblyPath) in C:\Users\Ishant\Source\Repos\Lean\Engine\Setup\ConsoleSetupHandler.cs:line 105
at QuantConnect.Lean.Engine.Engine.Run(AlgorithmNodePacket job, AlgorithmManager manager, String assemblyPath, WorkerThread workerThread) in C:\Users\Ishant\Source\Repos\Lean\Engine\Engine.cs:line 119
20210222 14:30:37.433 TRACE:: JOB HANDLERS:
20210222 14:30:37.433 TRACE:: DataFeed: QuantConnect.Lean.Engine.DataFeeds.FileSystemDataFeed
20210222 14:30:37.434 TRACE:: Setup: QuantConnect.Lean.Engine.Setup.ConsoleSetupHandler
20210222 14:30:37.435 TRACE:: RealTime: QuantConnect.Lean.Engine.RealTime.BacktestingRealTimeHandler
20210222 14:30:37.436 TRACE:: Results: QuantConnect.Lean.Engine.Results.BacktestingResultHandler
20210222 14:30:37.437 TRACE:: Transactions: QuantConnect.Lean.Engine.TransactionHandlers.BacktestingTransactionHandler
20210222 14:30:37.440 TRACE:: Alpha: QuantConnect.Lean.Engine.Alphas.DefaultAlphaHandler
20210222 14:30:37.441 TRACE:: ObjectStore: QuantConnect.Lean.Engine.Storage.LocalObjectStore
20210222 14:30:37.442 TRACE:: DefaultAlphaHandler.Exit(): Exiting...
20210222 14:30:37.444 TRACE:: DefaultAlphaHandler.Exit(): Ended
20210222 14:30:37.445 TRACE:: BacktestingResultHandler.Exit(): starting...
20210222 14:30:37.447 TRACE:: BacktestingResultHandler.Exit(): Saving logs...
20210222 14:30:37.452 TRACE:: StopSafely(): waiting for 'Result Thread' thread to stop...
20210222 14:30:37.453 ERROR:: Algorithm.Initialize() Error: During the algorithm initialization, the following exception has occurred: Loader.TryCreatePythonAlgorithm(): Unable to import python module C:\Users\Ishant\source\repos\Lean\Algorithm.Python\BasicTemplateAlgorithm.py. Unable to load DLL 'python36': The specified module could not be found. (Exception from HRESULT: 0x8007007E) Stack Trace: During the algorithm initialization, the following exception has occurred: Loader.TryCreatePythonAlgorithm(): Unable to import python module C:\Users\Ishant\source\repos\Lean\Algorithm.Python\BasicTemplateAlgorithm.py. Unable to load DLL 'python36': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
During the algorithm initialization, the following exception has occurred: Loader.TryCreatePythonAlgorithm(): Unable to import python module C:\Users\Ishant\source\repos\Lean\Algorithm.Python\BasicTemplateAlgorithm.py. Unable to load DLL 'python36': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Derek Melchin
Hi Ishant,
We recommend following these installation instructions. Python 3.6 is required.
Best,
Derek Melchin
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.
Ulugbek Zairov
It did not worked out for me as well.
Derek Melchin
Hi Ulugbek,
Please share steps we can follow to reproduce the issue.
Best,
Derek Melchin
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.
Fred Painchaud
Hi All,
This is an aging thread but I believe my reply can help many out there.
I personally recommend adding environment variables to point to your Python installation/venv of choice for your local LEAN project/solution. From as far as I can remember, it's been the way to go in doing Python in Visual Studio / VS Code. I'm on Windows 10 so it might be different on Linux and Mac - but setting environment variables there too might help.
Now, you do not want to set those environment variables globally in your system if you don't want to disrupt other Python projects.
So, in VS Code, open the .vscode/launch.json file and add the following lines in the “Launch” configuration, under the last line of that configuration (in my case at the time of this writing: "internalConsoleOptions": "neverOpen" → add a ‘,’ (comma) after that and add the following):
"env": {
"PYTHONHOME": "Your path to Python installation",
"PYTHONPATH": "Your path to Python installation",
"PYTHONNET_PYDLL": "Your path to the Python DLL itself (such as python36.dll, python37.dll, etc)"
}
In the case of the OP, I believe the error comes from Python.NET not finding the Python DLL (last of the three environment variables - but you can still need all three).
In Visual Studio (the complete IDE), it has been a while since I used it but I believe you can set environment variables in the solution itself, in the settings.
Hope this helps someone,
Cheers,
Fred
Jared Broad
We recommend people now use the LEAN CLI – it bypasses the need to set up a local environment using Docker. You can see how to install this here:
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.
Fred Painchaud
Hi Jared,
I have an immense respect for what you have accomplished with LEAN and QuantConnect btw. Kudos!
This is not the right place, I know, but I still take this opportunity to say that I do use both LEAN CLI and the standard full development VS environment (in fact I have integrated both into one environment right now) for one reason: I find that full source code access and navigation while coding my algorithms is giving me invaluable documentation. Much more than stubs only, as one gets from working with LEAN CLI only. While the documentation available at https://www.quantconnect.com/docs is nice, I am not happy/satisfied when I learn only by examples and overall concepts. I need the whole thing, all the details. At least in my current learning stage…
Thought I would share my view on the matter. Food for thought I guess.
Cheers!
Ishant Jain
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!