Closed leogout closed 3 weeks ago
The relative import syntax is for modules that are within a package, but the main Python code does not reside "in a package". You get something similar when you try to perform the same action on standard desktop Python (3.11 on my system), though the message is different:
$ echo 'from .test import test' > issue9774.py
$ python issue9774.py
Traceback (most recent call last):
File "/home/jepler/issue9774.py", line 1, in <module>
from .test import test
ImportError: attempted relative import with no known parent package
You may mean to write from test import test
instead
I should mention that I need relative imports in the root folder for the following reason (hope I'll avoid the XY problem) :
code.py
I can't put it in the root folder of my project or in my PYTHONPATH, because it would shadow the code
module required by pytest and otherssrc.mymoduleA.mymoduleB
Oh okay thanks @jepler, I did not know that. I'll try to find another solution !
At least the following 4 names are always supported for the primary code file:
"code.txt", "code.py", "main.py", "main.txt"
so maybe one of the .txt files would help with what you're trying to do.
Well thanks @jepler, you just made my day
At least the following 4 names are always supported for the primary code file:
"code.txt", "code.py", "main.py", "main.txt"
so maybe one of the .txt files would help with what you're trying to do.
I use main.py exactly for that reason. It allows me to run unittests both with CPython on the desktop computer, and in Circuitpython. No complaints about shadowing.
CircuitPython version
Code/REPL
Behavior
Which translates to :
Description
I have the following files structure : In the test folder : And here is the source code for the
main.py
and thetest.py
:Additional information
The hardware connection is done through USB on Windows 10, the output logs are on COM4, reading them with putty.
Further investigation reveals that relative imports from non root folders works : If I remove the relative import in the
code.py
and use one in thetest/test.py
, it works as expected :Output :