Python3 tied to older version - python

Background: I'm currently running Elementary OS Hera(Ubuntu 18.04 LTS). The distribution came with Python 3.6.9. At some point I installed Python 3.7.5...this is when the issue(s) started.
Problem: I'm attempting to install PyQt5 which keeps defaulting to the older version for some reason. I ensured that Python3 was referencing the newer version:
Wick:~$ python3 --version
Python 3.7.5
I also ensured that python3.7 was the primary version:
Wick:~$ sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/python3.7 2 auto mode
1 /usr/bin/python3.6 1 manual mode
* 2 /usr/bin/python3.7 2 manual mode
BUT when I run sudo apt-get install -y python3-pyqt5 . It still continues to install to the 3.6 version. This can be verified via:
:~$ python3 -c "from PyQt5.QtCore import QSettings; print('done')"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'PyQt5.QtCore'
AND the kicker
:~$ python3.6 -c "from PyQt5.QtCore import QSettings; print('done')"
done
OR
:~$ python3.7 -c "from PyQt5.QtCore import QSettings; print('done')"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'PyQt5.QtCore'
Any assistance you can offer is greatly appreciated. I'm losing my mind.
p.s. apologies in advance for being a newbie

The package only depends on python3.6: https://packages.ubuntu.com/bionic/python3-pyqt5. It will be installed because it is a prerequisite.

This was resolved by #chepner:
"I would recommend using a virtual environment created with /usr/bin/python3.7, >then install pyqt5 there using pip rather than trying to install it via your >package manager."
Thanks so much!

Related

Trying to resolve ModuleNotFoundError: No module named

Trying to install third party modules on my mac using pip and I am getting this error (Using IDLE as the Python shell):
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import pyperclip
ModuleNotFoundError: No module named 'pyperclip'
(Have tried multiple modules and all return the same error)
I have installed the modules using:
pip3 install pyperclip
I can see that it has installed into the directory:
/opt/homebrew/lib/python3.9/site-packages
Ive tried going through several of the posts posted on StackOverflow such as this and this but after trying their steps I cant seem to resolve it.
which -a pip3 gives:
/opt/homebrew/bin/pip3
/Library/Frameworks/Python.framework/Versions/3.9/bin/pip3
/usr/local/bin/pip3
/usr/bin/pip3
which -a python3 gives:
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
/usr/local/bin/python3
/usr/bin/python3
print(sys.version) gives:
3.9.1 (v3.9.1:1e5d33e9b9, Dec 7 2020, 12:44:01)
[Clang 12.0.0 (clang-1200.0.32.27)]
pip --version gives:
pip 22.1.2 from /opt/homebrew/lib/python3.9/site-packages/pip (python 3.9)
Running /usr/local/bin/python3 test1.py:
Traceback (most recent call last):
File "/Users/dxz/Downloads/test1.py", line 1, in <module>
import pyperclip
ModuleNotFoundError: No module named 'pyperclip'
Thanks
You can run this command for seeing pip3 uses which python:
pip3 --version
But i guess you are not choosing right python to run your .py file
if you run your .py like this:
python3 main.py
You must be sure pip3 installing libraries to 'python3' linked python or you can run your main.py file with one of pip3 linked python
I mean if pip3 --version gives you
/usr/bin/python3
this python path you can run your main.py like this
/usr/bin/python3 main.py
but if you want to use best practice i recommend to you using virtualenv

netplan not working after change default python

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.
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.
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.
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.so → apt_pkg.cpython-36m-x86_64-linux-gnu.so

ImportError: cannot import name '_gi' from partially initialized module 'gi' (most likely due to a circular import)

