Python中如何从父目录及嵌套目录文件调用函数?含模块/包使用方案咨询
Hey there! I totally get how confusing module imports can be, especially when you're digging through threads and not finding clear answers. Let's walk through your exact scenario first, then tackle nested directories.
Since main.py and main2.py live in the same directory, this is straightforward—Python automatically checks the current directory for modules when you run a script.
You have two simple options:
Option 1: Import the specific function directly
In main2.py, add this code:
# main2.py from main import print_hello # Call the function greeting = print_hello() print(greeting) # Output: hello world!!
Option 2: Import the entire module
If you want to use multiple functions from main.py later, you can import the whole module:
# main2.py import main # Call the function using the module prefix greeting = main.print_hello() print(greeting)
Either approach works perfectly here—pick whichever feels cleaner for your use case.
When dealing with deeply nested folders, we need to use Python's package system (directories marked as packages with an __init__.py file; note: Python 3.3+ supports namespace packages without this file, but adding it makes your structure explicit and avoids edge cases).
Let’s use a sample nested structure to demonstrate:
project_root/ ├── dir_1/ │ ├── __init__.py │ ├── main.py # Contains print_hello() │ └── sub_dir/ │ ├── __init__.py │ └── main2.py # Needs to import print_hello() └── dir_2/ ├── __init__.py └── another_script.py # Also needs print_hello()
Case 1: Import from a parent directory (sub_dir/main2.py → dir_1/main.py)
Use relative imports to reference the parent directory. The .. syntax means "go up one directory level":
# dir_1/sub_dir/main2.py from ..main import print_hello print(print_hello())
⚠️ Note: When using relative imports, don’t run the script directly (e.g., python dir_1/sub_dir/main2.py will throw an error). Instead, run it as a module from the project root:
python -m dir_1.sub_dir.main2
Case 2: Import from an unrelated directory (dir_2/another_script.py → dir_1/main.py)
Here, we need to tell Python where to find the dir_1 package. There are two reliable ways to do this:
Method 1: Add the project root to Python’s path in your script
This makes the project root visible to Python’s module search system:
# dir_2/another_script.py import sys from pathlib import Path # Get the path to project_root (assuming this script is in project_root/dir_2) project_root = Path(__file__).parent.parent sys.path.append(str(project_root)) # Now you can import directly from dir_1.main import print_hello print(print_hello())
Method 2: Set the PYTHONPATH environment variable
Add your project root to the PYTHONPATH environment variable so Python always searches it for packages.
- On Linux/macOS (terminal):
export PYTHONPATH="/full/path/to/project_root:$PYTHONPATH" - On Windows (Command Prompt):
set PYTHONPATH=C:\full\path\to\project_root;%PYTHONPATH%
Once set, you can import directly in another_script.py without modifying the path in code:
from dir_1.main import print_hello
If you want to simplify imports for your package, you can export commonly used functions in the __init__.py file. For example, in dir_1/__init__.py:
from .main import print_hello
Now other scripts can import print_hello directly from the dir_1 package:
from dir_1 import print_hello
内容的提问来源于stack exchange,提问作者Shubham Sharma

