尝试访问模块功能时的AttributeError

时间:2017-10-14 23:37:10

标签: python module

我正在编写一个在the invoke framework内运行的任务:

from invoke import task

@task
def profitreport(ctx, ini=None):
    from lib import report
    report.profit.main(ini)

但是,当我运行任务时,我得到以下错误追溯:

schemelab@metta:~/prg/surgetrader/src$ invoke profitreport
Processing ini-mikegardner936@gmail.ini
Traceback (most recent call last):
  File "/home/schemelab/install/miniconda3/bin/invoke", line 11, in <module>
    sys.exit(program.run())
  File "/home/schemelab/install/miniconda3/lib/python3.6/site-packages/invoke/program.py", line 293, in run
    self.execute()
  File "/home/schemelab/install/miniconda3/lib/python3.6/site-packages/invoke/program.py", line 414, in execute
    executor.execute(*self.tasks)
  File "/home/schemelab/install/miniconda3/lib/python3.6/site-packages/invoke/executor.py", line 129, in execute
    result = call.task(*args, **call.kwargs)
  File "/home/schemelab/install/miniconda3/lib/python3.6/site-packages/invoke/tasks.py", line 115, in __call__
    result = self.body(*args, **kwargs)
  File "/home/schemelab/prg/surgetrader/src/tasks.py", line 43, in profitreport
    report.profit.main(ini)
AttributeError: module 'lib.report' has no attribute 'profit'
schemelab@metta:~/prg/surgetrader/src$ 

主要功能肯定存在于src/lib/report/profit.py中,并且我在目录路径中都有__init__.py个文件,但由于某种原因它没有被调用。

这是文件系统层次结构:

  /home/schemelab/prg/surgetrader/src:
  total used in directory 19568 available 21885552
  drwxr-xr-x 8 schemelab schemelab     4096 Oct 14 19:05 .
  drwxr-xr-x 5 schemelab schemelab     4096 Oct 12 15:49 ..
  drwxr-xr-x 4 schemelab schemelab     4096 Oct 14 09:04 lib
  -rw-r--r-- 1 schemelab schemelab      910 Oct 14 09:10 tasks.py

  /home/schemelab/prg/surgetrader/src/lib:
  total used in directory 60 available 21885552
  drwxr-xr-x 4 schemelab schemelab 4096 Oct 14 09:04 .
  drwxr-xr-x 8 schemelab schemelab 4096 Oct 14 19:05 ..
  -rw-r--r-- 1 schemelab schemelab    1 Jul 15 02:26 __init__.py
  drwxr-xr-x 2 schemelab schemelab 4096 Oct 14 09:06 report

  /home/schemelab/prg/surgetrader/src/lib/report:
  total used in directory 16 available 21885552
  drwxr-xr-x 2 schemelab schemelab 4096 Oct 14 09:06 .
  drwxr-xr-x 4 schemelab schemelab 4096 Oct 14 09:04 ..
  -rw-r--r-- 1 schemelab schemelab    1 Oct 14 09:04 __init__.py
  -rw-r--r-- 1 schemelab schemelab 2379 Oct 14 09:06 profit.py

这里是profit.py

#!/usr/bin/env python


import configparser
import argh
import collections
import logging
from retry import retry
from db import db
import mybittrex
from bittrex.bittrex import SELL_ORDERBOOK
from pprint import pprint

def loop_forever():
    while True:
        pass


logger = logging.getLogger(__name__)

def open_order(result):

    pprint(result['IsOpen'])
    return result['IsOpen']


def report_profit(config_file, b):
    import csv
    csv_file = "tmp/" + config_file + ".csv"
    csvfile = open(csv_file, 'w', newline='')
    fieldnames = 'sell_closed sell_opened market units_sold sell_price sell_commission units_bought buy_price buy_commission profit'.split()
    csv_writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    csv_writer.writeheader()

    for buy in db().select(
        db.buy.ALL,
        orderby=~db.buy.timestamp
    ):


        if buy.config_file != config_file:
            #print("config file != {}... skipping".format(config_file))
            continue

        if len(buy.sell_id) < 12:
            #print("No sell id ... skipping")
            continue

        so = b.get_order(buy.sell_id)['result']

        if open_order(so):
            print("Open order ... skipping")
            continue

        pprint(buy)
        pprint(so)

        sell_proceeds = so['Price'] - so['CommissionPaid']

        bo = b.get_order(buy.order_id)['result']

        buy_proceeds = bo['Price'] + bo['CommissionPaid']

        pprint("sell_proceeds = {}. buy Order = {}. buy proceeds = {}".format(
            sell_proceeds, bo, buy_proceeds))

        profit = sell_proceeds - buy_proceeds

        calculations = {
            'sell_closed': so['Closed'],
            'sell_opened': so['Opened'],
            'market': so['Exchange'],
            'units_sold': so['Quantity'],
            'sell_price': so['PricePerUnit'],
            'sell_commission': so['CommissionPaid'],
            'units_bought': bo['Quantity'],
            'buy_price': bo['PricePerUnit'],
            'buy_commission': bo['CommissionPaid'],
            'profit': profit
        }

        csv_writer.writerow(calculations)


def main(ini):

    config_file = ini
    config = configparser.RawConfigParser()
    config.read(config_file)

    b = mybittrex.make_bittrex(config)
    report_profit(config_file, b)

if __name__ == '__main__':
    argh.dispatch_command(main)

1 个答案:

答案 0 :(得分:1)

您有以下可导入的软件包/模块:

  • lib
  • lib.report
  • lib.report.profit(包含少量功能)

在您的任务功能中,您导入lib.report,但访问了未导入的lib.report.profit。因此,您会收到profit不存在的错误。

Python不会在函数调用中查看文件系统,而只会在导入时查看。因此,在进行lib.report.profit调用时,它不知道...main()模块是否存在。

将其更改为以下内容:

from lib.report.profit import main
main(ini)

PS:为此目的,main()函数的使用是一个单独的主题,但它超出了导入问题的范围。