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

15,534

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:\Naveen\so>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
Share:
15,534
NBC
Author by

NBC

Updated on July 16, 2022

Comments

  • NBC
    NBC almost 2 years

    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:\python\python36-32\lib\site-packages\decorator-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']