PlatformIO Community

NameError: name 'Import' is not defined for pre/post hook

Hi,
I am trying to add a pre and post-hook to gzip & gunzip my text based files before & after file system upload.
This is my python file:

Import("env")

import os
import gzip
from pathlib import Path
data_folder = Path(Path.cwd(), "data")
print(data_folder)

def before_upload(source, target, env):
    os.system(f'for i in `find | grep -E "\.css$|\.html$|\.js$"`; do gzip "$i" ; done')

def after_upload(source, target, env):
    os.system(f"gunzip {data_folder}/*.gz")

env.AddPreAction("uploadfs", before_upload)
env.AddPostAction("uploadfs", after_upload)

This is my platformio file:

[env:esp32dev]

platform = espressif32
framework =
   arduino
   espidf
board = esp32dev
build_flags =
   -D ESP32
board_build.partitions = partition.csv
build_unflags =
   -Werror=all
   -fno-exceptions
monitor_speed = 115200
platform_packages = framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#idf-release/v4.0
lib_compat_mode = strict
lib_deps =
   AsyncTCP
   SHA-1 Hash
   RTC
   https://github.com/me-no-dev/ESPAsyncWebServer.git
   https://github.com/bblanchon/ArduinoJson#v6.15.2
extra_scripts = pre:gzip.py

[platformio]
description = The Final Module For ESP32 Timer Device

I am not sure why this error is occurring:

NameError: name ‘Import’ is not defined:
File “/home/maxwell/.platformio/penv/lib/python3.6/site-packages/platformio/builder/main.py”, line 166:
env.SConscript(item, exports=“env”)
File “/home/maxwell/.platformio/packages/tool-scons/script/…/engine/SCons/Script/SConscript.py”, line 597:
return _SConscript(self.fs, *files, **subst_kw)
File “/home/maxwell/.platformio/packages/tool-scons/script/…/engine/SCons/Script/SConscript.py”, line 286:
exec(compile(scriptdata, scriptname, ‘exec’), call_stack[-1].globals)
File “/home/maxwell/Documents/PlatformIO/Projects/200630-225357-espidf-arduino-wifiscan/gzip.py”, line 4:
import gzip
File “/home/maxwell/Documents/PlatformIO/Projects/200630-225357-espidf-arduino-wifiscan/gzip.py”, line 1:
Import(“env”)

It seems to be using your version of python but still throwing that error.

1 Like

You called your script gzip.py, and in gzip.py you do a import gzip? Isn’t that recursive o_o

2 Likes

Darn it. This is embarrassing :expressionless:

1 Like