简体   繁体   中英

Python module not found error "No module named 'ortools' "

I'm new to python and or-tools. I'm trying to run thissample program from Google OR-Tools.

I've installed or-tools by python -m pip install --upgrade --user ortools and copied full script from the link, and added #!/usr/bin/env python3 and did chmod +x tsp.py .

After running the script, this error showed up:

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'

First, I thought this is because PYTHONPATH, so did :

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

But the error text didn't changed. If anyone knows what it cause, help me please.. Thank you

-----------UPDATE-----------------------

After I got comments, I ran python -m site This is showed up.

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

I also went to ~/python2.7/site-packages/ and ~/python3.6/dist-packages/, but there is no ortools module..

I needed to install 'OR-Tools' by python3 -m pip install --upgrade --user ortools to use in python3. And also PYTHONPATH should be the parent directory of the packages which is ./.local/lib/python2.7/site-packages/ .

Now the script seems running correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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