import sys# Add the ptdraft folder path to the sys.path listsys.path.append('/path/to/ptdraft/')
# Now you can import your modulefrom ptdraft import nib# Or justimport ptdraft
from inspect import getsourcefileimport os.path as path, syscurrent_dir = path.dirname(path.abspath(getsourcefile(lambda:0)))sys.path.insert(0, current_dir[:current_dir.rfind(path.sep)])import my_module # Replace "my_module" here with the module name.sys.path.pop(0)
对于比这更少的行,将第二行替换为import os.path as path, sys, inspect, 在getsourcefile的开头添加inspect.(第3行)并删除第一行。 -但是这会导入所有模块,因此可能需要更多的时间,内存和资源。
我的答案代码(更长的版本)
from inspect import getsourcefileimport os.pathimport sys
current_path = os.path.abspath(getsourcefile(lambda:0))current_dir = os.path.dirname(current_path)parent_dir = current_dir[:current_dir.rfind(os.path.sep)]
sys.path.insert(0, parent_dir)
import my_module # Replace "my_module" here with the module name.
# Just add parent path
add_parent_path(1)
# Append to syspath and delete when the exist of with statement.
with add_parent_path(1):
# Import modules in the parent path
pass