Skip to content

Attribute error on bigquery import #646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pj598 opened this issue May 4, 2021 · 8 comments
Closed

Attribute error on bigquery import #646

pj598 opened this issue May 4, 2021 · 8 comments
Labels
api: bigquery Issues related to the googleapis/python-bigquery API. type: question Request for information or clarification. Not an issue.

Comments

@pj598
Copy link

pj598 commented May 4, 2021

I've been getting an attribute error whenever I try to import pandas-gbq. They suggested I try asking for help here, since it's breaking on the bigquery import.

Traceback (most recent call last):
File "/Users/Desktop/temp.py", line 4, in
import pandas_gbq
File "/Users/opt/anaconda3/lib/python3.8/site-packages/pandas_gbq/init.py", line 1, in
from .gbq import to_gbq, read_gbq, Context, context # noqa
File "/Users/opt/anaconda3/lib/python3.8/site-packages/pandas_gbq/gbq.py", line 12, in
from google.cloud import bigquery
File "/Users/opt/anaconda3/lib/python3.8/site-packages/google/cloud/bigquery/init.py", line 35, in
from google.cloud.bigquery.client import Client
File "/Users/opt/anaconda3/lib/python3.8/site-packages/google/cloud/bigquery/client.py", line 58, in
from google.cloud.bigquery import _pandas_helpers
File "/Users/opt/anaconda3/lib/python3.8/site-packages/google/cloud/bigquery/_pandas_helpers.py", line 40, in
from google.cloud.bigquery import schema
File "/Users/opt/anaconda3/lib/python3.8/site-packages/google/cloud/bigquery/schema.py", line 28, in
"STRING": types.StandardSqlDataType.STRING,
AttributeError: type object 'StandardSqlDataType' has no attribute 'STRING'

  • Python version: 3.8
  • pip version: 21.0.1
  • google-cloud-bigquery version: 2.15.0
    Thanks!
@product-auto-label product-auto-label bot added the api: bigquery Issues related to the googleapis/python-bigquery API. label May 4, 2021
@plamut plamut added the type: question Request for information or clarification. Not an issue. label May 5, 2021
@plamut
Copy link
Contributor

plamut commented May 5, 2021

@pj598 I was not able to reproduce the error in a fresh 3.8 virtualenv. Simply running pip install pandas-gbq and then importing it worked:

$ python
Python 3.8.0 (default, Nov  7 2019, 13:39:39) 
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas_gbq
>>>

Would you mind sharing the output of pip freeze? I'm pasting mine for the reference to see if there are any version mismatches. In my case, pip pulled in pandas-gbq==0.15.0:

$ pip freeze
cachetools==4.2.2
certifi==2020.12.5
cffi==1.14.5
chardet==4.0.0
google-api-core==1.26.3
google-auth==1.30.0
google-auth-oauthlib==0.4.4
google-cloud-bigquery==2.15.0
google-cloud-bigquery-storage==2.4.0
google-cloud-core==1.6.0
google-crc32c==1.1.2
google-resumable-media==1.2.0
googleapis-common-protos==1.53.0
grpcio==1.37.1
idna==2.10
libcst==0.3.18
mypy-extensions==0.4.3
numpy==1.20.2
oauthlib==3.1.0
packaging==20.9
pandas==1.2.4
pandas-gbq==0.15.0
proto-plus==1.18.1
protobuf==3.15.8
pyarrow==3.0.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.20
pydata-google-auth==1.2.0
pyparsing==2.4.7
python-dateutil==2.8.1
pytz==2021.1
PyYAML==5.4.1
requests==2.25.1
requests-oauthlib==1.3.0
rsa==4.7.2
six==1.15.0
typing-extensions==3.10.0.0
typing-inspect==0.6.0
urllib3==1.26.4

@plamut
Copy link
Contributor

plamut commented May 5, 2021

This part of the traceback is interesting:

...
from google.cloud.bigquery import schema
File "/Users/opt/anaconda3/lib/python3.8/site-packages/google/cloud/bigquery/schema.py", line 28, in
"STRING": types.StandardSqlDataType.STRING,
AttributeError: type object 'StandardSqlDataType' has no attribute 'STRING'

The schema module tries to access types.StandardSqlDataType.STRING in line 28, but in the most recent version, that is actually line 29 referencing types.StandardSqlDataType.TypeKind.STRING (note the .TypeKind part).

This part was changed 7 months ago in a preparation for the 2.0 release, thus I suspect a different version of google-cloud-bigquery is used than the one reported here.

Could you double check and print the version at runtime? What's the output?

Edit:
I was able to reproduce the error by installing an 1.x version of google-cloud-bigquery, which I think is the problem here:

$ pip install "google-cloud-bigquery < 2.0.0"
...
Installing collected packages: google-cloud-bigquery
  Attempting uninstall: google-cloud-bigquery
    Found existing installation: google-cloud-bigquery 2.15.0
    Uninstalling google-cloud-bigquery-2.15.0:
      Successfully uninstalled google-cloud-bigquery-2.15.0
