Your project is as follows:
+- proj | +- test.py +- UTILS.py +- ...
If you want to import UTILS.py, you can choose:
(1) add the path to sys.path in test.py
import os, sys sys.path.append(os.path.join(os.path.dirname(__file__), "..")) # now you may get a problem with what I wrote below. import UTILS
(2) create package (import only)
Python +- proj | +- test.py | +- __init__.py +- UTILS.py +- __init__.py +- ...
Now you can write this in test.py if you import Python.proj.test :
from .. import UTILS
INCORRECT ANSWER
I have had this error several times. I think I remember.
Fix: do not run test.py , run ./test.py .
If you look at sys.path , you will see that there is an empty line inside which is the path to the file.
test.py adds '' to sys.path./test.py adds '.' in sys.path
Import can only be done from "." , I think.
User
source share