gpt4 book ai didi

Python - failed to import external python program under different folder(Python-无法在不同文件夹下导入外部Python程序)

转载 作者:bug小助手 更新时间:2023-10-22 13:50:27 28 4
gpt4 key购买 nike



I have a web-based Python application using Flask and a Python application, tradingBot.
I copied my tradingBot application to a Web-based application under a different directory so I can call the tradingBot application. the directory structure like below:

我有一个基于web的Python应用程序,使用Flask和一个Python应用程序tradingBot。我将tradingBot应用程序复制到另一个目录下的一个基于Web的应用程序中,这样我就可以调用tradingBots应用程序。目录结构如下:



  • myTradingBot

    • apps

      • home

        • routes.py



      • static

        • css

        • js



      • template

        • home

          • dashboard.html

          • tradingDemo.html





      • autoTrade <== want to import this package to routes.py

        • bot.py

        • tradelib.py








I have tried the following in the route.py file with no success

我在route.py文件中尝试了以下操作,但没有成功


try:
from apps.home import blueprint
import pyasx.data.companies

import pandas as pd
import plotly
import os
import sys
sys.path.append(os.path.abspath("/myTradingBot/apps/autoTrade/"))
import bot

print("ALl modules Loaded ")
except Exception as e:
print("Error : {} ".format(e))

@blueprint.route("/stock-movement", methods=['GET', 'POST'])
def stockMovement():ry:

companyInfo = pyasx.data.companies.get_company_info(company_share_code)
bot.tradeBotStart()

It return an error stating that:
bot is undefined.

它返回一个错误,说明:bot未定义。


Note: in my bot.py, i have alredy commented out the protected main

注意:在我的bot.py中,我已经评论掉了受保护的main


def tradeBotStart

print("start - TradeBotStart")
api = tradeapi.REST(gvars.API_KEY, gvars.API_SECRET_KEY, api_version='v2')


#if __name__ == '__main__':
# main()
# #main(sys.argv[1:])

Please advise

请告知


更多回答

Please include the full traceback error.

请包括完整的回溯错误。

i even print the current location using print(os.getcwd()) then add the physical path import os import sys sys.path.append(os.path.abspath("/Users/p5653/PythonApp/web/myTradingBot/apps/autoTrade/")) import bot still return bot undefined error

我甚至使用print(os.getcwd())打印当前位置,然后添加物理路径import os import sys.path.append(os.path.abspath(“/Users/p5653/PythonApp/web/myTradingBot/apps/autoTrade/”)import bot仍然返回bot未定义的错误

Please include that in your original post and not in the comments.

请将其包含在您的原始帖子中,而不是评论中。

thank you. ewokx. as I look at the trace to resolve the issue. but I have another question but will raise a separate question

非常感谢。ewokx。当我查看跟踪以解决问题时。但我还有一个问题,但会提出另一个问题

优秀答案推荐

It would have been helpful if you included the error message. Here are some general approaches that will help out.

如果您包含错误消息,将会很有帮助。以下是一些有帮助的一般方法。



  • Does this line def stockMovement():ry: mean def stockMovement():?. This is one of the points of error.

    这行def stockMovement():ry:是指def stockMove():?。这是其中一个错误点。



  • Check for errors due to other modules by setting some breakpoints. It's not always the Flask unless the error is explicitly thrown by Flask (This applies to both, the route.py and the external code file).

    通过设置一些断点来检查是否存在由其他模块引起的错误。除非Flask明确抛出错误,否则它并不总是Flask(这适用于route.py和外部代码文件)。



  • Try getting the external Python code that you are trying to get into the directory of route.py and import it like a normal module. It might help resolve the external modules issue as well as compatibility errors.

    尝试将要获取的外部Python代码获取到route.py目录中,并像导入普通模块一样导入它。它可能有助于解决外部模块问题以及兼容性错误。



  • If that works, then go on debugging sys.path.append(os.path.abspath("/myTradingBot/apps/autoTrade/"))

    如果有效,那么继续调试sys.path.append(os.path.abspath(“/myTradingBot/apps/autoTrade/”)



  • If the error persists, then this might not be a syntax or logical error. Try updating the modules and also consider the external dependencies that conflict with the given application.

    如果错误仍然存在,则这可能不是语法或逻辑错误。尝试更新模块,同时考虑与给定应用程序冲突的外部依赖关系。




I can recommend these steps as there is no error specified. I hope it still helps.

我可以推荐这些步骤,因为没有指定错误。我希望它仍然有帮助。


更多回答

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com