简体   繁体   English

导入文件名是灰色的

[英]import filename is greyed out

I'm working on a project in PyCharm and I have two files: connect.py and run.py.我正在 PyCharm 中开展一个项目,我有两个文件:connect.py 和 run.py。 Both of these files are in the root of the project directory.这两个文件都在项目目录的根目录中。

connect.py连接.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运行.py

import connect

# some more code here...

The first line, import connect is greyed out:第一行import connect是灰色的: 在此处输入图像描述

When I hover over the greyed out text a popup 'unused import statement' appears with a clickable 'more actions' option:当我在灰色文本上使用 hover 时,会出现一个弹出式“未使用的导入语句”,并带有一个可单击的“更多操作”选项:

在此处输入图像描述

This opens an 'inspection results' pane with an error warning.这将打开一个带有错误警告的“检查结果”窗格。 See screen for details.有关详细信息,请参阅屏幕。

The code inside of connect.py was initially at the top of run.py, I just wanted to separate into two scripts. connect.py 内部的代码最初位于 run.py 的顶部,我只是想分成两个脚本。 When it was all in the same file the script ran fine.当它们都在同一个文件中时,脚本运行良好。 It's only when I've separated out into two files that this happened.只有当我分成两个文件时才会发生这种情况。

How can I import connect.py into run.py?如何将 connect.py 导入 run.py? Let me know if there are more details I should provide?让我知道我是否应该提供更多详细信息?

If importing a whole module, make sure to use the dot operator ex.如果导入整个模块,请确保使用点运算符 ex。 foo.bar

If using python < 3.3:如果使用 python < 3.3:

You need to create a blank __init__.py file so that python knows the directory you're in should be treated as a python package.您需要创建一个空白的__init__.py文件,以便 python 知道您所在的目录应该被视为 python package。

See more in this answer here答案中查看更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM