0%

Import Python File Like Module

[TOC]
Content

1 Intro

We have a hierachy of python files as follows. We will introduce how to import .py file like a module in four different situations.

1
2
3
4
5
6
7
8
9

|--- parent_dir
|--- parent_file.py
|--- parent_test.py
|--- sub_dir1
|--- sub_file.py
|--- sub_dir2
|--- sub_test.py

In parent_file.py:

1
2
3
4
5

# function in parent_file.py
def parent_fun():
print("This is a function from parent_file")

In sub_file.py:

1
2
3
4
5

# function in parent_file.py
def sub_fun():
print("This is a function from sub_file")

2 Import from same directory

1
2
3
4
5
6
7
8

|--- parent_dir
|--- **parent_file.py**
|--- **parent_test.py**
|--- sub_dir1
|--- sub_file.py
|--- sub_dir2
|--- sub_test.py

We will write codes in parent_test.py to use the function in parent_file.py
Method 1: import xxx.py

1
2
3
import parent_file
parent_file.parent_fun()

Method 2: from xxx.py import function

1
2
3
4
#parent_test.py
from parent_file import parent_fun
parent_fun()

3 Import from sub directory

We will write codes in parent_test.py to use the function in sub_file.py

1
2
3
4
5
6
7
8

|--- parent_dir
|--- parent_file.py
|--- **parent_test.py**
|--- sub_dir1
|--- **sub_file.py**
|--- sub_dir2
|--- sub_test.py

Method: from dir import xxx.py

1
2
3
4
#parent_test.py
from sub_dir1 import sub_file
sub_file.sub_fun()

4 Import from parent directory

We will write codes in sub_test.py to use the function in parent_file.py

1
2
3
4
5
6
7
8

|--- parent_dir
|--- **parent_file**.py
|--- parent_test.py
|--- sub_dir1
|--- sub_file.py
|--- sub_dir2
|--- **sub_test**.py

Method: Change curent path to parent dir (target dir)
Tips: If you run the code in terminal, swith the path to sub_dir2 (The dir of your sub_test.py in).

1
2
3
4
5
6
7
8
9
10
11
12
#parent_test.py
import sys
sys.path.append("...")

## Method 1
import parent_file
parent_file.parent_fun()

## Method 2
from parent_file import parent_fun
parent_fun()

5 Import from neighbor directory

We will write codes in sub_test.py to use the function in sub_file.py

1
2
3
4
5
6
7
8

|--- parent_dir
|--- parent_file.py
|--- parent_test.py
|--- sub_dir1
|--- **sub_file**.py
|--- sub_dir2
|--- **sub_test**.py

Method: The same as part 4. Change curent path to parent dir.
Tips: If you run the code in terminal, swith the path to sub_dir2 (The dir of your sub_test.py in).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#parent_test.py
import sys
sys.path.append("..")

## Method 1
from sub_dir1 import sub_file
sub_file.sub_fun()

## Method 2
from sub_dir.sub_file import sub_fun
sub_fun()

## Method 3
import sub_dir.sub_file as sub_file
sub_file.sub_fun()

6 Supplementary

6.1 __init__.py

Usually, we need to create a blank __init__.py file in our dir of .py file to be imported. However, in my experiments (python 3.9.7 as interpreter), all above 4 types work well without __init__.py.

6.2 How import works?

Please see the following experiment results.

6.3 Path

sys.path.append() is used to add new path to the default path.
sys_path

1
2
3
4
5
6
7
8
9
10
11

(LiziX) D:\research\programming\python\python_import\parent_dir\sub_dir2>python
Python 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', 'D:\\software\\anaconda\\next\\envs\\LiziX\\python39.zip', 'D:\\software\\anaconda\\next\\envs\\LiziX\\DLLs', 'D:\\software\\anaconda\\next\\envs\\LiziX\\lib', 'D:\\software\\anaconda\\next\\envs\\LiziX', 'D:\\software\\anaconda\\next\\envs\\LiziX\\lib\\site-packages', 'D:\\software\\anaconda\\next\\envs\\LiziX\\lib\\site-packages\\win32', 'D:\\software\\anaconda\\next\\envs\\LiziX\\lib\\site-packages\\win32\\lib', 'D:\\software\\anaconda\\next\\envs\\LiziX\\lib\\site-packages\\Pythonwin']
>>> sys.path.append("D:\\software\\anaconda\\next\\envs\\LiziX\\python39.zip")
>>> print(sys.path)
['', 'D:\\software\\anaconda\\next\\envs\\LiziX\\python39.zip', 'D:\\software\\anaconda\\next\\envs\\LiziX\\DLLs', 'D:\\software\\anaconda\\next\\envs\\LiziX\\lib', 'D:\\software\\anaconda\\next\\envs\\LiziX', 'D:\\software\\anaconda\\next\\envs\\LiziX\\lib\\site-packages', 'D:\\software\\anaconda\\next\\envs\\LiziX\\lib\\site-packages\\win32', 'D:\\software\\anaconda\\next\\envs\\LiziX\\lib\\site-packages\\win32\\lib', 'D:\\software\\anaconda\\next\\envs\\LiziX\\lib\\site-packages\\Pythonwin', 'D:\\software\\anaconda\\next\\envs\\LiziX\\python39.zip']
>>> ^Z
-------------End of blogThanks for your reading-------------