我在github中有我的databricks python代码。我设置了一个使用flake8链接python代码的基本工作流。这是因为我的脚本(如spark、sc、dbutils、getArgument等)在databricks上运行时隐式可用的名称在databricks上运行时是不可用的,当flake8在databricks之外链接它时(在github中)。
如何在github中使用flake8将数据库链接到笔记本
例如,我得到的错误:
test.py:1:1: F821 undefined name 'dbutils'
test.py:3:11: F821 undefined name 'getArgument'
test.py:5:1: F821 undefined name 'dbutils'
test.py:7:11: F821 undefined name 'spark'我的笔记本电脑在github:
dbutils.widgets.text("my_jdbcurl", "default my_jdbcurl")
jdbcurl = getArgument("my_jdbcurl")
dbutils.fs.ls(".")
df_node = spark.read.format("jdbc")\
.option("driver", "org.mariadb.jdbc.Driver")\
.option("url", jdbcurl)\
.option("dbtable", "my_table")\
.option("user", "my_username")\
.option("password", "my_pswd")\
.load()工作流程/lint.yml
on:
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v1
with:
python-version: 3.8
- run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Lint with flake8
run: |
pip install flake8
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics发布于 2021-08-10 09:43:19
你能做的一件事是:
from pyspark.sql import Spark Session
spark = SparkSession.builder.getOrCreate()这将使用或不使用Databricks,在普通Python中或在pyspark客户机中。
要检测您是在文件中还是在Databricks笔记本中,可以运行:
try:
__file__
print("We are in a file, like in our IDE or being tested by flake8.")
except NameError:
print("We are in a Databricks notebook. Act accordingly.")然后,您可以有条件地初始化或为display()和其他工具创建虚拟变量。
这只是一个部分的解决办法。我正在研究一个更好的解决方案,我会不断更新这个答案。
发布于 2021-08-10 22:09:20
这是我的观点,并非所有的指针都适用于所有用例,这就是我所做的。我使用了预提交钩子,忽略了规则F821。
# Flake rules: https://lintlyci.github.io/Flake8Rules/
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.4
hooks:
- id: flake8
exclude: (^docs/)
additional_dependencies: [flake8-typing-imports==1.7.0]
# F821 undefined name
args:
[
"--max-line-length=127",
"--config=setup.cfg",
"--ignore=F821",
]要匹配您的语法,添加--ignore标志:
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --ignore=C901,F821 --statisticshttps://stackoverflow.com/questions/61019498
复制相似问题