chinese-postman
chinese-postman copied to clipboard
AttributeError: module 'networkx' has no attribute 'connected_component_subgraphs'
I read https://github.com/rkistner/chinese-postman/issues/21 .
Doing
pip install --force-reinstall networkx==2.3
on Command Prompt for Windows operating system and Terminal on Linux operating system does not solve the problem when using it as a plugin for QGIS Desktop software.
The following images show what I did with red lines going around the button I clicked. If there is more than 1 line going around, there is number on the left showing the order it was clicked.
experiencing the same on macOS🥲
experiencing the same on macOS🥲
turns out unlike I've read somewhere qgis does install it's own python interpter. I've run pip install --force-reinstall networkx==2.3
on the pip of the qgis python installation and got one step further!
I did encounter another issue though but thankfully it was an easy fix. qgis comes with python3.9 which has moved some function called gcd from a module named fractions to math. I've changed in a file named dag.py from fractions import gcd
to from math import gcd
and it seems like everything works now.
@MgTheProgrammer How did you run the QGIS version of PIP? I tried running in from a Windows command prompt in the directory that pip.exe is installed in under QGIS, but get a fatal python error and no module named encodings.
@MgTheProgrammer I also tried:
import pip
pip.main(['install', '--force-reinstall', 'networkx==2.3'])
But got tons of errors. Next I tried:
pip.main(['install', '--force-reinstall networkx==2.3'])
But this crashes QGIS (I opened, re-ran the import and called it again and it crashes every time)
Another thing I tried was running this from the QGIS python directory:
python -m pip install --force-reinstall networkx==2.3
But this failed with a message pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available
following which it failed to download anything due to the missing ssl module.
@michael-ts
Sadly I don't quite remember what I did. I imagine though that I ran pip using its directory (on Mac it probably looked like that:
/Applications/QGIS.app/Contents/MacOS/bin/pip3 install networkx==2.3
).
I opened qgis now as you reminded me about this. And weirdly enough it doesn't work correctly, it gives me a path and concludes that its length is 0 km. I'm pretty sure last time I've played with it, it worked though. So I guess it's more related to something wrong with the input I give from qgis.
Btw if what you are looking for is mostly an optimized route and less about specifically using it with qgis. This repo works without much effort from you as a user. It does give a bit less control but it's good enough imo.
@MgTheProgrammer Thanks, I'm looking at this and it looks like it might be helpful at least until I can figure out how to get the QGIS plug-in working. I noticed a couple of issues which using QGIS may or may not solve:
-
I accidentally deleted a segment in the middle of a road I wanted to follow (not sure why; apparently it tries to find the closest segment to the mouse and despite the segment it deleted being some distance away it must have been the closest) and couldn't figure out how to add it back, so all the clicking I had done so far was wasted. It seems to be a LOT of clicking as there are lots of tiny line segments making up a single stretch of road.
-
There doesn't seem to be any way of specifying that you can take a segment if needed, but don't have to. In this case I had two disconnected networks of roads I wanted to reach in one trip, and it would be nice if it could find them minimal distance way to get between the two. But as soon as I the two networks became disconnected, the one not attached to my starting point was automatically erased.
@MgTheProgrammer Also, it seems to be brute-forcing the solution. I thought there was a polynomial time solution; this isn't the travelling salesman problem...
@MgTheProgrammer It's also not clear to me how to find the actual route... I downloaded the GPX file (was the only option offered). I tried to download it into Google Maps but it claimed (wrongly) that the GPX file was bad. I found a GPX to KML convertor but when I look at the route on the map it shows all the streets reached but I can't tell in what order to visit them. I found an online GPX editor and I can tell the route but only by dragging a slider and this would be a pain to try to manually transcribe into something like turn by turn directions. What method do you use?
This is how I make the plugin work on a newly installed Fedora Workstation that have been updated, and have its graphic driver and related software installed.
After opening , I install the plugin.
I somehow see the information about error.
I do something to pip.
pip install --upgrade pip
pip install --force-reinstall networkx==2.3
This is how i run QGIS Desktop.
I see there is still error and click there to see more information.
I can see above the line mentioning the Python version an error. Above that error I see it says it is in a file called dag.py in line 23.
I click on the option to show hidden files and then go to that file so I can edit it.
I click on the thing to shows the line number..
I change the writing in line 23 to
from math import gcd
I click on the thing that looks the letter x on the top right.
I run QGIS Desktop again and see that error is gone.
I click that so the hidden file is not shown anymore.
Following modification to postman.py
also fixes the error. nx.connected_component_subgraphs()
is deprecated, thus causing the error.
def graph_components(graph):
# The graph may contain multiple components, but we can only handle one connected component. If the graph contains
# more than one connected component, we only use the largest one.
components = list((graph.subgraph(c) for c in nx.connected_components(graph))) #list(nx.connected_component_subgraphs(graph)) <-- this is deprecated
components.sort(key=lambda c: c.size(), reverse=True)
return components