Skip to main content
 首页 » 编程设计

python之缺少 1 个必需的位置参数,参数通过它传递

2025年05月04日20lexus

我一直收到这个错误,即使我已经通过 var 传递了它要求的值。我不确定为什么会这样。

class process_raw_snowplow_event_data(luigi.Task): 
    dataset_date = luigi.DateParameter(default=date.today() - timedelta(days=1)) 
    # force_run = luigi.BoolParameter() 
    _start = luigi.DateSecondParameter(default=datetime.utcnow()) 
    file_root = luigi.Parameter() 
 
 
    @staticmethod 
    def download_s3_file(self, s3_filename): 
 
        local_filename = "/Users/xxx/etl/%s" % s3_filename 
 
        s3_file_full_path = re.compile(r"snowplow-enrich-output/enriched/archive/run=" + strftime("%Y-%m-%d") +r"-\d{2}-\d{2}-\d{2}/") 
 
        try: 
            s3.download_file(Bucket=os.environ.get('SP_BUCKET'), Key=s3_filename, Filename=local_filename) 
        except Exception as e: 
            logger.error("%s - Could not retrieve %s because: %s" % ("download_s3_file()", s3_filename, e)) 
            raise    

class process_sp_data(process_raw_snowplow_event_data): 
    def run(self): 
        s3_filename = "part_%s.%s.json.gz" % (self.file_root, (self.dataset_date + timedelta(days=1)).strftime("%Y-%m-%d")) 
        infile_name = self.download_s3_file(s3_filename) 
        match_files = self.list_files(os.environ.get('SP_BUCKET')) 
 
        with gzip.open(self.output().path, "wb") as outfile: 
            with gzip.open(infile_name, "rb") as infile: 
                for line in infile: 
                    try: 
                        indict = json.loads(line) 
                    except Exception as e: 
                        logger.warn("s -Could not parse line: %s =%s" % (self.__class__.__name__, e, line)) 
                        continue 
 
                        outdict = indict 

错误是:

Traceback (most recent call last): 
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/luigi/worker.py", line 199, in run 
new_deps = self._run_get_new_deps() 
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/luigi/worker.py", line 139, in _run_get_new_deps 
task_gen = self.task.run() 
  File "target.py", line 123, in run 
infile_name = self.download_s3_file(s3_filename) 
TypeError: download_s3_file() missing 1 required positional argument: 's3_filename' 

为什么即使我已经通过它传递了参数,它还是不起作用?感谢您的帮助。

请您参考如下方法:

您已指定 @staticmethod,因此 self 不是隐式对象参数。所以当你打电话时:

self.download_s3_file(s3_filename) 

s3_filename 设置为 self 并且...缺少一个参数

要么删除 self 要么删除 @staticmethod

@staticmethod 
def download_s3_file(s3_filename): 

或

def download_s3_file(self, s3_filename): 

如果你不在方法中使用self,它是静态的,所以选择第一个选项