Repository navigation
Description
Setup
Hi there. Huge huge fan of the Entrez module.
This is more of an FYI, in case it's useful to know my experience. I defer to the team about whether this enhancement rises to the level of a revision to the package.
I had a Flask web app that uses Entrez working on my local Windows 10 machine. When I deployed to Amazon Web Services as a Lambda function, it ran into trouble.
Entrez.email = "<my email address>"
handle = Entrez.esearch(db="pubmed", retmax=10, term=lookupID)
record = Entrez.read(handle)Expected behaviour
A record to a PubMed entry, as it does on my local machine.
Actual behaviour
On Lambda, esearch threw an exception and no record was read.
The error occurs in parser.py, makedirs:
- 'OSError: [Errno 30] Read-only file system: '/home/sbx_user1060'
Full trace here:
trace (showing error with Entrez module).docx.
Steps to reproduce
Deploy a python project to AWS Lambda function that calls an Entrez method, such as esearch or esummary.
Source of error
The trace shows the error happens when the Data Handler creates new directories for local_dtd and local_xsd. I learned that AWS Lambda only gives functions write access within a specific directory, 'tmp'. So Entrez is forbidden from creating the directories where it wants to.
if platform.system() == 'Windows':
directory = os.path.join(os.getenv("APPDATA"), "biopython")
else: # Unix/Linux/Mac
home = os.path.expanduser('~')
directory = os.path.join(home, '.config', 'biopython')
del home
local_dtd_dir = os.path.join(directory, 'Bio', 'Entrez', 'DTDs')
local_xsd_dir = os.path.join(directory, 'Bio', 'Entrez', 'XSDs')
del directory
del platform
try:
os.makedirs(local_dtd_dir) # use exist_ok=True on Python >= 3.2
except OSError as exception:
# Check if local_dtd_dir already exists, and that it is a directory.
# Trying os.makedirs first and then checking for os.path.isdir avoids
# a race condition.
if not os.path.isdir(local_dtd_dir):
raise exception
try:
os.makedirs(local_xsd_dir) # use exist_ok=True on Python >= 3.2Solution that worked for me
I forked Entrez and changed the directory for DataHandler. This allowed my project to work on Lambda.
Forked here: https://github.com/cparmet/Entrez-AWS
I replaced this code ...
if platform.system() == 'Windows':
directory = os.path.join(os.getenv("APPDATA"), "biopython")
else: # Unix/Linux/Mac
home = os.path.expanduser('~')
directory = os.path.join(home, '.config', 'biopython')
del home
With...
directory = '/tmp/'
Closing
I read that another serverless platform, Google Cloud Functions, also limits read/write access. But I'm not informed about it or what the workaround would be.
Thanks for reading.
Activity
peterjc commentedon Jan 28, 2018
Would exposing the cache location via something like
Bio.Entrez.cachework for you? I can see this might have other applications, e.g. running Biopython as apache as part of a webservice.CC @mdehoon
mdehoon commentedon Jan 29, 2018
@peterjc Yes exposing Bio.Entrez.cache would work. It's a bit fragile, since python will try to create the directory as soon as Bio.Entrez.Parser is imported.
So
works, but
wouldn't.
peterjc commentedon Jan 29, 2018
Hmm. Style wise it is unfortunate - lots of projects like to have all their imports at top level before any code (and there are flake8 plugins etc to enforce this).
We could delay looking at the cache folder, but I think even with that proviso it would work for this use case.
What do you think @cparmet ?
cparmet commentedon Jan 29, 2018
peterjc commentedon Jan 29, 2018
@cparmet would you like to make a pull request implementing this idea?
cparmet commentedon Jan 29, 2018
peterjc commentedon Nov 10, 2020
I believe this was fixed by exposing the cache setting.