0

I'm working on a project in PyCharm and I have two files: connect.py and run.py. Both of these files are in the root of the project directory.

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...

The first line, import connect is greyed out: enter image description here

When I hover over the greyed out text a popup 'unused import statement' appears with a clickable 'more actions' option:

enter image description here

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. 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? Let me know if there are more details I should provide?

Doug Fir
  • 19,971
  • 47
  • 169
  • 299

1 Answers1

1

If importing a whole module, make sure to use the dot operator ex.foo.bar

If using 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.

See more in this answer here

  • Hi thanks for the tip. I added an empty file __init__.py but nothing happened or changed. I noticed on the link you shared a comment: ```Update: The file __init__.py was required under Python 2.X and is still required under Python 2.7.12 (I tested it) but it is no longer required from (allegedly) Python 3.3 onwards, and is not required under Python 3.4.3 (I tested it). See stackoverflow.com/questions/37139786 for more details. – Rob_before_edits ``` I'm using Python3.7 – Doug Fir Sep 30 '19 at 01:24
  • hmm good point. Have you tried just importing the desired functions? like `from foo import bar` or you could try `from foo import *` – Josh Kearney Sep 30 '19 at 01:28
  • Hi Josh. Yes that does appear to work! However, would be good to understand how/why I cannot just use ```import connect```. I'll run with this meantime but leave the question open – Doug Fir Sep 30 '19 at 01:31
  • 1
    @DougFir so after some more digging I found [this](https://www.jetbrains.com/help/pycharm/configuring-folders-within-a-content-root.html). So it looks like you need to tell pycharm what directory contains your project files. – Josh Kearney Sep 30 '19 at 01:43
  • I found the issue by accident. I need to refer to connect methods throughout the script with ```connect.somemethod``` first otherwise PyCharm will just grey it out. I was calling methods directly without prepending them with ```connect.``` – Doug Fir Sep 30 '19 at 01:46
  • Ahh yes. That would do it too. The Sources Root seems to also have made pycharm happy for me (i was getting red lines under local packages). – Josh Kearney Sep 30 '19 at 01:48