简体   繁体   English

__init__.py 中的 google.cloud 命名空间导入错误

[英]google.cloud namespace import error in __init__.py

I have read through at least a dozen different stackoverflow questions that all present the same basic problem and have the same basic answer: either the module isn't installed correctly or the OP is doing the import wrong.我已经阅读了至少十几个不同的 stackoverflow 问题,它们都提出了相同的基本问题并具有相同的基本答案:模块未正确安装或 OP 导入错误。

In this case, I am trying to do from google.cloud import secretmanager_v1beta1 .在这种情况下,我试图from google.cloud import secretmanager_v1beta1做。

It works in my airflow container when I run airflow dags or if I run pytest tests/dags/test_my_dag.py .当我运行airflow dags pytest tests/dags/test_my_dag.py或运行pytest tests/dags/test_my_dag.py时,它在我的气流容器中工作。 However, if I run cd dags; python -m my_dag但是,如果我运行cd dags; python -m my_dag cd dags; python -m my_dag or cd dags; python my_dag.py cd dags; python -m my_dagcd dags; python my_dag.py cd dags; python -m my_dag cd dags; python my_dag.py cd dags; python my_dag.py I get this error: cd dags; python my_dag.py我收到这个错误:

from google.cloud import secretmanager as secretmanager
ImportError: cannot import name 'secretmanager' from 'google.cloud' (unknown location)

I can add from google.cloud import bigquery in the line right above this line and that works OK.我可以在此行正上方的行中添加from google.cloud import bigquery并且工作正常。 It appears to literally just be a problem with this particular package.从字面上看,这似乎只是这个特定包的问题。

Why does it matter if pytest and airflow commands succeed?为什么pytest和airflow命令成功很重要? Because, I have another environment where I am trying to run dataflow jobs from the command-line and I get this same error.因为,我有另一个环境,我试图从命令行运行数据流作业,但我遇到了同样的错误。 And unfortunately I don't think I can bypass this error in that environment for several reasons.不幸的是,出于多种原因,我认为我无法在该环境中绕过此错误。

UPDATE 6更新 6

I have narrowed down the error to an issue with the google.cloud namespace and the secretmanager package within that namespace in the __init__.py file.我已将错误范围缩小到__init__.py文件中google.cloud命名空间和该命名空间内的secretmanager包的问题。

If I add from google.cloud import secretmanager to airflow/dags/__init__.py and then try to run python -m dags.my_dag.py , I receive this error but with a slightly different stacktrace:如果我将from google.cloud import secretmanager添加from google.cloud import secretmanager airflow/dags/__init__.py ,然后尝试运行python -m dags.my_dag.py ,我会收到此错误,但python -m dags.my_dag.py略有不同:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/usr/local/lib/python3.7/runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "/workspace/airflow/dags/__init__.py", line 3, in <module>
    from google.cloud import secretmanager
ImportError: cannot import name 'secretmanager' from 'google.cloud' (unknown location)

OLD INFORMATION旧信息

I am 95% sure that it's still a path problem and that pytest and airflow are fixing something I'm not aware of that isn't handled when I try to manually run the python script.我 95% 确定它仍然是路径问题,并且 pytest 和气流正在修复一些我不知道的问题,当我尝试手动运行 python 脚本时没有处理这些问题。

Things I have tried:我尝试过的事情:

cd /airflow; python setup.py develop --user
cd /airflow; pip install -e . --user
cd /airflow/dags; pip install -r requirements.txt --user

UPDATE更新

As per requests in the comments, here are the contents of requirements.txt :根据评论中的requirements.txt以下是requirements.txt的内容:

boto3>=1.7.84
google-auth==1.11.2
google-cloud-bigtable==1.2.1
google-cloud-bigquery==1.24.0
google-cloud-spanner==1.14.0
google-cloud-storage==1.26.0
google-cloud-logging==1.14.0
google-cloud-secret-manager>=0.2.0
pycloudsqlproxy>=0.0.15
pyconfighelper>=0.0.7
pymysql==0.9.3
setuptools==45.2.0
six==1.14.0

And I accidentally omitted the --user flags from the pip and python installation command examples above.我不小心从上面的 pip 和 python 安装命令示例中省略了--user标志。 In my container environment everything is installed into the user's home directory using --user and NOT in the global site-packages directory.在我的容器环境中,所有内容都使用--user安装到用户的主目录中,而不是在全局site-packages目录中。

UPDATE 2更新 2

I've added the following code to the file that is generating the error:我已将以下代码添加到生成错误的文件中:

print('***********************************************************************************')
import sys
print(sys.path)
from google.cloud import secretmanager_v1beta1 as secretmanager
print('secretmanager.__file__: {}'.format(secretmanager.__file__))

From airflow list_dags :airflow list_dags

