NameError: name ‘pd’ is defined 当加载一个pickle文件时 – 但pandas已经定义

huangapple go评论62阅读模式
英文:

NameError: name 'pd' is not defined when loading a pickled file - but pandas IS defined

问题

I'm sorry, but I can't provide a translation for this code because it contains specific technical details and code snippets that require context and understanding of the code's purpose. If you have any specific questions or need assistance with a particular part of the code, please feel free to ask in English, and I'll do my best to help you.

英文:

I'm trying to model a simple pipeline using Apache Airflow. I'm running an instance locally using docker. Some of the stuff that needs to be done is loading a pickled sklearn model and transforming a pandas dataframe. When i load that model and i try to use it i get the most simple error.

NameError: name 'pd' is not defined

So, first thing i do is go to the top to import pandas... but pandas is there.

I transcribe here my script and my environment relevant files.

A simplified version of my airflow task:

import dill
import pandas as pd

model_file = 'models/the_model.pkl'


def task_run_model(**context):

    # Load the pre-trained models from the .pkl files
    with open(model_file, 'rb') as f:
        model = dill.load(f)

    # Test models
    file_name = "train_set.csv"
    time_series_df = pd.read_csv(file_name)
    train_features_df = model.transform(time_series_df)

    return train_features_df

The error with the stack trace:

[2023-05-14, 19:21:10 UTC] {taskinstance.py:1847} ERROR - Task failed with exception
Traceback (most recent call last):
  File "/home/airflow/.local/lib/python3.8/site-packages/airflow/operators/python.py", line 181, in execute
    return_value = self.execute_callable()
  File "/home/airflow/.local/lib/python3.8/site-packages/airflow/operators/python.py", line 198, in execute_callable
    return self.python_callable(*self.op_args, **self.op_kwargs)
  File "/opt/airflow/dags/my_tasks/transformation.py", line 15, in task_run_model
    train_features_df = model.transform(time_series_df)
  File "/Users/<USER_NAME>/Repos/algorithms/Projects/xxxxxxxxx/model.py", line 19, in transform
NameError: name 'pd' is not defined
[2023-05-14, 19:21:10 UTC] {taskinstance.py:1368} INFO - Marking task as FAILED. dag_id=feature_creation, task_id=create_features, execution_date=20230514T192058, start_date=20230514T192110, end_date=20230514T192110
[2023-05-14, 19:21:10 UTC] {standard_task_runner.py:104} ERROR - Failed to execute job 4 for task create_features (name 'pd' is not defined; 248)
[2023-05-14, 19:21:10 UTC] {local_task_job_runner.py:232} INFO - Task exited with return code 1
[2023-05-14, 19:21:10 UTC] {taskinstance.py:2674} INFO - 0 downstream tasks scheduled from follow-on schedule check

of course i don't have access to /Users/<USER_NAME>/Repos/algorithms/Projects/xxxxxxxxx/model.py, i just have the .pkl file that was given to me

The only data that i have about that environment is that it was built with this dependencies:

pandas           : 1.3.5
numpy            : 1.21.6
dateutil         : 2.8.2
scipy            : 1.10.1

This is my environment:

docker-compose.yml

---
version: &#39;3.4&#39;

x-common:
  &amp;common
  build:
    context: .
    dockerfile: Dockerfile
  user: &quot;${AIRFLOW_UID}:0&quot;
  env_file: 
    - .env
  volumes:
    - ./dags:/opt/airflow/dags
    - ./logs:/opt/airflow/logs
    - ./plugins:/opt/airflow/plugins
    - ./models:/opt/airflow/models
    - ./tests:/opt/airflow/tests
    - /var/run/docker.sock:/var/run/docker.sock

x-depends-on:
  &amp;depends-on
  depends_on:
    postgres:
      condition: service_healthy
    airflow-init:
      condition: service_completed_successfully

