1

我是 python 和 or-tools 的新手。我正在尝试从 Google OR-Tools运行这个示例程序。

我已经安装了 or-toolspython -m pip install --upgrade --user ortools并从链接复制了完整的脚本,并添加#!/usr/bin/env python3chmod +x tsp.py.

运行脚本后,出现了这个错误:

Traceback (most recent call last):
File "tsp.py", line 5, in <module>
from ortools.constraint_solver import routing_enums_pb2
ModuleNotFoundError: No module named 'ortools'

首先,我认为这是因为 PYTHONPATH,所以这样做了:

export PYTHONPATH="./.local/lib/python2.7/site-packages/ortools"

但是错误文本没有改变。如果有人知道它是什么原因,请帮助我..谢谢

- - - - - -更新 - - - - - - - - - - - -

收到评论后,我运行了python -m site This is displayed。

ros@ros-dynabook-R734-M:~/tsp$ python -m site
sys.path = [
'/home/ros/tsp',
'/opt/ros/melodic/lib/python2.7/dist-packages',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/home/ros/.local/lib/python2.7/site-packages',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/wx-3.0-gtk3',
]
USER_BASE: '/home/ros/.local' (exists)
USER_SITE: '/home/ros/.local/lib/python2.7/site-packages' (exists)
ENABLE_USER_SITE: True

我也去了~/python2.7/site-packages/和~/python3.6/dist-packages/,但是没有ortools模块。。

4

1 回答 1

0

我需要安装“OR-Tools”python3 -m pip install --upgrade --user ortools才能在 python3 中使用。而且 PYTHONPATH 应该是包的父目录,即./.local/lib/python2.7/site-packages/.

现在脚本似乎运行正常。

于 2020-10-14T14:48:00.320 回答