Looks like I have broken my python installation when I wanted to switch to python 3.8. Using Ubuntu 18.04. Trying to use the gi, gives the following error:
$ python
Python 3.8.1 (default, Dec 31 2019, 18:42:42)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gi.repository import GLib, Gio
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/gi/__init__.py", line 42, in <module>
from . import _gi
ImportError: cannot import name '_gi' from partially initialized module 'gi' (most likely due to a circular import) (/usr/lib/python3/dist-packages/gi/__init__.py)
Tried running update-alternatives for python, but it tells me there is only one python alternative configured (3.8).
Tried to reinstall python3-gi and python3.8. Still the same problem
I had the same issue. I linked python3 to python3.6, for me it was pointing to 3.8. That solved the issue.
cd /usr/bin/
rm python3
ln -s python3.6 python3
Thats all. Now my system started working fine.
Install gi for python 3.8: python3.8 -m pip install pgi
Then, instead of import gi use:
import pgi
pgi.install_as_gi()
from gi.repository import GLib, Gio
Alternatively, you can force install PyGObject for python 3.8:
sudo python3.8 -m pip install --ignore-installed PyGObject
which should allow one to from gi import ... as before.
For me the workaround was to create a symlink:
cd /usr/lib/python3/dist-packages/gi/
sudo ln -s _gi.so _gi.cpython-38-x86_64-linux-gnu.so
and it solved the problem for me.
I had the same problem on ubuntu 18 as python3 was referring to python3.9.
In order to solve it, I changed the alternative for python3:
sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/python3.9 2 auto mode
1 /usr/bin/python3.6 1 manual mode
2 /usr/bin/python3.9 2 manual mode
By choosing number 1, now python3 points to python3.6 and everything works fine again
Found answer here https://bugzilla.redhat.com/show_bug.cgi?id=1709787:
The cause is - /usr/lib64/python3.8/site-packages/gi/_gi.cpython-38m-x86_64-linux-gnu.so has incorrect name:
sh-5.0# python3 -c 'from gi.repository import GLib'
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib64/python3.8/site-packages/gi/init.py", line 42, in
from . import _gi
ImportError: cannot import name '_gi' from 'gi' (/usr/lib64/python3.8/site-packages/gi/init.py)
sh-5.0# mv /usr/lib64/python3.8/site-packages/gi/_gi.cpython-38m-x86_64-linux-gnu.so /usr/lib64/python3.8/site-packages/gi/_gi.cpython-38-x86_64-linux-gnu.so
sh-5.0# python3 -c 'from gi.repository import GLib'
Note that since 3.8.0a4, the "m" is not supposed to be there. Is it somehow hardcoded?
sh-5.0# python3-config --extension-suffix
.cpython-38-x86_64-linux-gnu.so
in my case it was
$ sudo ln -s /usr/lib/python3/dist-packages/gi/_gi.cpython-35m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/gi/_gi.cpython-38-x86_64-linux-gnu.so
$ sudo ln -s /usr/lib/python3/dist-packages/gi/_gi_cairo.cpython-35m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/gi/_gi_cairo.cpython-38-x86_64-linux-gnu.so
The reason of this error is that this app can't find the matched Python version of _gi_cairo.cpython-(version)-x86_64-linux-gnu.so.
Normally, this unmatched situation is caused by some wrong mixed usage of different versions of Python.
So basically, you can try to switch your Python version ( to the default version of your OS). Or you can go to '/usr/lib/python3/dist-packages/gi' and create a new .so library file:
cp _gi_cairo.cpython-(old version)-x86_64-linux-gnu.so _gi_cairo.cpython-(new version)-x86_64-linux-gnu.so
or
ln -s _gi_cairo.cpython-(old version)-x86_64-linux-gnu.so _gi.so
Have same issue, can't load module from script folder. In my case work if i changing workdir for example (but only for inline command):
cd /tmp; python -c 'from gocd import Server'
echo $?
0
After copy script to /tmp, it's does't work
cp gocd.py /tmp
cd /tmp; python -c 'from gocd import Server'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/gocd.py", line 3, in <module>
from gocd import Server
ImportError: cannot import name 'Server' from partially initialized module 'gocd' (most likely due to a circular import) (/tmp/gocd.py)

cannot import gi module python 3.6 on mac

I am integrating gstreamer and pocketsphinx on mac using python 3.6; however, the first line of code, from gi import pygtkcompat, raises an error.
The command:
python3 demoapp_chinese.py
returns
Traceback (most recent call last):
File "demoapp_chinese.py", line 1, in
from gi import pygtkcompat
ModuleNotFoundError: No module named 'gi'
Here's how I install pygobject:
brew install pygobject3
And when I try to get more info. I execute
brew info pygobject3
which returns
pygobject3: stable 3.30.4 (bottled)
GNOME Python bindings (based on GObject Introspection)
https://wiki.gnome.org/Projects/PyGObject
/usr/local/Cellar/pygobject3/3.30.4 (69 files, 1.6MB) *
Poured from bottle on 2019-02-04 at 17:21:21
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/pygobject3.rb
(here I only post the first few couple of lines)
For the python info,
which python3
returns
/usr/local/bin/python3
Besides,
echo $PYTHONPATH
returns
/usr/local/lib/python3.6/site-packages
echo $PATH
returns
/Users/cindy/bin:/usr/local/bin/python3.6:/usr/local/lib/python3.6/site-packages:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public
Lastly, I don't know if this is normal, when I run
which pygobject3
NOTHING returns on the terminal.
Please help, thanks. If you need any extra info. to help me, please let me know.

Pip install is not working with python but working with python2

I have an issue with Python on my Mac with High Sierra.
After running pip install pysal, I have a successful installation. However, the module is not found when I run in python:
> import pysal
Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named pysal
I do not face such errors when running the same command in python2. However, most of my python scripts call the interpreter at the beginning of the file with #!/usr/bin/env python, so I would prefer not having to rely on python2 (I don't know how to do it either).
I figured out that this is python's executable is not on the same location where pip installs the modules:
macbook-pro-3:~ ME$ which -a python
/usr/bin/python
macbook-pro-3:~ ME$ which -a pip
/usr/local/bin/pip
I tried to follow the solution proposed by #J0ANMM in Modules are installed using pip on OSX but not found when importing . It recommeds downloading the script get-pip.py and executing it with sudo /usr/bin/python get-pip.py. I tried, but it did not change anything (the previous which commands still yield the same thing).
How do I manage to make pip work with my version of python?

Categories