Conflict between PyStan and Google Auth
Description:
There's some sort of conflict between PyStan and the Google Auth libs due to the use of pyximport.
For more context, we recently introduced Facebook's Prophet (which uses PyStan) into our project which already used Pandas + Panda's BigQuery libs (which use Google's BigQuery libs, which includes their auth libs). We discovered importing Prophet causes anything involving BigQuery to explode with a horrific traceback.
You can find the original issue I raised on Prophet here: facebook/prophet#363 I also raised one on google-auth: GoogleCloudPlatform/google-auth-library-python#222
Reproducible Steps:
General breaking case found by @bletham on facebook/prophet#363 (probably most relevant):
import pystan
model_code = 'parameters {real y;} model {y ~ normal(0,1);}'
model = pystan.StanModel(model_code=model_code) # this will take a minute
from google.auth import app_engine
Our working test (prompts for authentication):
import pandas as pd
df = pd.read_gbq('test', 'test')
Our breaking test:
import pandas as pd
from fbprophet import Prophet
df = pd.read_gbq('test', 'test')
Gives this traceback:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 879, in _find_spec
AttributeError: 'PyxImporter' object has no attribute 'find_spec'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 4, in <module>
df = pd.read_gbq('give me an error', 'a project id')
File "./venv/lib/python3.6/site-packages/pandas/io/gbq.py", line 100, in read_gbq
**kwargs)
File "./venv/lib/python3.6/site-packages/pandas_gbq/gbq.py", line 906, in read_gbq
dialect=dialect, auth_local_webserver=auth_local_webserver)
File "./venv/lib/python3.6/site-packages/pandas_gbq/gbq.py", line 202, in __init__
self.credentials = self.get_credentials()
File "./venv/lib/python3.6/site-packages/pandas_gbq/gbq.py", line 214, in get_credentials
credentials = self.get_application_default_credentials()
File "./venv/lib/python3.6/site-packages/pandas_gbq/gbq.py", line 243, in get_application_default_credentials
credentials, _ = google.auth.default(scopes=[self.scope])
File "./venv/lib/python3.6/site-packages/google/auth/_default.py", line 287, in default
credentials, project_id = checker()
File "./venv/lib/python3.6/site-packages/google/auth/_default.py", line 164, in _get_gae_credentials
from google.auth import app_engine
File "./venv/lib/python3.6/site-packages/google/auth/app_engine.py", line 32, in <module>
from google.appengine.api import app_identity
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 946, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 881, in _find_spec
File "<frozen importlib._bootstrap>", line 855, in _find_spec_legacy
File "./venv/lib/python3.6/site-packages/pyximport/pyximport.py", line 253, in find_module
fp, pathname, (ext,mode,ty) = imp.find_module(fullname,package_path)
File "~/.pyenv/versions/3.6.1/lib/python3.6/imp.py", line 270, in find_module
"not {}".format(type(path)))
RuntimeError: 'path' must be None or a list, not <class '_frozen_i
Requirements with this setup:
cachetools==2.0.1
certifi==2017.11.5
chardet==3.0.4
cycler==0.10.0
Cython==0.27.3
fbprophet==0.2.1
google-api-python-client==1.6.4
google-auth==1.2.1
google-auth-httplib2==0.0.3
google-auth-oauthlib==0.2.0
httplib2==0.10.3
idna==2.6
matplotlib==2.1.0
numpy==1.13.3
oauth2client==4.1.2
oauthlib==2.0.6
pandas==0.21.0
pandas-gbq==0.2.0
pyasn1==0.3.7
pyasn1-modules==0.1.5
pyparsing==2.2.0
pystan==2.17.0.0
python-dateutil==2.6.1
pytz==2017.3
requests==2.18.4
requests-oauthlib==0.8.0
rsa==3.4.2
six==1.11.0
uritemplate==3.0.0
urllib3==1.22
PyStan Version:
2.17.0.0
Python Version:
3.6.1
Operating System:
macOS 10.1.2.3
Interesting. Probably related to this
https://groups.google.com/forum/m/#!topic/cython-users/lgECSn-0hRY
Currently not sure why they are interfering. Is there some global parameter in pyximport? So it will fail after the PyStan model is compiled and imported (loaded?) to python environment (from tmp folder).
Will it fail if one uses pickled model?
Pickle the model in one session
with open(path, 'wb') as f:
pickle.dump(sm, f, protocol=-1)
And then in another session
with open(path, 'rb') as f:
sm = pickle.load(f)
import google...
I haven't tested the pickle yet but as a workaround, you can import both at the start of your application with the order switched and it's fine (since modules are only imported once).
import fbprophet
import pandas
It's gross, but it makes it work.
The pickle thing fails also.
This doesn't seem like a PyStan issue. Glad to see there's a workaround.
My team is also experiencing problems with this issue in the latest version of pystan 2.18.0, conflicting with the google cloud libraries. The order of the imports is not a real solution here, we have even gone to the point where we have a unique module with all the imports of pystan and google libraries where pystan is always imported first but is still raising these exceptions. We are running this on celery workers which are configured to die after each execution as defined here celery docs which is creating the issue to appear randomly. The key here is that restarting the processes means reimporting the modules and despite doing it in the "right" order the exception comes and goes. There must be something else happening in the import of pystan, maybe conditional, maybe lazy evaluated, that is polluting the namespace of the google libraries.
Yes, this was a bit weird problem.
It has something to do with pyximport but not sure what. Some global parameter probably, or something gets killed/dereferenced.
Is there a way to see is really going on with the import? Lower level?