It's time to study the pass.
It was a Python file I made, and I could use it by specifying the path when reading another file. I will leave it as a memo. The content is for beginners.
--The form sys.path.append (os.pardir) using sys library and os library is often used. --If you specify a relative path in the hoge part of sys.path.append ("hoge"), it will be read from there.
Assuming you want to read functions.py from main.py in the following file
/application
/common
functions.py
/main
main.py #Suppose you are here now
import sys, os
sys.path.append(os.pardir)
#File name you want to read
from common.functions import hogehoge
--sys, os-> Python basic library (= library that can be used only by specifying import without installation). sys enables operations related to the python execution environment, and os enables basic operations related to the OS.
--sys.path-> Represents the path to load the library (like C: / hogehoge / hogehoge /)
--sys.path.append ("hoge")-> Change the above to hoge
--os.pardir-> Represents a string representing the OS-dependent parent directory. For Windows, it corresponds to *** ".." *** (corresponds to `..``` when typing
`cd ..``` at the command prompt)
--In the above code, the reading directory of the library is changed to / applications at the moment when sys.path.append (os.pardir) and the code are read, so common.functions.py can be read after that. ing
import sys
sys.path.append("../common")
--What you are doing is the same as Method 1
――However, you can express a complicated directory here.
--For example, `` sys.path.append ("../../another_application/lib/hogehoge ")
`
(that's all)
Recommended Posts