Python 如何使其他模块可以访问某个模块
我有两个文件“mod1.py”和“mod2.py” mod1需要请求模块才能工作。但是我没有在mod1中导入它们,而是在mod2中导入了request和mod1模块 但是 我收到一个错误“名称”请求“未定义”。我知道,如果我直接在mod1中导入“请求”模块,它就可以正常工作。但我还想使用其他需要“请求”模块的模块那么,如何导入模块一次并使所有其他模块都可以访问该模块? mod1.pyPython 如何使其他模块可以访问某个模块,python,python-3.x,class,module,Python,Python 3.x,Class,Module,我有两个文件“mod1.py”和“mod2.py” mod1需要请求模块才能工作。但是我没有在mod1中导入它们,而是在mod2中导入了request和mod1模块 但是 我收到一个错误“名称”请求“未定义”。我知道,如果我直接在mod1中导入“请求”模块,它就可以正常工作。但我还想使用其他需要“请求”模块的模块那么,如何导入模块一次并使所有其他模块都可以访问该模块? mod1.py class getUrl(): def __init__(self, url): sel
class getUrl():
def __init__(self, url):
self.url = url
def grab_html(self):
html = requests.get(self.url).text
return html
import requests
import mod1
module1 = mod1.getUrl('https://www.wikipedia.org/')
HTML = module1.grab_html()
import requests
class getUrl():
def __init__(self, url):
self.url = url
def grab_html(self):
html = requests.get(self.url).text
return html
import mod1
module1 = mod1.getUrl('https://www.wikipedia.org/')
HTML = module1.grab_html()
# And also access requests via mod1
indirectly = mod1.requests.get('https://www.wikipedia.org/').text
mod2.py
class getUrl():
def __init__(self, url):
self.url = url
def grab_html(self):
html = requests.get(self.url).text
return html
import requests
import mod1
module1 = mod1.getUrl('https://www.wikipedia.org/')
HTML = module1.grab_html()
import requests
class getUrl():
def __init__(self, url):
self.url = url
def grab_html(self):
html = requests.get(self.url).text
return html
import mod1
module1 = mod1.getUrl('https://www.wikipedia.org/')
HTML = module1.grab_html()
# And also access requests via mod1
indirectly = mod1.requests.get('https://www.wikipedia.org/').text
编辑:完全错误
Traceback (most recent call last):
File "C:\Users\camel\Desktop\test\mod2.py", line 5, in <module>
HTML = module1.grab_html()
File "C:\Users\camel\Desktop\test\mod1.py", line 6, in grab_html
html = requests.get(self.url).text
NameError: name 'requests' is not defined
[Finished in 0.5s with exit code 1]
[shell_cmd: python -u "C:\Users\guru\Desktop\test\mod2.py"]
回溯(最近一次呼叫最后一次):
文件“C:\Users\camel\Desktop\test\mod2.py”,第5行,在
HTML=module1.grab_HTML()
文件“C:\Users\camel\Desktop\test\mod1.py”,第6行,以grab\U html格式
html=requests.get(self.url).text
NameError:未定义名称“请求”
[在0.5s内完成,退出代码为1]
[shell\u cmd:python-u“C:\Users\guru\Desktop\test\mod2.py”]
导入请求应在mod1.py中,因为它用于mod1.py中定义的类的方法中。如果mod2.py中也需要它,您可以在这两个位置导入它。您需要创建一个\uuuu init\uuuuuuuuuupy
文件(它可以是空的),以便包含mod1
的文件夹被识别为一个模块
然后,您可以从mod1 import*执行,或从path.to.mod1 import*
执行操作,它将把所有导入带到mod2
。查看相关答案。在我看来,这是一种明智的做法,因为您可以将所有依赖项保存在一个集中的位置
由于您担心内存利用率,请查看相同的问题。由于您没有在mod2.py中使用请求,您可以在mod1.py中执行导入请求
如果您担心内存,它将占用与您将在一个脚本中使用它相同的数量。但是,如果您正在使用,如果您计划在mod2.py中也使用它,那么您还必须在其中包含它 导入某个内容时,它将成为导入该内容的模块中的命名内容。请求不是由mod2.py直接使用的,而是由mod1.py直接使用的,所以您应该在那里导入它
例如,你可以这样做
mod1.py
class getUrl():
def __init__(self, url):
self.url = url
def grab_html(self):
html = requests.get(self.url).text
return html
import requests
import mod1
module1 = mod1.getUrl('https://www.wikipedia.org/')
HTML = module1.grab_html()
import requests
class getUrl():
def __init__(self, url):
self.url = url
def grab_html(self):
html = requests.get(self.url).text
return html
import mod1
module1 = mod1.getUrl('https://www.wikipedia.org/')
HTML = module1.grab_html()
# And also access requests via mod1
indirectly = mod1.requests.get('https://www.wikipedia.org/').text
mod2.py
class getUrl():
def __init__(self, url):
self.url = url
def grab_html(self):
html = requests.get(self.url).text
return html
import requests
import mod1
module1 = mod1.getUrl('https://www.wikipedia.org/')
HTML = module1.grab_html()
import requests
class getUrl():
def __init__(self, url):
self.url = url
def grab_html(self):
html = requests.get(self.url).text
return html
import mod1
module1 = mod1.getUrl('https://www.wikipedia.org/')
HTML = module1.grab_html()
# And also access requests via mod1
indirectly = mod1.requests.get('https://www.wikipedia.org/').text
错误来自哪一行?您可以发布完整的错误吗?每次需要时,您都必须导入请求。这会增加内存吗。如果我继续这样做?不,这不应该是一个问题,但这可能是一个不好的做法,因为您必须手动维护不同文件中的导入。否。不,这不是一个坏习惯。如果模块需要库,则必须导入它。您可以使用跟踪无用的导入语句。它是否会增加内存。这就是我所关心的,看看这个问题。Python并不是一种真正关心内存的语言。是的,我搞错了一些东西并修复了答案。您可以从mod1 import*