Why can I use the module by importing with python?

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
...

I want to call a class or function I wrote

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. .. .. ..

What this one-line script is doing

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

Why can I use the module by importing with python?
I tried using the Datetime module by Python
[Python] I want to use the -h option with argparse
I liked the tweet with python. ..
I couldn't use tkinter with python installed by pyenv of anyenv
Until you can use opencv with python
I tried using "Streamlit" which can do the Web only with Python
I get a Python No module named'encodings' error with the aws command
I made a module PyNanaco that can charge nanaco credit with python
I want to use MATLAB feval with python
I tried "differentiating" the image with Python + OpenCV
Specify the Python executable to use with virtualenv
Use logger with Python for the time being
I tried "binarizing" the image with Python + OpenCV
The easiest way to use OpenCV with python
Specifying the module loading destination with GAE python
I want to use Temporary Directory with Python2
Examine Python script bottlenecks with the cProfile module
Use the nghttp2 Python module from Homebrew from pyenv's Python
I tried searching for files under the folder with Python by file name
[Python] Use JSON with Python
Use mecab with Python3
Use DynamoDB with Python
Use Python 3.8 with Anaconda
Use python with docker
Why I chose Python
String manipulation with python & pandas that I often use
I tried to touch the CSV file with Python
How to use the Raspberry Pi relay module Python
3 things I noticed by analyzing twitter followers with python
I tried to solve the soma cube with python
Mitsuhiko "I can tell the criminal by introducing myself!"
I wanted to use the Python library from MATLAB
I want to inherit to the back with python dataclass
Let's use the Python version of the Confluence API module.
Specify MinGW as the compiler to use with Python
A memo that I touched the Datastore with python
I tried to solve the problem with Python Vol.1
I want to use the R dataset in python
I moved the automatic summarization API "summpy" with python3.
I tried hitting the API with echonest's python client
[Python] You can save an object to a file by using the pickle module.
I thought about why Python self is necessary with the feeling of a Python interpreter
I tried to find the entropy of the image with python
I tried fp-growth with python
I tried scraping with Python
I replaced the Windows PowerShell cookbook with a python script.
I tried "gamma correction" of the image with Python + OpenCV
I tried to simulate how the infection spreads with Python
Use Trello API with python
I tried using the Python library from Ruby with PyCall
I wanted to solve the Panasonic Programming Contest 2020 with Python
Use Twitter API with Python
Minimum knowledge to get started with the Python logging module
About the Python module venv
I made blackjack with python!
I wrote the basic grammar of Python with Jupyter Lab
Use TUN / TAP with Python
Why shouldn't we use the word "pass by reference" in Java, JavaScript, Python, Ruby, etc.?
I made a class to get the analysis result by MeCab in ndarray with python
I evaluated the strategy of stock system trading with Python.