3

in ubuntu 18.04, when i change default python from python 3.6 to other version by this command:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1

or when i remove python 3.6 and install other version netplan apply not working and result this error:

  File "/usr/sbin/netplan", line 20, in <module>
    from netplan import Netplan
  File "/usr/share/netplan/netplan/__init__.py", line 18, in <module>
    from netplan.cli.core import Netplan
  File "/usr/share/netplan/netplan/cli/core.py", line 24, in <module>
    import netplan.cli.utils as utils
  File "/usr/share/netplan/netplan/cli/utils.py", line 25, in <module>
    import netifaces
ModuleNotFoundError: No module named 'netifaces'

and command pip install netifaces has some errors.

Ali Mohammadi
  • 1,306
  • 1
  • 14
  • 28

3 Answers3

4

I faced the same issue before with vagrant. If you use update-alternatives to make python3 alias points to another version of Python, vagrant will not work. You cannot use update-alternatives to change the alias of Python3.

Ahmed
  • 2,825
  • 1
  • 25
  • 39
1

For some reason I lost my python configuration after an update made by my ubuntu server,solved by:

Checking if I could import the module so on CLI:

python
import python

After getting the same message I realize my python environment was falling to use the modules even when it all showed up as installed I went ahead and "upgrade" the python modules:

python pip install --upgrade pip

python pip install --upgrade netifaces

python pip install --upgrade "any module you need to use for your script"

And just like that modules were recognized updated and properly install.

Reza Rahemtola
  • 1,182
  • 7
  • 16
  • 30
Stash
  • 11
  • 1
0

tl;dr: Create a link netifaces.so to netifaces.cpython-36m-x86_64-linux-gnu.so in /usr/lib/python3/

In /usr/lib/python3/dist-packages/ is only a module for python3.6:

vagrant@ubuntu18.04:~$ find /usr/lib/python3/dist-packages/ -type f -name netifaces\*
/usr/lib/python3/dist-packages/netifaces.cpython-36m-x86_64-linux-gnu.so

That's why importing 'netifaces' failes for python3.8 while it works for python3.6:

vagrant@ubuntu18.04:~$ python3.6 -c 'import netifaces; print("works!")'
works!
vagrant@ubuntu18.04:~$ python3.8 -c 'import netifaces; print("works!")'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'netifaces'

One can link to it with a more unspecific name so python3.8 can find and use it:

vagrant@ubuntu18.04:~$ sudo ln -s /usr/lib/python3/dist-packages/netifaces.cpython-36m-x86_64-linux-gn
u.so /usr/lib/python3/dist-packages/netifaces.so
vagrant@ubuntu18.04:~$ python3.8 -c 'import netifaces; print("works!")'
works!

Hint: I had to do the same for apt_pkg.soapt_pkg.cpython-36m-x86_64-linux-gnu.so

dtrv
  • 693
  • 1
  • 6
  • 14