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:
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.