services:
  postgres:
    image: postgres:13
    container_name: postgres
    ports:
      - &quot;5434:5432&quot;
    healthcheck:
      test: [&quot;CMD&quot;, &quot;pg_isready&quot;, &quot;-U&quot;, &quot;airflow&quot;]
      interval: 5s
      retries: 5
    env_file:
      - .env

  scheduler:
    &lt;&lt;: *common
    &lt;&lt;: *depends-on
    container_name: pipeline-scheduler
    command: scheduler
    restart: on-failure
    ports:
      - &quot;8793:8793&quot;

  webserver:
    &lt;&lt;: *common
    &lt;&lt;: *depends-on
    container_name: pipeline-webserver
    restart: always
    command: webserver
    ports:
      - &quot;8080:8080&quot;
    healthcheck:
      test: [&quot;CMD&quot;, &quot;curl&quot;, &quot;--fail&quot;, &quot;http://localhost:8080/health&quot;]
      interval: 30s
      timeout: 30s
      retries: 5
  
  airflow-init:
    &lt;&lt;: *common
    container_name: pipeline-init
    entrypoint: /bin/bash
    command:
      - -c
      - |
        mkdir -p /sources/logs /sources/dags /sources/plugins /sources/models
        chown -R &quot;${AIRFLOW_UID}:0&quot; /sources/{logs,dags,plugins,models}
        exec /entrypoint airflow version

Dockerfile

FROM apache/airflow:latest-python3.8
USER root
RUN apt-get update &amp;&amp; \
    apt-get clean &amp;&amp; \
    apt-get install vim-tiny -y &amp;&amp; \
    apt-get autoremove -yqq --purge &amp;&amp; \
    apt-get clean &amp;&amp; \
    rm -rf /var/lib/apt/lists/*
USER airflow
ENV PYTHONPATH &quot;${PYTHONPATH}:${AIRFLOW_HOME}&quot;
COPY requirements.txt /tmp/requirements.txt
RUN pip install -r /tmp/requirements.txt

requirements.yml

pip==22.3.1
scikit-learn==1.1.3
numpy==1.21.6
scipy==1.10.1
pandas==1.3.5
dill==0.3.6
python-dateutil==2.8.2

.env

# Meta-Database
POSTGRES_USER=airflow
POSTGRES_PASSWORD=airflow
POSTGRES_DB=airflow

# Airflow Core
AIRFLOW__CORE__FERNET_KEY=UKMzEm3yIuFYEq1y3-2FxPNWSVwRASpahmQ9kQfEr8E=
AIRFLOW__CORE__EXECUTOR=LocalExecutor
AIRFLOW__CORE__DAGS_ARE_PAUSED_AT_CREATION=True
AIRFLOW__CORE__LOAD_EXAMPLES=False
AIRFLOW_UID=0

# Backend DB
AIRFLOW__DATABASE__SQL_ALCHEMY_CONN=postgresql+psycopg2://airflow:airflow@postgres/airflow
AIRFLOW__DATABASE__LOAD_DEFAULT_CONNECTIONS=False

# Airflow Init
_AIRFLOW_DB_UPGRADE=True
_AIRFLOW_WWW_USER_CREATE=True
_AIRFLOW_WWW_USER_USERNAME=airflow
_AIRFLOW_WWW_USER_PASSWORD=airflow

To build this I'm using

docker compose up -d

The pickle DOES run locally if i run it in PyCharm

import pandas as pd
import dill

model_file = &#39;models/the_model.pkl&#39;

train_dataset_file = &#39;datasets/train.csv&#39;
test_dataset_file = &#39;datasets/test.csv&#39;

# Load the pre-trained model from the .pkl files
with open(model_file, &#39;rb&#39;) as f:
    model = dill.load(f)

# Load the datasets
train_df = pd.read_csv(train_dataset_file)
test_df = pd.read_csv(test_dataset_file)

# Test model
train_features_df: pd.DataFrame = model.transform(train_df)
test_features_df: pd.DataFrame = model.transform(test_df)


print(train_features_df, test_features_df)

Locally i'm using python 3.8, pandas==1.5.3 & dill==0.3.6
(yes, the first thing i tried was upgrading pandas to 1.5.3 in the requirements.txt, but same same.

答案1

得分: 1

这段代码的中文翻译如下:

import pandas as pd
import __main__
__main__.pd = pd

with open('pandize.pkl', 'rb') as f:
    p = dill.load(f)

p(1)
英文:

https://stackoverflow.com/a/65318623/10972050 <- answer

Basically

import pandas as pd
import __main__
__main__.pd = pd

with open(&#39;pandize.pkl&#39;, &#39;rb&#39;) as f:
    p = dill.load(f)

p(1)

huangapple
  • 本文由 发表于 2023年5月15日 05:01:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定