Successfully installed google-cloud-bigquery-1.28.0
$ python
Python 3.8.0 (default, Nov  7 2019, 13:39:39) 
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas_gbq
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/peter/workspace/python-bigquery/env/py3.8/lib/python3.8/site-packages/pandas_gbq/__init__.py", line 1, in <module>
    from .gbq import to_gbq, read_gbq, Context, context  # noqa
  File "/home/peter/workspace/python-bigquery/env/py3.8/lib/python3.8/site-packages/pandas_gbq/gbq.py", line 12, in <module>
    from google.cloud import bigquery
  File "/home/peter/workspace/python-bigquery/env/py3.8/lib/python3.8/site-packages/google/cloud/bigquery/__init__.py", line 35, in <module>
    from google.cloud.bigquery.client import Client
  File "/home/peter/workspace/python-bigquery/env/py3.8/lib/python3.8/site-packages/google/cloud/bigquery/client.py", line 61, in <module>
    from google.cloud.bigquery import _pandas_helpers
  File "/home/peter/workspace/python-bigquery/env/py3.8/lib/python3.8/site-packages/google/cloud/bigquery/_pandas_helpers.py", line 41, in <module>
    from google.cloud.bigquery import schema
  File "/home/peter/workspace/python-bigquery/env/py3.8/lib/python3.8/site-packages/google/cloud/bigquery/schema.py", line 28, in <module>
    "STRING": types.StandardSqlDataType.STRING,
AttributeError: type object 'StandardSqlDataType' has no attribute 'STRING'
>>> 

@pj598
Copy link
Author

pj598 commented May 5, 2021

Here's the output of pip freeze. Still showing that pandas-gbq==0.15.0 and google-cloud-bigquery==2.15.0

cachetools                        4.2.2
certifi                            2020.12.5
cffi                               1.14.5
chardet                            3.0.4
google-api-core                    1.25.1
google-auth                        1.30.0
google-auth-oauthlib               0.4.4
google-cloud-bigquery              2.15.0
google-cloud-bigquery-storage      2.4.0
google-cloud-core                  1.6.0
google-crc32c                      1.1.2
google-resumable-media             1.2.0
googleapis-common-protos           1.53.0
grpcio                             1.37.1
idna                               2.10
libcst                             0.3.18
mypy-extensions                    0.4.3
numpy                              1.20.1
oauthlib                           3.1.0
packaging                          20.9
pandas                             1.0.5
pandas-gbq                         0.15.0
proto-plus                         1.18.1
protobuf                           3.15.8
pyarrow                            3.0.0
pyasn1                             0.4.8
pyasn1-modules                     0.2.8
pycparser                          2.20
pycurl                             7.43.0.6
pydata-google-auth                 1.2.0
pyparsing                          2.4.7
python-dateutil                    2.8.1
pytz                               2021.1
PyYAML                             5.4.1
requests                           2.25.1
requests-oauthlib                  1.3.0
rsa                                4.7.2
six                                1.15.0
typing-extensions                  3.7.4.3
typing-inspect                     0.6.0
urllib3                            1.26.4

When I double checked the version at runtime:

pkg_resources.get_distribution("google-cloud-bigquery").version
'2.15.0'

I installed everything on Friday and then updated it on Monday morning (as part of a last ditch effort to troubleshoot)

@plamut
Copy link
Contributor

plamut commented May 5, 2021

The versions look fine, but what's the output if the following is placed at the very top of the application's entry point?

from google.cloud import bigquery
print(getattr(bigquery, "__version__", "older than 2.2.0")

The version reported by pkg_resources can differ from what's actually being used, we've had such cases in the past (example). For example, this is the output of my local IPython session:

In [1]: import pkg_resources

In [2]: from google.cloud import bigquery

In [3]: pkg_resources.get_distribution("google-cloud-bigquery").version
Out[3]: '1.28.0'

In [4]: print(getattr(bigquery, "__version__", "older than 2.2.0"))
2.15.0

Checking the __version__ attribute is more reliable, provided that it exists (if it doesn't, it means that the BigQuery version used pre-dates v2.2.0)

Edit:
You might also want to check where the bigquery module is actually imported from:

print(bigquery.__path__)

See if it's indeed the location you expect and if its code matches a 2.x release. 🙂

@pj598
Copy link
Author

pj598 commented May 5, 2021

Sorry bear with me, I'm new to code. When I tried using the code above, I got the same traceback error. I did just check anaconda navigator specifically though, and its showing that google-cloud-bigquery is version 1.22.0 (which I would assume means the issue is actually with updating the package in anaconda?).

@plamut
Copy link
Contributor

plamut commented May 5, 2021

@pj598 No worries, we at least confirmed the theory that a different version of the BigQuery client is used than expected.

Yes, it looks like the issue with anaconda installation. When the package is upgraded to the latest version there, the import error should disappear.

@pj598
Copy link
Author

pj598 commented May 5, 2021

Thank you for all of your help!

@plamut
Copy link
Contributor

plamut commented May 6, 2021

Since the solution has been found, I'm closing this issue, but feel free to re-open it, should you still experience problems.

@plamut plamut closed this as completed May 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the googleapis/python-bigquery API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

2 participants