I was a little worried, and when I looked it up, it turned out to be a memo
The import statement that everyone casually uses. I'm sure everyone used it once when they started studying python to use the modules installed with pip.
import os
import math
import cv2
...
Once you're able to write python to some extent, you'll want to split and manage your own code. For example, it's like creating a file that defines a lot of functions called util_func.py so that it can be used in other scripts.
At that time, without any thought
from util_func import f1
If you write something like this, do you guys get the error Module not found?
When I looked it up there, it seems that it's okay to insert the following code.
sys.path.append(os.pardir) #It depends on the file structure. .. ..
When I actually try it, it's done properly. .. .. ..
The introduction has become long, but it is the main subject. Here's a quick explanation of what this one-line script does.
First, try printing sys.path.
import sys
print(sys.path)
Then the following list will be returned.
[ '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages', '~/.local/lib/python3.6/site-packages/IPython/extensions', '~/.ipython', '..']
This shows the path that python can read.
In other words, when I imported my own function or class from another file, I was angry with "module not found" because the path of the script did not exist in sys.path
.
Therefore, if you use the ʻappend` method to add path information to the list, you can call your own script brilliantly.
Recommended Posts