Python - failed to import external python program under different folder

39 Views Asked by At

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:

  • 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

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.

Note: in my bot.py, i have alredy commented out the protected 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

1

There are 1 best solutions below

0
Aditya Patil On

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.

  • 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).

  • 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.

  • If that works, then go on debugging 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.