I have python code in the "project" folder, so my code files are in the / * project. py. I want to have submodules inside it, for example
project/code.py
project/mymodule1
project/mymodule2
each module directory has its own init .py file, for example
project/mymodule1/__init__.py
Suppose I have a test.py file in mymodule1 (project / mymodule1 / test.py), and I would like to refer to something from the "code", for example. import the function "myfunc"
== project/mymodule1/test.py ==
from code import myfunc
the problem is that the "code" will not be found unless the user has placed the "project /" directory in PYTHONPATH. Is there a way to avoid this and use some kind of “relative path” to import myfunc, for example.
from ../code import myfunc
, PYTHONPATH, script. , .
? : PYTHONPATH , , "" - , , , "project/code.py" , , "myfunc".
EDIT: - ? , "mymodule1" :
from .. import foo
"foo" code.py, . init.py mymodule1, :
project/code.py
project/mymodule1/__init__.py
project/mymodule1/module1_code.py
module1_code.py foo, , "code.py".
EDIT: , , , , /sub 1/test, "cd" sub1 "python test.py", . , "", "import project.sub1.test". , , . test.py, /sub 1/. , :
$ cd project/sub1
$ python test.py
:
ValueError: Attempted relative import in non-package
?
.