Skip to main content
 首页 » 编程设计

python之使用一条语句导入多个模块

2025年05月04日14sky-heaven

我每天使用多个模块,每次我想使用它们时都导入它们非常麻烦。

我希望有这样的东西

#essentials.py 
from bs4 import BeautifulSoup 
import requests 
etc etc 

然后在其他方面,我可以做:

import essentials 
r = requests.get(example) #Requests is not defined here, as I have not imported it 
soup  = BeautifulSoup(r, 'lxml') 

请您参考如下方法:

from essentials import * 

这会将 essentials.py 中的所有名称放入模块的命名空间,如果这是在模块的顶层完成的话。

所以你可以这样做

from essentials import * 
r = requests.get(example) 
soup  = BeautifulSoup(r, 'lxml') 

看看the official documentation for reference .