"Import" is not defined Pylance for extra_scripts

Dear All,
I got “Import is not defined” message from Pylance in my extra_scripts for a C++ project. Below is the content of platformio.ini and script.py: I could not figure out what was happening and how to fix it. Help is appreciated.

[platformio]
default_envs = 
	debug

[env]
extra_scripts = script.py

[env:debug]
build_flags = 
	${env.build_flags}
	-D DEBUG

The content of the script.py is as follows:

Import("env")

# General options that are passed to the C and C++ compilers
env.Append(CCFLAGS=[])

# General options that are passed to the C compiler (C only; not C++).
env.Append(CFLAGS=[])

# General options that are passed to the C++ compiler
env.Append(CXXFLAGS=["-Wno-deprecated-declarations",
                     "-Wno-register",
                     "-Wno-pmf-conversions"])

The Pylance is correct. It has no idea about SCons construction library. Please ignore this warning.

1 Like

I thought the problem had been solved by ignoring the warning. However, the CXXFLAGS I specify in the script.py does not work. The compile emits a lot of warnings that should have been disabled in the “script.py.” Any idea? Below is part of the warning message.

/home/ev/.platformio/packages/framework-arduinoststm32/system/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_rtc.h:1293:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
 1293 |   register uint32_t temp = 0U;

Since this is in the framework package, you gotta add those options to the global construction environment, not just env.

See documentation.

I modified the script as below, but it does nothing to the warning messages…

from SCons.Script import (DefaultEnvironment)

env = DefaultEnvironment()

# General options that are passed to the C and C++ compilers
env.Append(CCFLAGS=[])

# General options that are passed to the C compiler (C only; not C++).
env.Append(CFLAGS=[])

# General options that are passed to the C++ compiler
env.Append(CXXFLAGS=["-Wno-deprecated-declarations",
                     "-Wno-register",
                     "-Wno-pmf-conversions"])

The problem has been fixed by adding “pre:” in front of extra_scripts of the platformio.ini file as below:

[env]
extra_scripts = pre:script.py

You can also get pylance to ignore it by typing a simple comment after the Import, like this:

Import('env') # type: ignore