Python Forum
Imports that work with Python 3.8 fail with 3.9 and 3.10
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Imports that work with Python 3.8 fail with 3.9 and 3.10
#1
OS: Ubuntu 20.04
Python 3.8, 3.9, 3.10 built from source without errors.

Imports work as expected with Python 3.8:
$ python
Python 3.8.12 (default, Mar  3 2022, 04:28:02)
[GCC 9.3.0] on linux
>>> import gi
>>> gi.require_version('AyatanaAppIndicator3', '0.1')
>>> from gi.repository import AyatanaAppIndicator3
>>> AyatanaAppIndicator3
<IntrospectionModule 'AyatanaAppIndicator3' from '/usr/lib/x86_64-linux-gnu/girepository-1.0/AyatanaAppIndicator3-0.1.typelib'>
>>>
>>> gi.require_version('AppIndicator3', '0.1')
>>> from gi.repository import AppIndicator3
>>> AppIndicator3
<IntrospectionModule 'AppIndicator3' from '/usr/lib/girepository-1.0/AppIndicator3-0.1.typelib'>
>>>
The same imports give errors with Python 3.10 (same with 3.9):
$ python
Python 3.10.2 (main, Mar  3 2022, 03:40:42) [GCC 9.3.0] on linux
>>> import gi
>>> gi.require_version('AyatanaAppIndicator3', '0.1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/to/venv10/lib/python3.10/site-packages/gi/__init__.py", line 126, in require_version
    raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace AyatanaAppIndicator3 not available
>>> from gi.repository import AyatanaAppIndicator3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/to/venv10/lib/python3.10/site-packages/gi/importer.py", line 131, in load_module
    raise ImportError('cannot import name %s, '
ImportError: cannot import name AyatanaAppIndicator3, introspection typelib not found
>>>
>>> gi.require_version('AppIndicator3', '0.1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/to/venv10/lib/python3.10/site-packages/gi/__init__.py", line 126, in require_version
    raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace AppIndicator3 not available
>>> from gi.repository import AppIndicator3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/to/venv10/lib/python3.10/site-packages/gi/importer.py", line 131, in load_module
    raise ImportError('cannot import name %s, '
ImportError: cannot import name AppIndicator3, introspection typelib not found
>>>
How can I get the imports to work with Python 3.9/3.10?
Reply
#2
It look for that file external in OS,then is probably in sys.path of Python 3.8.
Open Python 3.8 and see what's in sys.path.
So can add to sys.path this is where Python look for .py or external file/dir.
Example:
tom@tom-VirtualBox:~$ python --version
Python 3.10.2

tom@tom-VirtualBox:~$ python 
Python 3.10.2 (main, Jan 31 2022, 15:25:53) [GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> 
>>> sys.path
['', '/home/tom/.pyenv/versions/3.10.2/lib/python310.zip', '/home/tom/.pyenv/versions/3.10.2/lib/python3.10', '/home/tom/.pyenv/versions/3.10.2/lib/python3.10/lib-dynload', '/home/tom/.pyenv/versions/3.10.2/lib/python3.10/site-packages']
>>> 
>>> # Add to path
>>> sys.path.append('/usr/lib/x86_64-linux-gnu/girepository-1.0/AyatanaAppIndicator3-0.1.typelib')
>>> # Test again
>>> sys.path
['', '/home/tom/.pyenv/versions/3.10.2/lib/python310.zip', '/home/tom/.pyenv/versions/3.10.2/lib/python3.10', '/home/tom/.pyenv/versions/3.10.2/lib/python3.10/lib-dynload', '/home/tom/.pyenv/versions/3.10.2/lib/python3.10/site-packages', '/usr/lib/x86_64-linux-gnu/girepository-1.0/AyatanaAppIndicator3-0.1.typelib']
# Now is same session try import again
>>> exit()
So sys.path.append is temporally to permanently look into PYTHONPATH or better use site module.
Here and older post about using site is for Windows but work the same on Linux.

4slam Wrote:Python 3.8, 3.9, 3.10 built from source without errors
Can advice to look into pyenv Simple Python Version Management.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  simple if fail, what am I missing? ajkrueger25 2 746 Nov-13-2024, 04:21 AM
Last Post: ajkrueger25
  Best programming practice for imports Curbie 8 1,607 Oct-16-2024, 02:20 AM
Last Post: Curbie
  Sudden Extremely Slow / Failed Python Imports bmccollum 1 1,082 Aug-20-2024, 02:09 PM
Last Post: DeaD_EyE
  keeping logs for every success fail attempt robertkwild 22 5,388 Jul-19-2024, 03:49 PM
Last Post: robertkwild
  Why does [root.destroy, exit()]) fail after pyinstaller? Rpi Edward_ 4 1,763 Oct-18-2023, 11:09 PM
Last Post: Edward_
  How to calculated how many fail in each site(s) in csv files SamLiu 4 2,286 Sep-26-2022, 06:28 AM
Last Post: SamLiu
  [SOLVED] Why does regex fail cleaning line? Winfried 5 3,485 Aug-22-2021, 06:59 PM
Last Post: Winfried
  scraping video src fail jacklee26 5 4,794 Jul-11-2021, 09:38 AM
Last Post: snippsat
  Imports in my first package cuppajoeman 1 2,490 Jun-28-2021, 09:06 AM
Last Post: snippsat
  script with imports works but pytest gives "ModuleNotFoundError"? Hpao 0 2,109 Jun-27-2021, 08:30 PM
Last Post: Hpao

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020