Summary of how to import files in Python 3

Files in the same directory

.
├── brother.py
└── main.py

brother.py


def hi():
    print("Hi! I'm your brother.")

main.py


import brother
brother.hi()

Individual files in the directory below

.
├── children
│   └── tom.py
└── main.py

tom.py


def hi():
    print("Hi! I'm Tom.")

** Good example 1 **:

main.py


from children import tom
tom.hi()

** Good example 2 **:

main.py


import children.tom
children.tom.hi()

** Bad example **: → Need to be packaged (see below)

main.py


import children
children.tom.hi() # >> AttributeError: module 'children' has no attribute 'tom'

Multiple files in the directory below

Create __init__.py and treat that directory as a package. In __init__.py, load the modules contained in the same directory there.

.
├── children
│   ├── __init__.py
│   ├── sushi.py
│   └── tom.py
└── main.py

sushi.py


def hi():
    print("Hi! I'm Sushi.")

** Good example **: In the module, use . to specify the module to be read with a relative path. (Official Release Notes, [Reference](http://stackoverflow.com/questions/22942650/relative-import-from- init-py-file-throws-error)))

__init__.py


from . import tom
from . import sushi

main.py


import children
children.sushi.hi()

** △ example **: You can also do this

__init__.py


from .tom import hi as tom_hi
from .sushi import hi as sushi_hi

main.py


from children import *
sushi_hi()

** Bad example **

__init__.py


import tom # >> ImportError: No module named 'tom'
import sushi

If it is troublesome to add a module every time, refer to here and read all the files in the same directory.

Recommended Posts

Summary of how to import files in Python 3
Summary of how to use MNIST in Python
[Python] Summary of how to use pandas
[Python2.7] Summary of how to use unittest
Summary of how to write .proto files used in gRPC
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
How to get the files in the [Python] folder
How to develop in Python
Summary of tools needed to analyze data in Python
How to get the number of digits in Python
How to download files from Selenium in Python in Chrome
How to add page numbers to PDF files (in Python)
[Python] Summary of how to specify the color of the figure
[Python] How to do PCA in Python
Summary of how to use pandas.DataFrame.loc
How to collect images in Python
How to use SQLite in Python
Handling of JSON files in Python
Summary of how to use pyenv-virtualenv
[Python] How to import the library
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
Summary of how to use csvkit
How to handle Japanese in Python
How to get a list of files in the same directory with python
How to import Python library set up in EFS to Lambda
[python] Summary of how to retrieve lists and dictionary elements
How to import a file anywhere you like in Python
[Python] Summary of how to use split and join functions
How to develop in a virtual environment of Python [Memo]
Comparison of how to use higher-order functions in Python 2 and 3
How to get a list of built-in exceptions in python
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to dynamically define variables in Python
How to do R chartr () in Python
Output tree structure of files in Python
[Itertools.permutations] How to put permutations in Python
Summary of various for statements in Python
How to work with BigQuery in Python
[Python] How to use import sys sys.argv
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
Summary of built-in methods in Python list
How to use regular expressions in Python
How to display Hello world in python
How to read CSV files in Pandas
How to use is and == in Python
How to write Ruby to_s in Python
Summary of how to write AWS Lambda
[Question] How to use plot_surface of python
How to display a specified column of files in Linux (awk)