Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't import Traveling Salesman Problem in NetworkX

I’m trying to run the TSP in NetworkX, and I’m following their code from https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.approximation.traveling_salesman.traveling_salesman_problem.html

However, whenever I try to import the module, it gives me an error.

For example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import networkx.approximation as na
gives the error

ModuleNotFoundError: No module named 'networkx.approximation'

And

import networkx.algorithms.approximation as naa

tsp = naa.traveling_salesman_problem

Gives the following error:

AttributeError: module 'networkx.algorithms.approximation' has no attribute 'traveling_salesman_problem'

I’m copying the code directly from the manual so I’m not sure what I’m doing wrong.

>Solution :

Someone who understands pip better may please jump in here.

I had the same errors as you mentioned and checked my version of networkx (which was 2.5.2) and apperantly the TSP part was added later. The current version mentioned on their website is 2.6.2. Through pip install networkx only 2.5.2 was installed. But with pip install --upgrade networkx[default] it upgraded to 2.6.2 and the the commands work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading