当我必须使用 "from module import method" 时如何模仿 "import module" 的使用
How to mimic the use of "import module" when I have to use "from module import method"
Google OAuth2 implementation in Python 的打包方式让我不得不
from google.oauth2 import service_account
credentials = service_account.Credentials...
由于我更愿意明确说明我的外部方法的来源,因此我非常希望
import google.oauth2
credentials = google.oauth2.service_account.Credentials...
但这不起作用,an answer to another question neatly explains why (there are also many other 外植,或多或少离子深度)。
有没有办法模仿 "full path notation" 以便我可以使用第二个版本,限制迫使我使用 from module import ...
?
我试过了
from google.oauth2 import service_account as google.oauth2.service_account
credentials = google.oauth2.service_account.Credentials....
但我得到
File "d:\Nextcloud\dev-perso\testing\googlecalendar\googlecalendar.py", line 4
from google.oauth2 import service_account as google.oauth2.service_account
^
SyntaxError: invalid syntax
是否有其他解决方案,还是我坚持使用短名称?
注意:我可以
from google.oauth2 import service_account as google_oauth2_service_account
credentials = google_oauth2_service_account.Credentials...
但这不是那么优雅(这是一个主观意见,如果没有更好的解决方案,我最终会使用它)
您可以使用绝对路径导入模块 service_account
:
import google.oauth2.service_account
google.oauth2.service_account.Credentials
#<class 'google.oauth2.service_account.Credentials'>
Google OAuth2 implementation in Python 的打包方式让我不得不
from google.oauth2 import service_account
credentials = service_account.Credentials...
由于我更愿意明确说明我的外部方法的来源,因此我非常希望
import google.oauth2
credentials = google.oauth2.service_account.Credentials...
但这不起作用,an answer to another question neatly explains why (there are also many other
有没有办法模仿 "full path notation" 以便我可以使用第二个版本,限制迫使我使用 from module import ...
?
我试过了
from google.oauth2 import service_account as google.oauth2.service_account
credentials = google.oauth2.service_account.Credentials....
但我得到
File "d:\Nextcloud\dev-perso\testing\googlecalendar\googlecalendar.py", line 4
from google.oauth2 import service_account as google.oauth2.service_account
^
SyntaxError: invalid syntax
是否有其他解决方案,还是我坚持使用短名称?
注意:我可以
from google.oauth2 import service_account as google_oauth2_service_account
credentials = google_oauth2_service_account.Credentials...
但这不是那么优雅(这是一个主观意见,如果没有更好的解决方案,我最终会使用它)
您可以使用绝对路径导入模块 service_account
:
import google.oauth2.service_account
google.oauth2.service_account.Credentials
#<class 'google.oauth2.service_account.Credentials'>