我正在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...
当我将鼠标悬停在灰色的文本上时,将出现一个弹出的“未使用的导入语句”,并带有可单击的“更多操作”选项:
这会打开“检查结果”窗格,并显示错误警告。请参阅屏幕以了解详细信息。
connect.py内部的代码最初位于run.py的顶部,我只想分成两个脚本。当它们都在同一个文件中时,脚本可以正常运行。只有在我分成两个文件时才发生这种情况。
如何将connect.py导入run.py?让我知道是否需要提供更多详细信息?
如果导入整个模块,请确保使用点运算符ex。foo.bar
如果使用python <3.3:
您需要创建一个空白的__init__.py
文件,以便python知道您所在的目录应被视为python软件包。
查看此答案中的更多内容here