简体   繁体   English

如何为自制模块设置 python 路径?

[英]How do I setup python paths for selfmade modules?

I ran into trouble while setting up a framework which I have been given.我在设置我得到的框架时遇到了麻烦。 You can see the folder hierarchy of the framework in the pictures below:您可以在下图中看到框架的文件夹层次结构:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

The problem is with the imports of the tug_resource_monitor_node.py which you can see in the last image.问题在于您可以在最后一张图片中看到的tug_resource_monitor_node.py的导入。 Imports look like:进口看起来像:

import rospy
from tug_resource_monitor.srv import *
import rosnode
import os
import psutil
from tug_resource_monitor.msg import NodeInfo, NodeInfoArray
from std_msgs.msg import Header
from tug_python_utils import YamlHelper as Config

I get the following error message: No module named tug_resource_monitor.srv我收到以下错误消息:No module named tug_resource_monitor.srv

I have already tried adding the python module through: sys.path.insert(0, "/home/username/catkin_ws/model_based_diagnosis/") (at the beginning of the tug_resource_monitor_node.py file) I also tried adding the path to .profile by adding the following line:我已经尝试通过以下方式添加 python 模块: sys.path.insert(0, "/home/username/catkin_ws/model_based_diagnosis/") (在tug_resource_monitor_node.py文件的开头)我也尝试将路径添加到.profile通过添加以下行:

export PYTHONPATH=$PYTHONPATH:/home/username/catkin_ws/model_based_diagnosis

Unfortunately none of the above worked.不幸的是,以上都没有奏效。 Can anybody help me out?有人可以帮我吗?

1 try export PYTHONPATH=$PYTHONPATH:/home/username/catkin_ws/model_based_diagnosis/scripts 1 尝试export PYTHONPATH=$PYTHONPATH:/home/username/catkin_ws/model_based_diagnosis/scripts

2 try from tug_resource_monitor import * 2 尝试from tug_resource_monitor import *

Changing import to from model_based_diagnosis.tug_resource_monitor.srv import * will work.将导入更改为from model_based_diagnosis.tug_resource_monitor.srv import *将起作用。

catkin_ws/
└── model_based_diagnosis
    ├── __init__.py
    └── tug_resource_monitor
        ├── scripts
        │   └── tug_resource_monitor_node.py
        └── srv
            ├── __init__.py
            └── test_import.py

~$ pwd
/Users/username/catkin_ws/model_based_diagnosis/tug_resource_monitor/scripts

~$ python tug_resource_monitor_node.py 
import module from srv
Import form  /Users/username/catkin_ws/model_based_diagnosis/tug_resource_monitor/srv/test_import.py

and code I used to test和我用来测试的代码

~$ cat tug_resource_monitor_node.py 
import sys

sys.path.insert(0, "/Users/username/catkin_ws")
from model_based_diagnosis.tug_resource_monitor.srv import test_import
print("import module from srv")
test_import.import_test()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM