我正在创建一个供内部使用的python包,它具有一些其他模块共有的内部功能。例如,下面的功能正在其他模块上使用-
def GetLocalImage(WebImage):
ImageLink = WebImage.get("data-src")
FileName = ImageLink.split("/")[-1]
urllib.request.urlretrieve(ImageLink,FileName)
return(FileName)
您可以看到该功能需要使用urllib
。下面是包的文件结构-
formatter
\ __init__.py
\ File1.py --> It would call GetLocalImage()
\ File2.py --> It would call GetLocalImage()
我的主程序使用from formatter import *
语句。我的问题是-
import urllib
和功能的正确位置是什么?我终于明白了。这是我解决的方法-
common.py
common.py
中,我有import urllib.request
File1.py
和File2.py
中,我已像from .common import *
一样导入import formatter
__init__.py
文件中,我保留-from .common import *
from .File1 import *
from .File2 import *
我认为我们可以跳过__init__.py
文件(不确定)。希望这对以后的人有所帮助。