Python跨目录导入问题:OpenBMP包ImportError排查求助
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
- Adding
src/site-packagestosys.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. - Adding
__init__.pytosrc/site-packages: You actually don't need this file here—site-packagesis just a directory that Python looks into for packages, but the package itself isopenbmp(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__.pyexists (even if it's empty)src/site-packages/openbmp/mrt/__init__.pyexists (also can be empty)- There are no syntax errors in either
__init__.pyfile—you can test this by runningpython -c "import openbmp.mrt"from the command line. If this throws an error, fix the syntax in the relevant file first.
内容的提问来源于stack exchange,提问作者Craig

