Tommy
Tommy

Reputation: 11

BioPython Entrez not returning results when called as a module

I'm trying to use PubMed's Entrez to search papers via the BioPython module. The issue that I'm having, is that when I run the search script as a standalone it works, but when I call it from another script it returns an empty result. I've included the example below.

PaperSearch.py

from Bio import Entrez

def search(query):
    Entrez.email = '[email protected]'
    handle = Entrez.esearch(db='pubmed',
                        sort='relevance',
                        retmax='50',
                        retmode='xml',
                        term=query)
    results = Entrez.read(handle)
    return results

if __name__ == '__main__':
    results = search('cancer')
    print(results)

Main.py

import PaperSearch

query = 'cancer'
results = PaperSearch.search(query)
print results

This is Python 2.7 on Windows 7.

Thanks

Upvotes: 0

Views: 214

Answers (2)

Tommy
Tommy

Reputation: 11

To get round this issue, I had to make a hack of calling the def from inside PaperSearch.py and then returning the output.

PaperSearch.py

from Bio import Entrez

def search(query):
    Entrez.email = '[email protected]'
    handle = Entrez.esearch(db='pubmed',
                    sort='relevance',
                    retmax='50',
                    retmode='xml',
                    term=query)
    results = Entrez.read(handle)
    return results

def start_search(query):
    papers = search(query)

    return papers

if __name__ == '__main__':
    results = search('cancer')
    print(results)

Main.py

import PaperSearch

query = 'cancer'
results = PaperSearch.start_search(query)
print results

Not sure if this is because I'm running on Windows, but haven;'t tested out no Linux yet, as I'm writing this to be run on Windows only currently.

Upvotes: 0

azalea
azalea

Reputation: 12630

The module file name is PaperSearch.py, but it seems you did not import the correct name.

Change Main.py to:

import PaperSearch

query = 'cancer'
results = PaperSearch.search(query)
print results

Upvotes: 1

Related Questions