`extraScript` import error: ModuleNotFoundError

I’m trying to use import yaml within an extraScript.py script.

But it isn’t installed and therefore results in “ModuleNotFoundError: No module named ‘yaml’”.

Is there a way to trigger pip install pyyaml within the build framework?

Thanks!

  • Frank

I would have thought that you could just install it inside the platformio venv… as that’s where it’s needed… If you’re running an IDE lets you open a PlatformIO terminal, do the pip install pyyaml from there. Otherwise, load the virtualenv, and then install your package.

That’s how I’d do it anyway :wink:

Thanks, @pfeerick. How would I go about doing that? I’m in VS Code. Open a terminal, run pip install pyyaml - it says the package is already installed. Then I run pio run and I get

ModuleNotFoundError: No module named 'pyyaml':
File "/Users/frank/.platformio/penv/lib/python3.6/site-packages/platformio/builder/main.py", line 126:
env.SConscript("$BUILD_SCRIPT")
File "/Users/frank/.platformio/packages/tool-scons/script/../engine/SCons/Script/SConscript.py", line 605:
return _SConscript(self.fs, *files, **subst_kw)
File "/Users/frank/.platformio/packages/tool-scons/script/../engine/SCons/Script/SConscript.py", line 286:
exec(compile(scriptdata, scriptname, 'exec'), call_stack[-1].globals)
File "/Users/frank/.platformio/platforms/atmelsam/builder/main.py", line 120:
target_elf = env.BuildProgram()
File "/Users/frank/.platformio/packages/tool-scons/script/../engine/SCons/Environment.py", line 224:
return self.method(*nargs, **kwargs)
File "/Users/frank/.platformio/penv/lib/python3.6/site-packages/platformio/builder/tools/platformio.py", line 122:
_build_project_deps(env)
File "/Users/frank/.platformio/penv/lib/python3.6/site-packages/platformio/builder/tools/platformio.py", line 47:
project_lib_builder = env.ConfigureProjectLibBuilder()
File "/Users/frank/.platformio/packages/tool-scons/script/../engine/SCons/Environment.py", line 224:
return self.method(*nargs, **kwargs)
File "/Users/frank/.platformio/penv/lib/python3.6/site-packages/platformio/builder/tools/piolib.py", line 1043:
project.install_dependencies()
File "/Users/frank/.platformio/penv/lib/python3.6/site-packages/platformio/builder/tools/piolib.py", line 861:
if _is_builtin(uri):
File "/Users/frank/.platformio/penv/lib/python3.6/site-packages/platformio/builder/tools/piolib.py", line 853:
for lb in self.env.GetLibBuilders():
File "/Users/frank/.platformio/packages/tool-scons/script/../engine/SCons/Environment.py", line 224:
return self.method(*nargs, **kwargs)
File "/Users/frank/.platformio/penv/lib/python3.6/site-packages/platformio/builder/tools/piolib.py", line 973:
lb = LibBuilderFactory.new(env, lib_dir)
File "/Users/frank/.platformio/penv/lib/python3.6/site-packages/platformio/builder/tools/piolib.py", line 59:
verbose=verbose)
File "/Users/frank/.platformio/penv/lib/python3.6/site-packages/platformio/builder/tools/piolib.py", line 130:
self.process_extra_options()
File "/Users/frank/.platformio/penv/lib/python3.6/site-packages/platformio/builder/tools/piolib.py", line 269:
"pio_lib_builder": self
File "/Users/frank/.platformio/packages/tool-scons/script/../engine/SCons/Script/SConscript.py", line 605:
return _SConscript(self.fs, *files, **subst_kw)
File "/Users/frank/.platformio/packages/tool-scons/script/../engine/SCons/Script/SConscript.py", line 286:
exec(compile(scriptdata, scriptname, 'exec'), call_stack[-1].globals)
File "/Users/frank/Futurose/Arduino/FreeRTOS/lib/AtmelStartASF4/library.py", line 3:
import pyyaml

That makes me think I need to activate the venv with something like python -m venv /Users/frank/.platformio/penv, but after doing that and install and run, I get the same module not found. There’s no activate in the .platformio directory. How do I go about activating the virtualenv that PlatformIO uses?

I botched all my environment experiments by changing the source to import pyyaml instead of import yaml. Restored that and now it’s working, but not clear which attempt was the right one.

I’d still be interested in a standard way to install build script dependencies in a library - I’m not looking forward to having to document “When you use this library you’ll have to install python modules XYZ”.

Yes, just call

try:
    import yaml
except ImportError:
    env.Execute("$PYTHONEXE -m pip install pyyaml")
    import yaml
1 Like