pytest类内导入异常及项目结构导入路径问题咨询
Hey there, let's break down what's going on here and fix those import headaches step by step—this is super common with Python project structures and pytest, so you're not alone!
First: Diagnose the Project Structure
9 times out of 10, weird import discrepancies boil down to missing __init__.py files or a misaligned project hierarchy. Let's start by confirming your setup looks something like this (adjust if your structure differs):
your_project_root/ ├── SRC/ │ ├── __init__.py # Critical—marks SRC as a Python package │ ├── Resources/ │ │ ├── __init__.py # Critical—marks Resources as a sub-package │ │ ├── MyClass.py │ │ ├── MyUtils.py │ │ └── OtherClass.py # The one that imports fine with `SRC.Resources.OtherClass` └── tests/ ├── __init__.py # Optional but recommended for pytest └── test_myclass.py # Your test file where you instantiate MyClass
If any of those __init__.py files are missing, Python won't recognize the directory as a proper package, which explains why MyClass has a wonky import path compared to other classes. Add empty __init__.py files to every directory that's part of your source code (SRC, Resources, tests) if they're missing.
Fix the MyClass Import in Your Test File
If your structure matches the example above, you should be able to import MyClass the same way as other classes:
# In test_myclass.py from SRC.Resources.MyClass import MyClass
If this isn't working, double-check:
- Did you accidentally put MyClass in a subdirectory under Resources (like
Resources/SomeSubfolder/MyClass.py)? If so, your import would need to befrom SRC.Resources.SomeSubfolder.MyClass import MyClass. - Are you running pytest from the wrong working directory? If you're in the
tests/folder when you runpytest, Python won't see theSRCpackage. Always run pytest from your project root.
Fix MyClass.py's Import of MyUtils
When MyClass tries to import MyUtils and throws an error, it's almost always an issue with relative vs absolute imports, or Python not knowing where to look for the package. Here are two reliable fixes:
Option 1: Use Relative Import (Best for Same-Package Files)
Since MyClass and MyUtils are in the same Resources sub-package, use a relative import in MyClass.py:
# In MyClass.py from .MyUtils import MyUtils # The dot means "look in the same directory as this file"
Option 2: Use Absolute Import (More Explicit)
If relative imports feel confusing, use a full absolute path that matches how you import in your test file:
# In MyClass.py from SRC.Resources.MyUtils import MyUtils
Ensure Pytest Can Find Your Packages
Even if your imports look right, pytest might still struggle to locate your SRC package. Fix this by:
- Running pytest from your project root: Open your terminal, navigate to
your_project_root/, then run:pytest tests/ - Add a pytest.ini file (if you don't have one already) in your project root. This tells pytest to add your root directory to Python's path:
# pytest.ini in your_project_root/ [pytest] pythonpath = .
Quick Troubleshooting Checks
- Avoid naming files after Python built-in modules (e.g., don't call a file
utils.py—it can conflict with standard library modules). - Never run MyClass.py directly as a script (e.g.,
python SRC/Resources/MyClass.py) if you're using relative imports—this will break them, because Python treats the file as a top-level script instead of part of a package.
内容的提问来源于stack exchange,提问作者Morgan LaVigne

