简体   繁体   中英

networkx installed but cannot import: No module named 'networkx'

I installed networkx with pip using this command:

[JohnRambo@Machine ~]$ pip install networkx

Just to show you, by using pip install networkx , it says it is already installed:

Requirement already satisfied: networkx in ./anaconda3/lib/python3.7/site-packages (2.6.2)

However, when I try to import networkx into python

import networkx as nx

I get this message:

ModuleNotFoundError: No module named 'networkx'

In addition, also by trying with ( pytest networkx )

[JohnRambo@Machine ~]$ pytest networkx

I get the error file not found: networkx , as shown here below:

============================= test session starts ==============================
platform linux -- Python 3.7.7, pytest-3.8.0, py-1.6.0, pluggy-0.7.1
rootdir: /home/JohnRambo, inifile:
plugins: remotedata-0.3.0, openfiles-0.3.0, doctestplus-0.1.3, arraydiff-0.2

========================= no tests ran in 0.01 seconds =========================
ERROR: file not found: networkx

Any idea on how to fix it?

Additional information, if it can be useful:

[JohnRambo@Machine ~]$ python -V
Python 3.7.7

[JohnRambo@Machine ~]$ python3 -V 
Python 3.7.7

[JohnRambo@Machine ~]$ pip --version 
pip 21.1.3 from /home/JohnRambo/anaconda3/lib/python3.7/site-packages/pip (python 3.7) 

[JohnRambo@Machine ~]$ pip3 --version 
pip 21.1.3 from /home/JohnRambo/anaconda3/lib/python3.7/site-packages/pip (python 3.7)

[JohnRambo@Machine ~]$ which python 
~/anaconda3/bin/python 

[JohnRambo@Machine ~]$ which python3 
~/anaconda3/bin/python3 

[JohnRambo@Machine ~]$ which pip 
~/anaconda3/bin/pip 

[JohnRambo@Machine ~]$ which pip3 
~/anaconda3/bin/pip3

[JohnRambo@Machine ~]$ which pytest 
~/anaconda3/bin/pytest

I have a sort of answer, but I do not think it is really good...

By following the NoModuleNamed guide , I first checked the sys.executable in python

import sys
print(sys.executable)

which gave this:

/bin/python3

Then, I tried to install networkx there, by using the instructions given both in the NoModuleNamed guide and in installation guide of networkx :

[JohnRambo@Machine ~]$ /bin/python3 -m pip install networkx[default]

Just for information, I got this (after installation):

Requirement already satisfied: networkx[default] in ./.local/lib/python3.6/site-packages (2.5.1)
WARNING: networkx 2.5.1 does not provide the extra 'default'
Requirement already satisfied: decorator<5,>=4.3 in ./.local/lib/python3.6/site-packages (from networkx[default]) (4.4.2)

Now, the command import networkx inside python does not give me any error, but

  1. if I check the networkx installation from Terminal [JohnRambo@Machine ~]$ pytest networkx , I still get ERROR: file not found: networkx , and
  2. that version of networkx is the 2.5.1 while I would like the 2.6 one.

Once I try to upgrade networkx through the command

[JohnRambo@Machine ~]$ /bin/python3 -m pip install --upgrade networkx[default]

I get this message:

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: networkx[default] in ./.local/lib/python3.6/site-packages (2.5.1)
WARNING: networkx 2.5.1 does not provide the extra 'default'
Requirement already satisfied: decorator<5,>=4.3 in ./.local/lib/python3.6/site-packages (from networkx[default]) (4.4.2)

Is it still OK to install within /bin/python3 or it is not a correct practice?

How can I upgrade my networkx?

while in a virtual env, do the command pipenv install networkx because for some reason pip install sometimes doesn't recognize pipenv commands.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM