[Python] Import the module one level higher

mydir └ parent.py   └ child.py

How to import parent.py from child.py in a folder structure such as

child.py



import sys
import pathlib

#Get the absolute path of the next higher hierarchy
parent_dir = str(pathlib.Path(__file__).parent.parent.resolve())

#Add the absolute path of the next higher hierarchy to the module search path
sys.path.append(parent_dir)

import parent

Recommended Posts

[Python] Import the module one level higher
Python module import
python original module import
How Python module import works
About the Python module venv
2015-11-26 python> Display the function list of the module> import math> dir (math)
[Python] How to import the library
Try using the Python Cmd module
Become the next higher Python user
Master the weakref module in Python
Pass the path of the imported python module
Check the path of the Python imported module
Module import and exception handling in python
Install the Python module in any directory
Import Error in Python3: No module named'xxxxx'
[Python] Colored map plot at the city level
python> import seiral> ImportError: No module named serial
[Python] Let's execute the module regularly using schedule
Import python script
Specifying the module loading destination with GAE python
I tried using the Datetime module by Python
[Python] A rough understanding of the logging module
Try using the collections module (ChainMap) of python3
The one that displays the progress bar in Python
Import cv2 ModuleNotFoundError: No module named'cv2' in python3
Python collections module
Examine Python script bottlenecks with the cProfile module
Use the nghttp2 Python module from Homebrew from pyenv's Python
About Python external module import <For super beginners>
Summarize Python import
[Reintroduction to python] How to import via the parent directory
How to use the Raspberry Pi relay module Python
[python] Get the list of classes defined in the module
Let's use the Python version of the Confluence API module.
Write data to KINTONE using the Python requests module
Call your own python module from the ROS package
Python module (Python learning memo ④)
One liner in Python
Create a Python module
python Zundko One Liner
Python from or import
the zen of Python
[Python] Split the date
Import tsv with Python
[Hikari-Python] Chapter 08-01 Module (Import Module)
Dynamically load the module.
[Python of Hikari-] Chapter 08-03 Module (Import and use of standard library)
python> link> dir_util.copy_tree ()> update = 1> Do not copy the acquired one?
Why can I use the module by importing with python?
Make the display of Python module exceptions easier to understand
Minimum knowledge to get started with the Python logging module
View using the python module of Nifty Cloud mobile backend
Different from the import type of python. from A import B meaning
[Note] Import of a file in the parent directory in Python
Hook to the first import of the module and print the module path
[Python: UnicodeDecodeError] One of the error solutions when reading CSV
Solve the Japanese problem when using the CSV module in Python.
What is the default TLS version of the python requests module?