networkx installed but cannot import

5.6k views Asked by At

I have installed networkx but when I tried to import it in jupyter notebook, an error shown

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-16-6743fca41c5c> in <module>
      1 import numpy as np
      2 import matplotlib.pyplot as plt
----> 3 import networkx as nx

ModuleNotFoundError: No module named 'networkx'

I have uninstalled and reinstalled it several times but nothing changed.

It worked when I import it in terminal.

(base) MacBook-Pro:~ gary$ python
Python 3.7.7 (default, Mar 26 2020, 10:32:53) 
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import networkx
>>> print(networkx.__version__)
2.5

Can anyone help me with this?

2

There are 2 answers

7
BeeFriedman On BEST ANSWER

You need to write it with an exclamation mark. ! pip install [package_name]

0
R_Z On

Activate another env and switch back. This works for me.