dnf installs python3 modules only for platform-python - python

I'm currently working on an Ansible project which relies on python. For the ansbile package module the python3-dnf module is required.
On the destination Server (AlmaLinux 8.6) python3-dnf is already installed:
$ dnf install python3-dnf
Last metadata expiration check: 3:36:12 ago on Thu 06 Oct 2022 07:42:20 AM CEST.
Package python3-dnf-4.7.0-8.el8.alma.noarch is already installed.
Dependencies resolved.
Nothing to do.
Complete!
I found out, that python3-dnf is only installed for the platform-python version 3.6:
$ ls -l /usr/lib/python3.6/site-packages/
[...]
drwxr-xr-x. 9 root root 4.0K Oct 6 11:08 dnf
drwxr-xr-x. 3 root root 4.0K May 10 19:36 dnf-plugins
drwxr-xr-x. 3 root root 44 Sep 5 10:29 dnfpluginscore
$ /usr/libexec/platform-python
Python 3.6.8 (default, Sep 13 2022, 07:19:15)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-10)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dnf
>>>
Because when I try to import dnf with my latest installed python version 3.9:
Python 3.9.7 (default, Apr 21 2022, 08:39:11)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-10)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dnf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'dnf'
So here is my question:
Is it possible to tell dnf ( dnf install python3-dnf ) to insall python modules not for the platform-python version but for the latest python version intalled or specify a python version?
Thanks

I found out, that python3-dnf is only installed for the platform-python version 3.6:
dnf can only install Python modules for the version of Python distributed with AlmaLinux. If you dnf install python3, you'll have a /usr/bin/python3 binary that can see Python modules installed using dnf.
If you're installing Python 3.9 via some other mechanism, you'll need to manually manage your dependencies.
I'm currently working on an Ansible project which relies on python.
Since you're using Ansible, one option is to simply use the platform-python version by setting ansible_python_interpreter=/usr/libexec/platform-python.

Related

How to downgrade to python 2.7.5 and make it as default in CentOs7?

CentOS7 comes with python 2.7.5 and I followed https://myopswork.com/install-python-2-7-10-on-centos-rhel-75f90c5239a5 and installed python 2.7.10 to meet yugabyte db installation pre-requisites.
Executed the following commands, without considering consequences:
alias python="/usr/local/bin/python2.7"
ln -fs /usr/bin/python2.7 /usr/bin/python
alternatives --install /usr/bin/python python /usr/bin/python2.7 50
Now, python 2.7.10 became default:
[root#srvr0 ~]# python
Python 2.7.10 (default, Jan 27 2020, 17:09:56)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
rpm lists installed packages list contains, python-2.7.5
[root#srvr0 python]# rpm -qa | grep python
...
python-2.7.5-86.el7.x86_64
...
I am getting error while invoking yum command:
[root#srvr0 ~]# yum install mysql
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:
No module named yum
Please install a package which provides this module, or
verify that the module is installed correctly.
It's possible that the above module doesn't match the
current version of Python, which is:
2.7.10 (default, Jan 27 2020, 17:09:56)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
If you cannot solve this problem yourself, please go to
the yum faq at:
http://yum.baseurl.org/wiki/Faq
I tried installing python 2.7.5:
[root#srvr0 python]# rpm -ivh python-2.7.5-76.el7.x86_64.rpm
error: Failed dependencies:
python-libs(x86-64) = 2.7.5-76.el7 is needed by python-2.7.5-76.el7.x86_64
python < 2.7.5-86.el7 conflicts with (installed) python-devel-2.7.5-86.el7.x86_64
[root#srvr0 python]#
Please help me in getting back the default python 2.7.5 along with required default packages/modules.
Maybe you can try uninstall orginal python version , and reinstall your specific python version ,i have forget the exact download command in CentOs7 , but in ubuntu it is sudo apt-get install python==2.7.5
after you install , try to use ln -s python2.7.5 python,so that every time you call python , it is python version 2.7.5

lsb_release not working after install Python 3.6.3 from source

platform: ubuntu 17.04 server
The ubuntu 17.04 server installation includes python 2.7 and python 3.5.
I installed the Python 3.6.3 manually from source. However, the lsb_release -a failed:
root#birds:~# lsb_release -a
Traceback (most recent call last):
File "/usr/bin/lsb_release", line 25, in <module>
import lsb_release
ModuleNotFoundError: No module named 'lsb_release'
But if I modify the first line of the file lsb_release from
#!/usr/bin/python3 -Es
to
#!/usr/bin/python3.5 -Es
it works again.
root#birds:~# lsb_release -a
LSB Version: core-9.20160110ubuntu5-amd64:core-9.20160110ubuntu5-noarch:security-9.20160110ubuntu5-amd64:security-9.20160110ubuntu5-noarch
Distributor ID: Ubuntu
Description: Ubuntu 17.04
Release: 17.04
Codename: zesty
Here are the module search path:
python3.5
root#birds:~# python3.5
Python 3.5.3 (default, Sep 14 2017, 22:58:41)
[GCC 6.3.0 20170406] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages']
>>> import lsb_release
>>> exit()
python3
root#birds:~# python3
Python 3.6.3 (default, Oct 14 2017, 20:35:42)
[GCC 6.3.0 20170406] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6', '/usr/local/lib/python3.6/lib-dynload', '/root/.local/lib/python3.6/site-packages', '/usr/local/lib/python3.6/site-packages']
>>> import lsb_release
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'lsb_release'
>>> exit()
does anyone know how to fix it? Thanks.
Solution:
sudo ln -s /usr/share/pyshared/lsb_release.py /usr/local/lib/python3.6/site-packages/lsb_release.py
Explain:
We can see in /usr/bin/lsb_release
#!/usr/bin/python3 -Es
# lsb_release command for Debian
# (C) 2005-10 Chris Lawrence <lawrencc#debian.org>
# This package is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 dated June, 1991.
# This package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this package; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA
from optparse import OptionParser
import sys
import os
import re
import lsb_release
The key step is import lsb_release, but the problem is Python 3.6 doesn't have this module.
So, you must have overrided python3 from python3.5 to python3.6. That's why your lsb_release is broken.
To verify it, we can see in python3.6:
➜ ~ python3.6
Python 3.6.4 (default, Feb 6 2018, 16:57:12)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import lsb_release
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'lsb_release'
then in python3.5:
➜ ~ python3.5
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import lsb_release
>>> lsb_release.__file__
'/usr/lib/python3/dist-packages/lsb_release.py'
where is this file:
➜ ~ ll /usr/lib/python3/dist-packages/lsb_release.py
lrwxrwxrwx 1 root root 38 Jul 7 2016 /usr/lib/python3/dist-packages/lsb_release.py -> ../../../share/pyshared/lsb_release.py
So, this module lsb_release exist in python3.5 but not exist in python3.6. And We find it eventually!
Now let's fix it through add a link to the original lsb_release.py file!
It works for me!
This happens when dist-packages is wiped out or not accessible to the python installation.
I got into this when I removed a dist-packages/ in /usr/lib/python3 - because there were some user-installed packages conflicting with my local packages.
To fix, you can try:
sudo apt reinstall lsb-release
This fixed it for me. It also avoids the issue with the sibling answer of purging lsb-release. No need.
I had this same problem with python 3.6 and python 3.7 on Raspberry Pi, but I thing it shall work anywhere.
The only thing that works like a charm was to move the lsb_release to a backup file.
sudo mv /usr/bin/lsb_release /usr/bin/lsb_release_back
The complete doc can be found here: [readthedocs.io].[1]https://neoctobers.readthedocs.io/en/latest/rpi/install_python3.html
You just purge existing lsb-release and reinstall it again. It will solve your problem.
$ sudo apt purge lsb-release
$ sudo apt install lsb-release

Upgrade an apt installed Python package in /usr/lib?

Is it possible to upgrade a package installed with apt-get, so located in /usr/lib/ , if such package do have a more recent version in pypi but not within the standard Ubuntu repositories as seen by apt?
I guess it is dangerous as it may break dependencies, but it's just to know.
Yes, it is.
I uninstall Flask
$ sudo apt-get remove python-flask
I don't have it:
$ python
Python 2.7.13 (default, Jan 19 2017, 14:48:08)
[GCC 6.3.0 20170118] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named flask
I install it
$ sudo apt-get install python-flask
$ python
Python 2.7.13 (default, Jan 19 2017, 14:48:08)
[GCC 6.3.0 20170118] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
>>> flask.__version__
'0.12'
Double-check:
$ pip list -o | grep Flask
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
Flask (0.12.1) - Latest: 0.12.2 [wheel]
Upgrading:
$ sudo pip install --upgrade Flask
...
Successfully installed Flask-0.12.2 Jinja2-2.9.6 MarkupSafe-1.0 Werkzeug-0.12.2 click-6.7 itsdangerous-0.24
$ python
Python 2.7.13 (default, Jan 19 2017, 14:48:08)
[GCC 6.3.0 20170118] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
>>> flask.__version__
'0.12.2'
I see I have an issue with pip check, however:
$ pip list -o | grep Flask
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
Flask (0.12.1) - Latest: 0.12.2 [wheel]
So I must have some links or something broken, but this issue is still alive even if I use apt-get remove. All in all I am able to import newer version of Flask which is what you need I guess.
EDIT
OK, the issue is that pip installs Flask in different location then apt-get. This is pip output:
>>> flask.__file__
'/usr/local/lib/python2.7/dist-packages/flask/__init__.pyc'
And this is apt-get's:
>>> flask.__file__
'/usr/lib/python2.7/dist-packages/flask/__init__.pyc'
Here is a description of how to make pip install you package in a different directory. I have not tested it, however.

Install NCurses on python3 for Ubuntu

I'm having issues installing ncurses for Python3. When I did the normal sudo apt-get install ncurses-dev, it appeared to install for Python2 but when I try to run my script for Python3, it says.
ImportError: No module named curses
How would you get ncurses to work for Python3?
I had this same problem. The issue was that ncurses was not installed on my Ubuntu installation. To fix it, I ran:
sudo apt-get install libncurses-dev
and then reinstalled Python. In my case with:
pyenv install 3.8.1
Answering y when asked continue with installation? (y/N)
This fixed the problem.
Try this:
import curses
curses is ncurses. It's also built in to python, there's nothing to install.
Welcome to Ubuntu 14.04.3 LTS (GNU/Linux 3.13.0-65-generic x86_64)
* Documentation: https://help.ubuntu.com/
Last login: Mon Oct 19 19:06:03 2015 from xxx.xxx.xxx.xxx
me#ubuntu:~$ python3
Python 3.4.0 (default, Jun 19 2015, 14:20:21)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import curses
>>>

Python3 has no acces to python2 modules (ubuntu)

Im fairly new to programming and Ubuntu. Yesterday I finally managed to create a dual-boot system, so now I'm running Ubuntu 12.04 LTS.
For a school project, I need to work in Python3 with a module called SPARQLWrapper (https://pypi.python.org/pypi/SPARQLWrapper).
On my freshly installed Ubuntu, I've installed the latest version of Python. When I type "python3" in my terminal, python 3.2.3 starts so thats good.
I installed easy_install (sudo apt-get install python-setuptools), and downloaded and installed the SPARQLWrapper egg file (sudo easy_install SPARQLWrapper-1.5.2-py3.2).
If I run python2 and use "import SPARQLWrapper", it just works. But if I try the same in python3 it gives me the following error:
x#ubuntu:~$ python3
Python 3.2.3 (default, Oct 19 2012, 20:10:41)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import SPARQLWrapper
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named SPARQLWrapper
So my problem is that python3 isn't able to acces the same modules as my python2. How do I fix this?
Thanks!
To install packages for Python3, you need python3's setuptools.
Following are the steps to be followed to install python3's setuptools and SPARQLWrapper
sudo apt-get install python3-setuptools
sudo easy_install3 pip
pip -V This should show the pip corresponding to your python3 installation.
sudo pip install SPARQLWrapper
After doing the above mentioned steps, I get this
~$ python3
Python 3.3.1 (default, Apr 17 2013, 22:30:32)
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import SPARQLWrapper
>>> exit()
~$
Each Python installation has its own modules directory. In addition, Python 3 is not backwards compatible and won't generally run Python 2 code. You'll need to find a Python 3 version of the module you need and install it for Python 3.

Categories

Resources