SoFunction
Updated on 2025-04-29

Example of implementation of module import under different directory structures in Python

In Python development, module import is a common operation, which allows us toReuse code, organize project structure and maintain code maintainability

1. Sub-directory directory

Directory structure

project/
│
├── 
│
└── subfolder/
    └── 

In VSCode, if you open it directly and run itThe Python interpreter willThe directory where you are located (i.e.project/) as the current working directory.
So you can import using the following codesubfolder/

# 
from subfolder import module

# Use functions in moduleresult = module.some_function()

On the command line, if you switch toproject/Directory, then runpython , the effect is the same as running directly in VSCode.
But if you don't switch directories, you run them directly in other directoriespython project/, the Python interpreter will take the directory where the command line is located as the current working directory, which may cause the import to fail because of the relative path.subfolderNot correct anymore.

2. Directory of the same level

Directory structure

project/
│
├── 
│
├── folder1/
│   └── 
│
└── folder2/
    └── 

Running in VSCodefolder1/, you can use the following code to import the directory at the same levelfolder2/

# folder1/
from ..folder2 import module2

# Use the function in module2result = module2.some_function()

This method may report an error in VSCode, because relative import fails when running as the main module.
This is because Python's relative import is designed to be used inside a package, not directly run from external files.
To solve this problem, you can use absolute import or modify

On the command line, you need to switch toproject/Directory, then runpython folder1/. This is the same as running directly in VSCode.
If you don't switch the directory, run it directlypython folder1/It may cause the import to fail because the Python interpreter starts looking for it from the directory where the command line is locatedfolder2

3. Advanced Directory

Directory structure

project/
│
├── 
│
└── subfolder/
    └── 

Running in VSCodesubfolder/You can use the following code to import the previous directory

# subfolder/
from .. import main

# Use functions in mainresult = main.some_function()

On the command line, you need to switch toproject/Directory, then runpython subfolder/. This is the same as running directly in VSCode. If you don't switch the directory, run it directlypython subfolder/It may cause the import to fail because the Python interpreter starts looking for it from the directory where the command line is located

Summarize

Import using absolute pathIt is ensured that the import is done correctly regardless of the current working directory.

This is the article about the implementation examples of importing under different directory structures in Python. For more related import content under different directory structures in Python, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!