Dataclasses on python3.9(>=3.7)

Hi there!

I’m using/developing application using dataclasses on python3.9(>=3.7)

I Know dataclasses is new feature in python3.7, And we can use it without pip install on python3.7 or later.
(dataclasses — Data Classes — Python 3.12.2 documentation)

BTW, I tried to pip install dataclasses on python3.9, I got dataclasses==0.6
I couldn’t get dataclasses==0.8

$ python3.9 -m venv env01
$ source env01/bin/activate
(env01) $
(env01) khwarizmi@laptop-alma9:~/test/dataclasses$ python -m pip install dataclasses
Collecting dataclasses
  Using cached dataclasses-0.6-py3-none-any.whl (14 kB)
Installing collected packages: dataclasses
Successfully installed dataclasses-0.6
(env01) $ python -m pip freeze
dataclasses==0.6
(env01) $ python -m pip install --upgrade dataclasses==0.8
ERROR: Could not find a version that satisfies the requirement dataclasses==0.8 (from versions: 0.1, 0.2, 0.3, 0.4, 0.5, 0.6)
ERROR: No matching distribution found for dataclasses==0.8

Next, I made below setup.py and tried to install dataclasses using setup.py

from setuptools import setup

setup(
    name='test',
    version='0.0.1',
    author='me',
    author_email='me@example.com',
    description='test for dataclasses==0.8 install',
    py_modules=['test'],
    scripts=['test.py'],
    install_requires=[
        'dataclasses',
    ],
)

Then, I got dataclasses==0.8.

$ python3.9 -m venv env02
$ source env02/bin/activate
(env02) $ python setup.py install
running install
running bdist_egg
running egg_info
creating test.egg-info
writing test.egg-info/PKG-INFO
writing dependency_links to test.egg-info/dependency_links.txt
writing requirements to test.egg-info/requires.txt
writing top-level names to test.egg-info/top_level.txt
writing manifest file 'test.egg-info/SOURCES.txt'
reading manifest file 'test.egg-info/SOURCES.txt'
writing manifest file 'test.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build
creating build/lib
copying test.py -> build/lib
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
copying build/lib/test.py -> build/bdist.linux-x86_64/egg
byte-compiling build/bdist.linux-x86_64/egg/test.py to test.cpython-39.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
installing scripts to build/bdist.linux-x86_64/egg/EGG-INFO/scripts
running install_scripts
running build_scripts
creating build/scripts-3.9
copying test.py -> build/scripts-3.9
changing mode of build/scripts-3.9/test.py from 644 to 755
creating build/bdist.linux-x86_64/egg/EGG-INFO/scripts
copying build/scripts-3.9/test.py -> build/bdist.linux-x86_64/egg/EGG-INFO/scripts
changing mode of build/bdist.linux-x86_64/egg/EGG-INFO/scripts/test.py to 755
copying test.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying test.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying test.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying test.egg-info/requires.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying test.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist/test-0.0.1-py3.9.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing test-0.0.1-py3.9.egg
Copying test-0.0.1-py3.9.egg to /home/khwarizmi/test/dataclasses/env02/lib/python3.9/site-packages
Adding test 0.0.1 to easy-install.pth file
Installing test.py script to /home/khwarizmi/test/dataclasses/env02/bin

Installed /home/khwarizmi/test/dataclasses/env02/lib/python3.9/site-packages/test-0.0.1-py3.9.egg
Processing dependencies for test==0.0.1
Searching for dataclasses
Reading https://pypi.org/simple/dataclasses/
Downloading https://files.pythonhosted.org/packages/fe/ca/75fac5856ab5cfa51bbbcefa250182e50441074fdc3f803f6e76451fab43/dataclasses-0.8-py3-none-any.whl#sha256=0201d89fa866f68c8ebd9d08ee6ff50c0b255f8ec63a71c16fda7af82bb887bf
Best match: dataclasses 0.8
Processing dataclasses-0.8-py3-none-any.whl
Installing dataclasses-0.8-py3-none-any.whl to /home/khwarizmi/test/dataclasses/env02/lib/python3.9/site-packages
Adding dataclasses 0.8 to easy-install.pth file

Installed /home/khwarizmi/test/dataclasses/env02/lib/python3.9/site-packages/dataclasses-0.8-py3.9.egg
Finished processing dependencies for test==0.0.1
(env02) $ python -m pip freeze
dataclasses==0.8
test==0.0.1
  1. What is the difference between standard dataclasses and installed dataclasses?
  2. When dataclasses are installed, which dataclasses are used, the standard ones or the installed ones? Is there a way to check which ones are being used?
  3. Why I cannot get dataclasses==0.8 using pip install? Despite that, why I can get dataclasses==0.8 using setup.py?

Where did these pythons come from in the first place?

Some Linux distro package managers like Debian’s apt-get re-package the offiical Python installer, and for whatever reason make things like venv and pip an optional extra. In that case apt-get is required to install the ‘extra’ core libraries.

