首页 > 解决方案 > 如何运行从 Deno 导入的 Python 脚本?

问题描述

我目前正在尝试使用以下代码从 deno 后端运行 python 文件...

const cmd = Deno.run({
    cmd: ["python", "python.py"], 
    stdout: "piped",
    stderr: "piped"
});

const output = await cmd.output() // "piped" must be set
const outStr = new TextDecoder().decode(output);

const error = await cmd.stderrOutput();
const errorStr = new TextDecoder().decode(error);

cmd.close(); 
console.log(outStr, errorStr);

const resultsAlgorithm = outStr
console.log('This is a test, python result is...',outStr)
console.log('Finished')

该代码适用于'print(“Hello”)'等基本脚本,但无法在更复杂的脚本上运行导入,例如......

   import pandas as pd  # Changes call up name to pd
   from yahoofinancials import YahooFinancials
   from datetime import date, datetime, timedelta,time

   Yahoo_Forex = pd.DataFrame()
   Currency_Pair_Prices = pd.DataFrame()

   print('Running')


    def DataExtract(FileName, DataFrameName, DataFrameName_2, time_range, Interval, ColumnName):
        print('Function started')
        start = date.today() - timedelta(days=2)  
        end = date.today() - timedelta(days=time_range)  
        DataFrameName = pd.read_excel(FileName, header=None)  
        DataFrameName.columns = ColumnName  
        n = 0  
        for ticker in DataFrameName[ColumnName[0]]:  
            Currency_Pair = DataFrameName.iloc[n, 1]  
            Currency_Pair_Ticker = YahooFinancials(ticker)  
            data = Currency_Pair_Ticker.get_historical_price_data(
                str(end), str(start), Interval) 
            Extracted_Data = pd.DataFrame(data[ticker]["prices"])  
            Currency_Close_Price = (Extracted_Data["close"])  
            DataFrameName_2[str(Currency_Pair)] = Currency_Close_Price  
            n = n+1  # 13
        print(DataFrameName_2)
        print("DataExtract Completed")
    
    
    DataExtract("yahoo_Forex.xlsx", Yahoo_Forex, Currency_Pair_Prices,int(10), "daily", ["Ticker", "Pair", "Exchange"])

python代码自己成功运行,所以必须是deno的东西,但确定我需要改变什么,所以任何帮助都将不胜感激!

标签: javascriptpythondeno

解决方案


推荐阅读