Как настроить пути Python для самодельных модулей? - PullRequest
0 голосов
/ 01 декабря 2019

У меня возникли проблемы при настройке структуры, которую мне дали. Вы можете увидеть иерархию папок каркаса на рисунках ниже:

enter image description here

enter image description here

enter image description here

enter image description here

Проблема заключается в импорте tug_resource_monitor_node.py , который вы можетеувидеть на последнем изображении. Импорт выглядит так:

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

Я получаю следующее сообщение об ошибке: Нет модуля с именем tug_resource_monitor.srv

Я уже пытался добавить модуль python через: sys.path.insert(0, "/home/username/catkin_ws/model_based_diagnosis/") (в начале tug_resource_monitor_node.py file) Я также попытался добавить путь к .profile , добавив следующую строку:

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

К сожалению, ничего из вышеперечисленного не сработало. Кто-нибудь может мне помочь?

Ответы [ 2 ]

1 голос
/ 01 декабря 2019

Изменение импорта на 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

и код, который я использовал для проверки

~$ 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()

1 голос
/ 01 декабря 2019

1 попытка export PYTHONPATH=$PYTHONPATH:/home/username/catkin_ws/model_based_diagnosis/scripts

2 попытка from tug_resource_monitor import *

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...