Thank you for your fast reply!!

I’m using AlmaLinux9.
And the python package comes from AlmaLinux9 standard repository.

$ cat /etc/almalinux-release
AlmaLinux release 9.3 (Shamrock Pampas Cat)
$ which python3.9
/usr/bin/python3.9
$ rpm -qf /usr/bin/python3.9
python3-3.9.18-1.el9_3.x86_64
$ dnf info python3-3.9.18-1.el9_3.x86_64 2>/dev/null
Last metadata expiration check: 0:00:15 ago on Thu 07 Mar 2024 11:31:30 PM JST.
Installed Packages
Name         : python3
Version      : 3.9.18
Release      : 1.el9_3
Architecture : x86_64
Size         : 32 k
Source       : python3.9-3.9.18-1.el9_3.src.rpm
Repository   : @System
From repo    : baseos
Summary      : Python 3.9 interpreter
URL          : https://www.python.org/
License      : Python
Description  : Python 3.9 is an accessible, high-level, dynamically typed, interpreted
             : programming language, designed with an emphasis on code readability.
             : It includes an extensive standard library, and has a vast ecosystem of
             : third-party libraries.
             :
             : The python3 package provides the "python3" executable: the reference
             : interpreter for the Python language, version 3.
             : The majority of its standard library is provided in the python3-libs package,
             : which should be installed automatically along with python3.
             : The remaining parts of the Python standard library are broken out into the
             : python3-tkinter and python3-test packages, which may need to be installed
             : separately.
             :
             : Documentation for Python is provided in the python3-docs package.
             :
             : Packages containing additional libraries for Python are generally named with
             : the "python3-" prefix.

On my environment, I can find below packages related pip

$ rpm -qa | grep python3-pip
python3-pip-wheel-21.2.3-7.el9.noarch
python3-pip-21.2.3-7.el9.noarch

I guess python3-pip-wheel-21.2.3-7.el9.noarch is extra libraries you mentioned, isn’t it?

$ dnf info python3-pip-wheel
Last metadata expiration check: 0:12:04 ago on Thu 07 Mar 2024 11:31:30 PM JST.
Installed Packages
Name         : python3-pip-wheel
Version      : 21.2.3
Release      : 7.el9
Architecture : noarch
Size         : 1.2 M
Source       : python-pip-21.2.3-7.el9.src.rpm
Repository   : @System
From repo    : baseos
Summary      : The pip wheel
URL          : https://pip.pypa.io/
License      : MIT and Python and ASL 2.0 and BSD and ISC and LGPLv2 and MPLv2.0 and (ASL 2.0
             : or BSD)
Description  : A Python wheel of pip to use with venv.

Search the package manager for everything related for python3.

It can be difficult to research all Python 3 related packages, so I would like to ask the following.
Is there a files or directories that specifies the reference repository when installing Python packages using pip or setup.py?
If so, I can use the following command to search for packages:
dnf provides </path/to/confdir>/*

And, if there is a pip option displays the referenced repositories, like dnf repolist please let me know.

I’ve used a lot of distros, but not Alma, so can’t help you unfortunately.

If noone else can help you here, you could ask on the Alma Linux forums how to install Python 3.12

OK. Then, I’ll change my question.

See, latest dataclasses page on Pypi: dataclasses · PyPI
We can see below requires

Requires: Python >=3.6, <3.7

There is also datacalasses==0.6 page on Pypi: dataclasses · PyPI
But dataclasses==0.6 has no requires.

So, every python3.7 or later environment not only on AlmaLinux but also on every distro referencing Pypi, get dataclasses==0.6 by using pip install dataclasses . I guess.

BUT

When I use setup.py on my environment, I could install dataclasses==0.8 .
I guess, setup.py(setuptools) has a bug where it doesn’t recognize requires. (or It’s default behaviour…)

However, this might be caused by my distribution environment. because I don’t know how to check what reposities I reference on my environment.
I would like to reproduce this in an environment that references only Pypi.

So, could you please check if the same phenomenon can be reproduced in your distoribution environment (referencing only Pypi) ?

I don’t know how those distros are made, but in a Python 3.7 distros people should not try to install dataclasses from the Pypi, since dataclasses is already part of the Python stdlib. If you do install it, it will by default not be used – the stdlib module will be used. You can check this by doing the pip install of dataclasses==0.6 and then running:

import dataclasses
print(dataclasses.__file__)  # will point to stdlib file
print(dataclasses.__version__)  # will raise an exception for the stdlib package

Your setup.py script runs without error and dumps a copy of the latest Pypi dataclasses (0.8) into the site-packages directory. But then that package will still not be used (fortunately) when you actually import dataclasses. I’m not 100% sure, but I think the setup.py install runs fine, since you didn’t put any restrictions on the dataclasses package. Don’t know if that should be seen as a bug or not - I agree it is a bit unexpected and less than ideal …

2 Likes

Thanks Hans!!

Your replies were very helpful and informative.
My questions on my first post have been answered. Thank you so much!!