['/home/app/.local/bin', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/home/app/.local/lib/python3.7/site-packages', '/home/app/.local/lib/python3.7/site-packages/Jeeves-0.0.1-py3.7.egg', '/home/app/.local/lib/python3.7/site-packages/google_cloud_secret_manager-0.2.0-py3.7.egg', '/home/app/.local/lib/python3.7/site-packages/pyconfighelper-0.0.7-py3.7.egg', '/home/app/.local/lib/python3.7/site-packages/click-7.1.1-py3.7.egg', '/workspace/airflow', '/usr/local/lib/python3.7/site-packages', '/workspace/airflow/dags', '/workspace/airflow/config', '/workspace/airflow/plugins']
secretmanager.__file__: /home/app/.local/lib/python3.7/site-packages/google_cloud_secret_manager-0.2.0-py3.7.egg/google/cloud/secretmanager_v1beta1/__init__.py

From python my_dag.py :python my_dag.py

['/workspace/airflow/dags', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/home/app/.local/lib/python3.7/site-packages', '/home/app/.local/lib/python3.7/site-packages/Jeeves-0.0.1-py3.7.egg', '/home/app/.local/lib/python3.7/site-packages/google_cloud_secret_manager-0.2.0-py3.7.egg', '/home/app/.local/lib/python3.7/site-packages/pyconfighelper-0.0.7-py3.7.egg', '/home/app/.local/lib/python3.7/site-packages/click-7.1.1-py3.7.egg', '/home/app/.local/lib/python3.7/site-packages/icentris_ml_airflow-0.0.0-py3.7.egg', '/usr/local/lib/python3.7/site-packages']

UPDATE 3 tree airflow/dags更新 3 tree airflow/dags

airflow/dags
├── __init__.py
├── __pycache__
│   ├── __init__.cpython-37.pyc
│   ├── bq_to_cs.cpython-37.pyc
│   ├── bq_to_wrench.cpython-37.pyc
│   ├── fetch_cloudsql_tables-bluesun.cpython-37.pyc
│   ├── fetch_cloudsql_tables.cpython-37.pyc
│   ├── fetch_app_tables-bluesun.cpython-37.pyc
│   ├── fetch_app_tables.cpython-37.pyc
│   ├── gcs_to_cloudsql.cpython-37.pyc
│   ├── gcs_to_s3.cpython-37.pyc
│   ├── lake_to_staging.cpython-37.pyc
│   ├── schedule_dfs_sql_to_bq-bluesun.cpython-37.pyc
│   ├── schedule_dfs_sql_to_bq.cpython-37.pyc
│   ├── app_to_bq_initial_load-bluesun.cpython-37.pyc
│   ├── app_to_lake-bluesun.cpython-37.pyc
│   └── app_to_lake.cpython-37.pyc
├── bq_to_wrench.py
├── composer_variables.json
├── my_ml_airflow.egg-info
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   ├── dependency_links.txt
│   └── top_level.txt
├── lake_to_staging.py
├── libs
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   ├── checkpoint.cpython-37.pyc
│   │   └── utils.cpython-37.pyc
│   ├── checkpoint.py
│   ├── io
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   └── __init__.cpython-37.pyc
│   │   └── gcp
│   │       ├── __init__.py
│   │       ├── __pycache__
│   │       │   ├── __init__.cpython-37.pyc
│   │       │   └── storage.cpython-37.pyc
│   │       └── storage.py
│   ├── shared -> /workspace/shared/
│   └── utils.py
├── requirements.txt
├── table_lists
│   └── table-list.json
└── templates
    └── sql
        ├── lake_to_staging.contacts.sql
        ├── lake_to_staging.orders.sql
        └── lake_to_staging.users.sql

11 directories, 41 files

UPDATE 4更新 4

I tried fixing it so that sys.path looked the same when running python dags/my_dag.py as it does when running airflow list_dags or pytest test_my_dag.py .我尝试修复它,以便sys.path在运行python dags/my_dag.py时看起来与运行airflow list_dagspytest test_my_dag.py

Still get the same error.仍然得到同样的错误。

Looking at a more recent version of documentation, I noticed that you should be able to just do from google.cloud import secretmanager .查看更新版本的文档,我注意到您应该可以只执行from google.cloud import secretmanager Which gave me the same result (works with airflow and pytest, not when trying to run directly).这给了我相同的结果(适用于气流和 pytest,而不是尝试直接运行时)。

At this point, my best guess is that it has something to do with namespace magic, but I'm not sure?在这一点上,我最好的猜测是它与命名空间魔法有关,但我不确定?

它必须通过终端pip install google-cloud-secret-managerpip install google-cloud-secret-manager因为包名不是 secretmanager 而是 google-cloud-secret-manager

After much trial and error, the issue is that currently one cannot import secretmanager from the google.cloud namespace if one has not previously imported another package from google.cloud.经过多次反复试验,问题是如果之前没有从 google.cloud 导入另一个包,则当前无法从 google.cloud 命名空间导入 secretmanager。

Ex.前任。

mod.py模块

from google.cloud import secretmanager # Fails with error

mod2.py mod2.py

from google.cloud import bigquery
from google.cloud import secretmanager # Works because the first import initialized the namespace

与诺亚的回答类似,这为我解决了问题,而无需导入不需要的模块:

import google.cloud.secretmanager as secretmanager

I'm using Python 3.8.2 and google-cloud-secret-manager 2.7.1.我正在使用 Python 3.8.2 和google-cloud-secret-manager 2.7.1。 The following line fixed the issue for me:以下行为我解决了这个问题:

from google.cloud import secretmanager_v1beta1 as secretmanager

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM