You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3.6.9中tests包导入models包模块失败求助

Hey, let's fix that import issue you're facing. The core problem here is how Python resolves module paths, and there are a few straightforward ways to get your test file importing ArmModel correctly. Let's break down the solutions step by step:

1. First, fix the package structure

Your src directory is missing an __init__.py file—Python requires this to recognize src as a proper package. Just create an empty file at project/src/__init__.py; this alone might resolve some of PyCharm's unresolved reference warnings.

2. Add the project root to Python's path (quick fix)

Modify your test_arm_model.py to add the project root directory to Python's module search path before importing. This ensures Python can find the src package no matter where you run the test from:

import sys
import os
# Add the project root directory to sys.path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))

import unittest
from src.models.ArmModel import ArmModel

class TestArmModel(unittest.TestCase):
    def test_arm_model(self):
        arm = ArmModel('arm')
        self.assertEqual(arm.definition, 'arm')

if __name__ == '__main__':
    unittest.main()

Let me explain the path logic:

  • os.path.dirname(__file__) gets the directory where test_arm_model.py lives (project/src/tests)
  • ../../ navigates up two levels to reach the project root (project)
  • os.path.abspath() converts this relative path to an absolute path so Python can reliably find it

3. Use relative imports (requires running as a module)

If you prefer not to modify sys.path, you can use relative imports—but you have to run the test as a Python module instead of directly executing the file.

First, update the import line in test_arm_model.py:

import unittest
from ..models.ArmModel import ArmModel  # .. refers to the parent directory (src)

class TestArmModel(unittest.TestCase):
    def test_arm_model(self):
        arm = ArmModel('arm')
        self.assertEqual(arm.definition, 'arm')

if __name__ == '__main__':
    unittest.main()

Then run the test from the project root directory using this command:

python -m src.tests.test_arm_model

This works because running with -m tells Python to treat the code as part of the package hierarchy, making relative imports valid.

4. Set the PYTHONPATH environment variable

You can also tell Python where to find your project by setting the PYTHONPATH environment variable to your project root.

  • On Linux/macOS, run this in your terminal before executing the test:
    export PYTHONPATH=/home/jamal/dev/tcc:$PYTHONPATH
    
  • On Windows (Command Prompt):
    set PYTHONPATH=C:\path\to\your\project;%PYTHONPATH%
    

After setting this, you can run your test file normally, and Python will automatically search the project root for modules.

5. Turn your project into an installable package (best for long-term projects)

For a more robust solution, set up your project as an installable package. This lets you import modules from anywhere on your system without path hacks.

  1. Create a setup.py file in your project root (project/setup.py) with this content:
from setuptools import setup, find_packages

setup(
    name="arm-project",
    version="0.1",
    packages=find_packages(where="src"),
    package_dir={"": "src"},
)
  1. Install the package in editable mode (so changes to your code are reflected immediately):
pip install -e .

Now you can import ArmModel directly in your test file without any path modifications:

import unittest
from models.ArmModel import ArmModel

class TestArmModel(unittest.TestCase):
    def test_arm_model(self):
        arm = ArmModel('arm')
        self.assertEqual(arm.definition, 'arm')

if __name__ == '__main__':
    unittest.main()

This also fixes PyCharm's unresolved reference issues since it'll recognize the installed package.


内容的提问来源于stack exchange,提问作者Fernando Augusto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 14:39:04