在python中如何从父目录导入文件?

2024-04-25 23:47:53 发布

您现在位置:Python中文网/ 问答频道 /正文

在python中如何从父目录导入文件?在

我的项目树如下:

enter image description here

我想从vehiclesListCrawler中的__main__.py文件导入父目录中的common.py。在

现在我这样做:

from common import get_init_url, create_urls_to_fetch, MILEAGE_RANGES, notify_on_error

但我得到一个错误:

^{pr2}$

有什么想法吗?在

更新

来自here的任何内容都没有帮助。这就是为什么我贴出了这个新问题。在

__main__.py内,我有如下主要功能:

def main(params):
    try:
        process.crawl(AutoscoutListSpider, params)
        process.start()
        return {"Success ": main_result}
    except Exception as e:
        return {"Error ": e, "params ": params}

我运行如下:

main({"make":"Audi", "model":"A3", "mileage":"2500"})

Tags: 文件项目frompyimport目录urlget
1条回答
网友
1楼 · 发布于 2024-04-25 23:47:53
方式_一_ : _绝对_导入_ :_
from asCrawlers.common import get_init_url, create_urls_to_fetch, MILEAGE_RANGES, notify_on_error

方式二:相对导入:

^{pr2}$

有关导入的更多信息here

When specifying what module to import you do not have to specify the absolute name of the module. When a module or package is contained within another package it is possible to make a relative import within the same top package without having to mention the package name. By using leading dots in the specified module or package after from you can specify how high to traverse up the current package hierarchy without specifying exact names. One leading dot means the current package where the module making the import exists. Two dots means up one package level. Three dots is up two levels, etc.

相关问题 更多 >

    热门问题