--Python cannot ** import from a higher directory ** or a distant directory ** --Python relative import is not intuitive --It is not a relative position specification from "the file". If the calling file (the first python file to be executed) changes, ** relative import references will shift ** --In Python, ** Difficult to structure directories when importing your own tools from multiple projects ** --Because it is difficult to import upper directories and sibling directories
Install relative path reference package
pip install relpath
from relpath import add_import_path
add_import_path("../") #Here, specify the location of the tool you want to import by relative reference
from my_module import some_function
If you use the relpath
package, as in the example below,
You can achieve intuitive relative import of modules.
from relpath import add_import_path
add_import_path("../")
from my_module import some_function
some_function()
Looking at the above example, it seems to work just as sys.path.append ("../ ")
.
However, if the hierarchical structure of the project folder is complicated and one module is used from different locations, sys.path.append ("../ ")
may not be able to handle it.
Therefore, it is recommended to always use ʻadd_import_path of the
relpath` package when you want to implement relative import.
If you use the relpath
package, it is not limited to import
Intuitive relative path reference is possible.
For example, consider a project consisting of multiple python files, such as:
.
`-- project_folder
|-- parts
| |-- data.txt
| `-- script_B.py
`-- script_A.py
In script_A.py
, use script_B.py
as shown below.
# script_A.py
# load script_B.py
from parts.script_B import get_data
print(get_data())
In this case, as in the code example below,
Attempts to read "./data.txt "
relative to script_B.py
will fail. (Note 1)
(Note 1) Strictly speaking, it can be read by specifying the relative path from
script_A.py
, If the caller is changed to another location, it will not work properly and maintenance will be poor. To avoid this, we recommend using therelpath
package.
# script_B.py
def get_data():
with open("./data.txt", "r") as f: # -> FileNotFoundError: [Errno 2] No such file or directory: './data.txt'
return f.read()
So, if you write the following using the relpath
package,
You will be able to read "./data.txt "
relatively. (Note 2)
# script_B.py
from relpath import rel2abs
def get_data():
with open(rel2abs("./data.txt"), "r") as f: # -> NO ERROR!!
return f.read()
(Note 2) The python spec for relative paths is not necessarily wrong. The python spec (relative path specification is always interpreted relative to the first caller, regardless of the location of the file it describes) Even if the location (file) where commands such as reading a file are written changes while developing a program The advantage is that there is no need to change the path specification method. The
relpath
package is just a way to give programmers another option besides the python spec, so It is recommended to consider the necessity of use depending on the situation.
--PyPI link for the relpath
package
- https://pypi.org/project/relpath/
Recommended Posts