簡體   English   中英

在 WSL 上安裝和運行 Airflow

[英]Installing and running Airflow on WSL

我按照本文中的過程在 WSL(Windows 10)下的 Ubuntu 中安裝了 Airflow。

但是在運行airflow db init時,出現以下錯誤:

AttributeError: module 'wtforms.fields' has no attribute 'TextField'

完整的跟蹤:

(airflow_env) sultani@Khalid:~/c/users/administrator/airflowhome$ airflow db init
DB: sqlite:///c/users/administrator/airflowhome/airflow.db
[2022-06-09 00:41:54,760] {db.py:1448} INFO - Creating tables
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
Traceback (most recent call last):
  File "/home/sultani/.local/bin/airflow", line 8, in <module>
    sys.exit(main())
  File "/home/sultani/.local/lib/python3.8/site-packages/airflow/__main__.py", line 38, in main
    args.func(args)
  File "/home/sultani/.local/lib/python3.8/site-packages/airflow/cli/cli_parser.py", line 51, in command
    return func(*args, **kwargs)
  File "/home/sultani/.local/lib/python3.8/site-packages/airflow/cli/commands/db_command.py", line 35, in initdb
    db.initdb()
  File "/home/sultani/.local/lib/python3.8/site-packages/airflow/utils/session.py", line 71, in wrapper
    return func(*args, session=session, **kwargs)
  File "/home/sultani/.local/lib/python3.8/site-packages/airflow/utils/db.py", line 648, in initdb
    upgradedb(session=session)
  File "/home/sultani/.local/lib/python3.8/site-packages/airflow/utils/session.py", line 68, in wrapper
    return func(*args, **kwargs)
  File "/home/sultani/.local/lib/python3.8/site-packages/airflow/utils/db.py", line 1449, in upgradedb
    command.upgrade(config, revision=to_revision or 'heads')
  File "/home/sultani/.local/lib/python3.8/site-packages/alembic/command.py", line 322, in upgrade
    script.run_env()
  File "/home/sultani/.local/lib/python3.8/site-packages/alembic/script/base.py", line 569, in run_env
    util.load_python_file(self.dir, "env.py")
  File "/home/sultani/.local/lib/python3.8/site-packages/alembic/util/pyfiles.py", line 94, in load_python_file
    module = load_module_py(module_id, path)
  File "/home/sultani/.local/lib/python3.8/site-packages/alembic/util/pyfiles.py", line 110, in load_module_py
    spec.loader.exec_module(module)  # type: ignore
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/sultani/.local/lib/python3.8/site-packages/airflow/migrations/env.py", line 107, in <module>
    run_migrations_online()
  File "/home/sultani/.local/lib/python3.8/site-packages/airflow/migrations/env.py", line 101, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "/home/sultani/.local/lib/python3.8/site-packages/alembic/runtime/environment.py", line 853, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/home/sultani/.local/lib/python3.8/site-packages/alembic/runtime/migration.py", line 611, in run_migrations
    for step in self._migrations_fn(heads, self):
  File "/home/sultani/.local/lib/python3.8/site-packages/alembic/command.py", line 311, in upgrade
    return script._upgrade_revs(revision, rev)
  File "/home/sultani/.local/lib/python3.8/site-packages/alembic/script/base.py", line 443, in _upgrade_revs
    for script in reversed(list(revs))
  File "/home/sultani/.local/lib/python3.8/site-packages/alembic/script/revision.py", line 799, in iterate_revisions
    revisions, heads = fn(
  File "/home/sultani/.local/lib/python3.8/site-packages/alembic/script/revision.py", line 1426, in _collect_upgrade_revisions
    for rev in self._parse_upgrade_target(
  File "/home/sultani/.local/lib/python3.8/site-packages/alembic/script/revision.py", line 1218, in _parse_upgrade_target
    return self.get_revisions(target)
  File "/home/sultani/.local/lib/python3.8/site-packages/alembic/script/revision.py", line 529, in get_revisions
    resolved_id, branch_label = self._resolve_revision_number(
  File "/home/sultani/.local/lib/python3.8/site-packages/alembic/script/revision.py", line 753, in _resolve_revision_number
    self._revision_map
  File "/home/sultani/.local/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 1095, in __get__
    obj.__dict__[self.__name__] = result = self.fget(obj)
  File "/home/sultani/.local/lib/python3.8/site-packages/alembic/script/revision.py", line 191, in _revision_map
    for revision in self._generator():
  File "/home/sultani/.local/lib/python3.8/site-packages/alembic/script/base.py", line 140, in _load_revisions
    script = Script._from_filename(self, vers, file_)
  File "/home/sultani/.local/lib/python3.8/site-packages/alembic/script/base.py", line 1013, in _from_filename
    module = util.load_python_file(dir_, filename)
  File "/home/sultani/.local/lib/python3.8/site-packages/alembic/util/pyfiles.py", line 94, in load_python_file
    module = load_module_py(module_id, path)
  File "/home/sultani/.local/lib/python3.8/site-packages/alembic/util/pyfiles.py", line 110, in load_module_py
    spec.loader.exec_module(module)  # type: ignore
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/sultani/.local/lib/python3.8/site-packages/airflow/migrations/versions/0073_2_0_0_prefix_dag_permissions.py", line 27, in <module>
    from flask_appbuilder import SQLA
  File "/home/sultani/.local/lib/python3.8/site-packages/flask_appbuilder/__init__.py", line 6, in <module>
    from .base import AppBuilder  # noqa: F401
  File "/home/sultani/.local/lib/python3.8/site-packages/flask_appbuilder/base.py", line 8, in <module>
    from .api.manager import OpenApiManager
  File "/home/sultani/.local/lib/python3.8/site-packages/flask_appbuilder/api/manager.py", line 8, in <module>
    from flask_appbuilder.baseviews import BaseView
  File "/home/sultani/.local/lib/python3.8/site-packages/flask_appbuilder/baseviews.py", line 21, in <module>
    from .forms import GeneralModelConverter
  File "/home/sultani/.local/lib/python3.8/site-packages/flask_appbuilder/forms.py", line 25, in <module>
    from .models.mongoengine.fields import MongoFileField, MongoImageField
  File "/home/sultani/.local/lib/python3.8/site-packages/flask_appbuilder/models/mongoengine/fields.py", line 4, in <module>
    from ...upload import BS3FileUploadFieldWidget, BS3ImageUploadFieldWidget
  File "/home/sultani/.local/lib/python3.8/site-packages/flask_appbuilder/upload.py", line 106, in <module>
    class FileUploadField(fields.TextField):
AttributeError: module 'wtforms.fields' has no attribute 'TextField'

不幸的是,您鏈接的博客文章存在很多問題:

  • 它至少有兩個錯誤。 首先, AIRFLOW_HOME變量前面缺少/ 此外,該博客建議將您的/mnt/c更改為簡單的/c ,然后繼續使用AIRFLOW_HOME中的/mnt/c ,這是行不通的。

  • 壞建議——一般來說,您不應該將 Windows 驅動器(例如/mnt/c )用於 WSL2 項目。 有性能問題和權限問題需要處理。

  • 不必要的步驟 - 即使您使用將 Airflow 安裝到 Windows 驅動器的錯誤建議,絕對沒有理由將 WSL 自動掛載目錄更改為/ 我建議編輯您的 WSL 配置文件以更改此設置,除非您出於其他原因需要它。 /etc/wsl.conf中刪除root = /行。

  • 不必要的軟件包和存儲庫——沒有理由添加 Universe,因為它已經存在(並且一直存在)在 WSL Ubuntu 中。 也沒有理由安裝software-properties-commonpython-setuptools —— 它們不是必需的。

  • 最糟糕的是,它只是過時和/或與官方文檔沖突。 這是您安裝的最大問題——在實際安裝過程中您會看到一個錯誤,即存在不兼容的軟件包版本。

因此,讓我推薦以下組合:

  • Python 最佳實踐——該博客在某種程度上確實做到了這一點,使用虛擬環境來避免污染通用 Python 系統包。
  • WSL2 最佳實踐——你真的可以按照上面鏈接的官方文檔進行操作。 博客文章只是一個糟糕的方法。
  • 官方文檔。

如果您沒有將它用於其他任何事情,您可能只想刪除您的 Ubuntu WSL2 安裝並重新開始。 我從一個干凈的 Ubuntu 20.04 WSL2 安裝開始來演示這一點:

sudo apt update && sudo apt upgrade -y
sudo apt install python3-pip python3-venv
mkdir -p ~/airflow/airflowhome
python3 -m venv ~/airflow/airflowenv
source ~/airflow/airflowenv/bin/activate
# Your prompt will change to "airflowenv"
pip3 install wheel # Not found in venv by default, but recommended for packages

在這一點上,我們直接從Apache Airflow 文檔開始工作,除了輕微的目錄更改之外沒有任何實質性的變化:

export AIRFLOW_HOME=~/airflow/airflowhome
AIRFLOW_VERSION=2.3.2
PYTHON_VERSION="$(python --version | cut -d " " -f 2 | cut -d "." -f 1-2)"
CONSTRAINT_URL="https://raw.githubusercontent.com/apache/airflow/constraints-${AIRFLOW_VERSION}/constraints-${PYTHON_VERSION}.txt"
pip install "apache-airflow==${AIRFLOW_VERSION}" --constraint "${CONSTRAINT_URL}"

仔細觀察輸出。 您應該在那里看不到任何錯誤——之前使用博客說明的嘗試顯示了紅色文本錯誤,提前表明了問題。

現在:

airflow standalone

這將啟動服務器。 正如醫生所說,密切注意屏幕上的最后幾行。 它將列出您的管理員用戶名和密碼。 您可以使用它通過 Windows 網絡瀏覽器在http://localhost:8080登錄。

您仍然可以將export AIRFLOW_HOME語句添加到您的~/.bashrc中,但是由於您每次都需要激活 venv,可能最好只是這樣做。 所以下次運行它,你會:

source ~/airflow/airflowenv/bin/activate
export AIRFLOW_HOME=~/airflow/airflowhome
airflow standalone

注意:無論何時執行source ~/airflow/airflowenv/bin/activate ,您都將處於 Python 虛擬環境中。 要在此時返回默認的 Python 系統環境,只需運行deactivate即可。

我自己不是 Airflow 用戶,因此我無法為您提供更多幫助。 但這些是至少在 WSL2 Ubuntu 上對我有用的步驟,以使其啟動並運行。

感謝您的回復,提供的博客中有幾個問題,我設法在本地運行氣流。

  1. / 在 C 之前丟失,路徑必須更改為 AIRFLOW_HOME=/c/users/YOURNAME/airflowhome 。
  2. 我必須從https://pypi.org/project/apache-airflow/安裝氣流,其余步驟都很好。

因為我是氣流新手,所以我現在需要在 pycharm 中修復一些其他問題,當我在 dag 文件夾中創建 dag 文件時,它會丟失依賴項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM