-1

I am trying to call function "searchF" in other file with redefined variable "search", but I am assume it doesn't work because function calls main thread in if __name__ == "__main__":

FileA.py

import FileB

search = "stackoverflow"    
searchF(search)

FileB.py

from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser

search = "Google"    
def searchF(search)
  DEVELOPER_KEY = "REPLACE_ME"
  YOUTUBE_API_SERVICE_NAME = "youtube"
  YOUTUBE_API_VERSION = "v3"

  def youtube_search(options):
    youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
      developerKey=DEVELOPER_KEY)

    search_response = youtube.search().list(
      q=options.q,
      type="video",
      part="id,snippet",
      maxResults=options.max_results
    ).execute()

    search_videos = []

    for search_result in search_response.get("items", []):
      search_videos.append(search_result["id"]["videoId"])
    video_ids = ",".join(search_videos)

    video_response = youtube.videos().list(
      id=video_ids,
      part='snippet, contentDetails'
    ).execute()

    videos = []

    for video_result in video_response.get("items", []):
      videos.append("%s, (%s,%s)" % (video_result["snippet"]["title"],
                                video_result["contentDetails"],
                                video_result["contentDetails"]))
    find = "licensedContent': True"
    result = ', '.join(videos)
    print find in result

  if __name__ == "__main__":
    argparser.add_argument("--q", help="Search term", default=search)
    argparser.add_argument("--max-results", help="Max results", default=25)
    args = argparser.parse_args()

    try:
      youtube_search(args)
    except HttpError, e:
      print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
Pavel Vanchugov
  • 383
  • 1
  • 3
  • 16
  • 3
    "It doesn't work" is not an adequate problem statement. You are only *assuming* it doesn't work? It looks like it should work just fine to me. What is the "main thread"? What is the error, if you've actually tried it. That is important information. – juanpa.arrivillaga Mar 03 '17 at 21:21
  • 3
    Actually, your code will throw a `NameError`, because `searchF` isn't defined. But that has nothing to do with `if __name__ == "__main__":` in `FileB.py`. It's because you need to use `FileB.searchF` if you used `import FileB` in `FileA.py` – juanpa.arrivillaga Mar 03 '17 at 21:24

1 Answers1

1
if __name__ == "__main__": 

(Very) simply put, it means what the script should do if it is launched from the terminal, like so

>>> python FileA.py

A much more in-depth discussion can be found here. But it is not why your import does not work.

From the code you pasted, it seems that your problem is the way you call searchF function. At the moment, it is not defined in FileA.py scope (module's symbol table to be precise). When you try to call it, it simply does not exist, it is not defined. However, you can reach it by calling it like so:

FileB.searchF(search)

If you want to call the function as you do, you should change your import to:

from FileB import searchF

This way you will be able to reach the function without prefixes. Good place to read more about this would be the docs.

Community
  • 1
  • 1
cegas
  • 2,823
  • 3
  • 16
  • 16
  • Thank you for answering my question and explaining what if __name doing. I am using python 2.7 and using your example now I am actually getting something in console. It's says NameError: name 'FileB' is not defined when I am using FileB.searchF(search) – Pavel Vanchugov Mar 03 '17 at 21:39
  • 1
    Did you perhaps write your import as `from FileB import searchF` and then tried to invoke the function by writing `FileB.searchF(search)`? If so, the correct way would be to invoke it by simply calling `searchF(search)`. In short, if your import is `import FileB`, you should call function like so: `FileB.searchF(search)`. If your import reads as `from FileB import searchF`, the function call should be `searchF(search)`. – cegas Mar 03 '17 at 21:52
  • It works when I test it with "print 'test'" but with actual code there is no results, it's like this code just skipped. – Pavel Vanchugov Mar 03 '17 at 22:08
  • 1
    Hm. I would suggest to create another question, as now the problem is different (the code does not seem to execute). In it, include the code calling the function, as well as the imported function. If you get any errors, definitely include those as well. Mention what you have tried (e.g. printing 'test') and what did not work, too. Unfortunately, from the context that I have now I cannot make any definitive guesses, and I do not want to mislead you. Creating a new question will allow you to frame it better and more eyes will see it (which usually means quicker resolution). – cegas Mar 03 '17 at 22:29
  • I have created new question http://stackoverflow.com/questions/42589824/function1-from-other-file-fail-when-that-function1-is-calling-another-function2 – Pavel Vanchugov Mar 03 '17 at 22:54