我正在PyCharm中做一个项目,我有两个文件: connect.py和run.py。这两个文件都位于项目目录的根目录中。
connect.py
"""Hello Analytics Reporting API V4."""
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
KEY_FILE_LOCATION = 'client_secrets.json'
VIEW_ID = '123456' # My actual view ID is here
def initialize_analyticsreporting():
"""Initializes an Analytics Reporting API V4 service object.
Returns:
An authorized Analytics Reporting API V4 service object.
"""
credentials = ServiceAccountCredentials.from_json_keyfile_name(
KEY_FILE_LOCATION, SCOPES)
# Build the service object.
analytics = build('analyticsreporting', 'v4', credentials=credentials)
return analytics
run.py
import connect
# some more code here...
第一行,import connect
是灰色的:
当我悬停在灰色的文本上时,弹出的‘未使用的导入语句’将显示一个可点击的‘更多操作’选项:
这将打开带有错误警告的“检查结果”窗格。有关细节,请参见屏幕。
connect.py内部的代码最初位于run.py的顶部,我只想把它分成两个脚本。当所有文件都在同一个文件中时,脚本运行得很好。只有当我把文件分成两个文件时才会发生这种情况。
如何将connect.py导入run.py?如果我需要提供更多的细节,请告诉我?
发布于 2019-09-29 17:18:57
如果导入整个模块,请确保使用点运算符ex.foo.bar
。
如果使用python <3.3::
您需要创建一个空白的__init__.py
文件,以便python知道您所在的目录应该被视为python包。
请参阅这个答案here中的更多内容
https://stackoverflow.com/questions/58160370
复制