【问题标题】:Python executes another filePython 执行另一个文件
【发布时间】:2020-04-13 05:35:51
【问题描述】:

我制作了包含以下代码的文件'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 执行另一个文件夹中的另一个旧代码!!!并以错误的文件名下载错误的文件而没有错误!

【问题讨论】:

  • 我又想知道,当我调用“DWDfunctional.py”中的函数时,它会正确执行!它运行正确。但我需要在该文件之外调用函数。
  • 导入后,您可以检查module.__file__ 以查看您的模块来自何处。并从导入中删除.py,只需名称就足够了。
  • 如何检查 module.__file__
  • ForecastDownloader.__file__
  • 请将此帮助添加为该问题的答案,以便我接受

标签: python function call


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 2014-07-03
    • 2016-12-12
    • 2017-12-21
    • 2019-02-20
    相关资源
    最近更新 更多