Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

python - AttributeError: module 'networkx' has no attribute 'Graph'

I installed networkx by downloading the distribution file and running:

python setup.py install

It appears to have been successful, I got this message after installing

Installed c:pythonpython36-32libsite-packagesdecorator-4.0.11-py3.6.egg
Finished processing dependencies for networkx==1.11

But when I run a really simple test code, I get errors

import networkx as nx

G=nx.Graph()

print(G.nodes())
print(G.edges())

print(type(G.nodes()))
print(type(G.edges()))
> Traceback (most recent call last):   File "netExample.py", line 3, in
> <module>
>     G=nx.Graph() 

> AttributeError: module 'networkx' has no attribute 'Graph'

Running print(dr(nx)) gives the following attributes:

> ['GraphMLReader', 'GraphMLWriter', '__builtins__', '__cached__',
> '__doc__', '__file__', '__loader__', '__name__', '__package__',
> '__path__', '__spec__', 'exception', 'generate_adjlist',
> 'generate_edgelist', 'generate_gexf', 'generate_gml',
> 'generate_graph6', 'generate_graphml', 'generate_multiline_adjlist',
> 'generate_pajek', 'generate_sparse6', 'parse_adjlist',
> 'parse_edgelist', 'parse_gml', 'parse_graph6', 'parse_graphml',
> 'parse_leda', 'parse_multiline_adjlist', 'parse_pajek',
> 'parse_sparse6', 'read_adjlist', 'read_edgelist', 'read_gexf',
> 'read_gml', 'read_gpickle', 'read_graph6', 'read_graphml',
> 'read_leda', 'read_multiline_adjlist', 'read_pajek', 'read_shp',
> 'read_sparse6', 'read_weighted_edgelist', 'read_yaml', 'readwrite',
> 'relabel_gexf_graph', 'utils', 'write_adjlist', 'write_edgelist',
> 'write_gexf', 'write_gml', 'write_gpickle', 'write_graph6',
> 'write_graphml', 'write_multiline_adjlist', 'write_pajek',
> 'write_shp', 'write_sparse6', 'write_weighted_edgelist', 'write_yaml']
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Following the below steps, it worked for me in python 3.5 version.

  1. Downloaded networkx-1.11.zip
  2. Extracted the zip file
  3. open the cmd and cd to extracted directory
  4. run python setup.py install
  5. verified the installation using pip freeze
  6. saved the test code in netExample.py file.
  7. In CMD, cd to the folder, which contains netExample.py
  8. run python netExample.py

Following is output that I got:

D:Naveenso>python netExample.py
[]
[]
<class 'list'>
<class 'list'>

Please check the following:

  1. Verify whether you are downloading from the official website.
  2. Check if any other file named networkx.py is present in Windows Path before the actual networkx module is present in

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...