I’m having a similar issue and I’m not sure how to implement this into a custom python test in the test_custom_runner.py
file.
When I go to include a library that I haven’t installed I get an error like this:
UserSideException: Could not find custom test runner by this path -> .../test/embedded/test_custom_runner.py
But when I prefix my python code with this
# ---------------------- PIO SCRIPTING ----------------------
# https://docs.platformio.org/en/stable/scripting/examples/extra_python_packages.html
# Do not format the PIO scripting code. It must be before python imports
# noqa: off
Import("env") # type: ignore
# List installed packages
env.Execute("$PYTHONEXE -m pip list") # type: ignore
# Install custom packages from the PyPi registry
env.Execute("$PYTHONEXE -m pip install pkg1 pkg2") # type: ignore
# Install missed package
try:
from saleae import automation
except ImportError:
env.Execute("$PYTHONEXE -m pip install saleae") # type: ignore
# noqa: on
# ---------------------- PIO SCRIPTING ----------------------
from saleae import automation
from platformio.public import TestCase, TestCaseSource, TestStatus, UnityTestRunner
import os
import os.path
from datetime import datetime
# Code here
I get this error
Verbosity level can be increased via `-v, -vv, or -vvv` option
Collected 2 tests
NameError: Traceback (most recent call last):
File "/Users/freddy/.platformio/penv/lib/python3.11/site-packages/platformio/__main__.py", line 103, in main
cli() # pylint: disable=no-value-for-parameter
^^^^^
File "/Users/freddy/.platformio/penv/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
return self.main(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/freddy/.platformio/penv/lib/python3.11/site-packages/click/core.py", line 1078, in main
rv = self.invoke(ctx)
^^^^^^^^^^^^^^^^
File "/Users/freddy/.platformio/penv/lib/python3.11/site-packages/platformio/cli.py", line 85, in invoke
return super().invoke(ctx)
^^^^^^^^^^^^^^^^^^^
File "/Users/freddy/.platformio/penv/lib/python3.11/site-packages/click/core.py", line 1688, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/freddy/.platformio/penv/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
return ctx.invoke(self.callback, **ctx.params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/freddy/.platformio/penv/lib/python3.11/site-packages/click/core.py", line 783, in invoke
return __callback(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/freddy/.platformio/penv/lib/python3.11/site-packages/click/decorators.py", line 33, in new_func
return f(get_current_context(), *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/freddy/.platformio/penv/lib/python3.11/site-packages/platformio/test/cli.py", line 137, in cli
runner = TestRunnerFactory.new(
^^^^^^^^^^^^^^^^^^^^^^
File "/Users/freddy/.platformio/penv/lib/python3.11/site-packages/platformio/test/runners/factory.py", line 58, in new
mod = load_python_module(module_name, custom_runner_path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/freddy/.platformio/penv/lib/python3.11/site-packages/platformio/compat.py", line 99, in load_python_module
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/Users/freddy/Library/Mobile Documents/com~apple~CloudDocs/Code/Arduino/ArduinoUnitPIO/test/embedded/test_custom_runner.py", line 5, in <module>
Import("env") # type: ignore
^^^^^^
NameError: name 'Import' is not defined
============================================================
An unexpected error occurred. Further steps:
* Verify that you have the latest version of PlatformIO using
`python -m pip install -U platformio` command
* Try to find answer in FAQ Troubleshooting section
https://docs.platformio.org/page/faq/index.html
* Report this problem to the developers
https://github.com/platformio/platformio-core/issues
============================================================
How do I interface with the virtual environment in this scenario?