Python 执行另一个文件

Python executes another file

我制作了包含以下代码的文件“DWDfunctional.py”:

def ForecastDownloader(system,ModelName,year,Month):
     import cdsapi
     c = cdsapi.Client()
     c.retrieve(
        'seasonal-monthly-single-levels',
        {
            'originating_centre': ModelName,
            'system': system,
            'variable': 'total_precipitation',
            'product_type': 'monthly_mean',
            'year': year,
            'month': Month,
            'area'          : [40, 44, 24, 64],
            'leadtime_month': [
                '1', '2', '3',
                '4', '5', '6',
            ],
            'format': 'netcdf',
        },
        'dwd.nc')

然后我调用了另一个文件中的函数:

from DWDfunctional.py import ForecastDownloader

ForecastDownloader('2','dwd',2020,2)

我想知道,当我运行上面两行代码时,python执行另一个文件夹中的另一个旧代码!并以错误的文件名下载错误的文件,没有错误!!

导入后,您可以检查 module.__file__ 以查看您的 module 来自哪里。

在您的特定情况下,您可以检查 ForecastDownloader.__file__ 应该会给您正确的文件路径。