使用非标准名称导入 Python 脚本
Importing Python script with nonstandard name
我正在编写一系列 python 脚本来扩展 QGIS 软件套件。因此,我试图遵循他们的命名约定。在本例中,我尝试导入的文件名为 "r.sun.distributed"。我试过使用 import r.sun.distributed 和 import("r.sun.distributed") 但是,python 表示在这两种情况下都找不到该名称的模块。
是否有解决方法,或者我是否需要重命名我的脚本以使其符合 Python 的约定?
您必须将其转换为 .pyc 才能调用它。
import py_compile
py_compile.compile('filename.py')
您可以使用 imp 模块导入没有 .py
扩展名的模块:
import imp
imp.load_source(path)
我正在编写一系列 python 脚本来扩展 QGIS 软件套件。因此,我试图遵循他们的命名约定。在本例中,我尝试导入的文件名为 "r.sun.distributed"。我试过使用 import r.sun.distributed 和 import("r.sun.distributed") 但是,python 表示在这两种情况下都找不到该名称的模块。
是否有解决方法,或者我是否需要重命名我的脚本以使其符合 Python 的约定?
您必须将其转换为 .pyc 才能调用它。
import py_compile
py_compile.compile('filename.py')
您可以使用 imp 模块导入没有 .py
扩展名的模块:
import imp
imp.load_source(path)