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

Python跨目录导入问题:OpenBMP包ImportError排查求助

Troubleshooting ImportError: No module named mrt.logger in OpenBMP's openbmp-mrt

Let's break down why you're hitting this import error and fix it step by step. From your directory structure, the openbmp.mrt.logger module does exist, so the issue is almost certainly with how Python is resolving the package path.

First, let's rule out common missteps with your current attempts

  1. Adding src/site-packages to sys.path: If you added this path after the import statement, Python would have already tried to resolve the module before the path was available. That's a super easy mistake to make.
  2. Adding __init__.py to src/site-packages: You actually don't need this file here—site-packages is just a directory that Python looks into for packages, but the package itself is openbmp (which already has its own __init__.py, as you showed). This step didn't break anything, but it also didn't fix your problem.

Here are the reliable fixes to try

Fix 1: Dynamically add the correct path at the start of your script

Hardcoding absolute paths can lead to issues if your directory structure moves. Instead, add this code at the very top of src/bin/openbmp-mrt, before any import statements:

import sys
import os

# Get the absolute path to the src directory (parent of bin)
src_dir = os.path.dirname(os.path.abspath(__file__))
# Build the path to site-packages
site_packages_path = os.path.join(src_dir, '../site-packages')
# Convert to absolute path to avoid relative path issues
site_packages_path = os.path.abspath(site_packages_path)

# Add the path to sys.path if it's not already there
if site_packages_path not in sys.path:
    sys.path.insert(0, site_packages_path)

This ensures Python can find the openbmp package no matter where you run the script from.

Fix 2: Verify the path is being added correctly

To confirm the path is in Python's module search list, add a print statement right after the code above:

print("Current sys.path:", sys.path)

Run the script and check if the absolute path to src/site-packages appears in the output. If it doesn't, double-check the path construction—maybe there's a typo in directory names, or your script's location is different than expected.

Fix 3: Use the PYTHONPATH environment variable (no script edits needed)

If you don't want to modify the script, you can tell Python where to find the package via the PYTHONPATH environment variable before running the program. On Linux/macOS:

export PYTHONPATH=/absolute/path/to/src/site-packages:$PYTHONPATH
./src/bin/openbmp-mrt

On Windows (Command Prompt):

set PYTHONPATH=C:\absolute\path\to\src\site-packages;%PYTHONPATH%
src\bin\openbmp-mrt

This makes Python look in your site-packages directory for packages every time you run the script, without changing the code.

Bonus: Verify your package structure is valid

Double-check that:

  • src/site-packages/openbmp/__init__.py exists (even if it's empty)
  • src/site-packages/openbmp/mrt/__init__.py exists (also can be empty)
  • There are no syntax errors in either __init__.py file—you can test this by running python -c "import openbmp.mrt" from the command line. If this throws an error, fix the syntax in the relevant file first.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:07:21