Python subpackage import from neighboring subpackage
Question:
Consider the following file structure hierarchy:
MusicEditor (package)
Converters (subpackage)
...
Transformations (subpackage)
...
Utility (subpackage)
...
What is the proper way to import a file/module from the utility subpackage into a file in the converters subpackage? e.g. import Utility/string.py into the file Converters/wav.py
How would I go about doing this?
EDIT in response to Ismael Infante
Would the following be correct?
Suppose that MusicEditor resided in /usr/something/MusicEditor. Then I would edit python path as follows?
PYTHONPATH = PYTHONPATH + /usr/something/
I would then add the init files to each directory as follows:
MusicEditor (package)
__init__.py
Converters (subpackage)
__init__.py
...
Transformations (subpackage)
__init__.py
...
Utility (subpackage)
__init__.py
...
And then to include something from utilities into a file located in converters, I would simply go:
(MusicEditor/Converters/wav.py)
import MusicEditor.Utility.string
Is this correct? But I could also use the following relative path, right?
(MusicEditor/Converters/wav.py)
import ..Utility.string
Answers:
You have to put an __init__.py
empty file inside of each directory. In this ways, your directories will become packages.
After that, you have to set up you PYTHONPATH variable. This variable must point to the parent directory of MusicEditor. It’s through this variable that the Python interpreter can reach your packages.
Consider the following file structure hierarchy:
MusicEditor (package)
Converters (subpackage)
...
Transformations (subpackage)
...
Utility (subpackage)
...
What is the proper way to import a file/module from the utility subpackage into a file in the converters subpackage? e.g. import Utility/string.py into the file Converters/wav.py
How would I go about doing this?
EDIT in response to Ismael Infante
Would the following be correct?
Suppose that MusicEditor resided in /usr/something/MusicEditor. Then I would edit python path as follows?
PYTHONPATH = PYTHONPATH + /usr/something/
I would then add the init files to each directory as follows:
MusicEditor (package)
__init__.py
Converters (subpackage)
__init__.py
...
Transformations (subpackage)
__init__.py
...
Utility (subpackage)
__init__.py
...
And then to include something from utilities into a file located in converters, I would simply go:
(MusicEditor/Converters/wav.py)
import MusicEditor.Utility.string
Is this correct? But I could also use the following relative path, right?
(MusicEditor/Converters/wav.py)
import ..Utility.string
You have to put an __init__.py
empty file inside of each directory. In this ways, your directories will become packages.
After that, you have to set up you PYTHONPATH variable. This variable must point to the parent directory of MusicEditor. It’s through this variable that the Python interpreter can reach your packages.