Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configparser.MissingSectionHeaderError: File contains no section headers. #520

Closed
yejue opened this issue Jan 11, 2025 · 5 comments
Closed
Assignees

Comments

@yejue
Copy link

yejue commented Jan 11, 2025

I encountered a very strange problem. I just used the following code to import pydantic_settings.

from pydantic_settings import BaseSettings
print("hello world")

But the following error was displayed:

root@ubuntu:~/projects/SightlessGuide2# python test.py 
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from pydantic_settings import BaseSettings
  File "/usr/local/lib/python3.8/dist-packages/pydantic_settings/__init__.py", line 1, in <module>
    from .main import BaseSettings, CliApp, SettingsConfigDict
  File "/usr/local/lib/python3.8/dist-packages/pydantic_settings/main.py", line 14, in <module>
    from .sources import (
  File "/usr/local/lib/python3.8/dist-packages/pydantic_settings/sources.py", line 40, in <module>
    from pydantic import AliasChoices, AliasPath, BaseModel, Json, RootModel, TypeAdapter
  File "<frozen importlib._bootstrap>", line 1039, in _handle_fromlist
  File "/usr/local/lib/python3.8/dist-packages/pydantic/__init__.py", line 421, in __getattr__
    module = import_module(module_name, package=package)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/local/lib/python3.8/dist-packages/pydantic/root_model.py", line 35, in <module>
    class RootModel(BaseModel, typing.Generic[RootModelRootType], metaclass=_RootModelMetaclass):
  File "/usr/local/lib/python3.8/dist-packages/pydantic/_internal/_model_construction.py", line 224, in __new__
    complete_model_class(
  File "/usr/local/lib/python3.8/dist-packages/pydantic/_internal/_model_construction.py", line 620, in complete_model_class
    cls.__pydantic_validator__ = create_schema_validator(
  File "/usr/local/lib/python3.8/dist-packages/pydantic/plugin/_schema_validator.py", line 38, in create_schema_validator
    plugins = get_plugins()
  File "/usr/local/lib/python3.8/dist-packages/pydantic/plugin/_loader.py", line 39, in get_plugins
    for entry_point in dist.entry_points:
  File "/usr/lib/python3.8/importlib/metadata.py", line 240, in entry_points
    return EntryPoint._from_text(self.read_text('entry_points.txt'))
  File "/usr/lib/python3.8/importlib/metadata.py", line 100, in _from_text
    config.read_string(text)
  File "/usr/lib/python3.8/configparser.py", line 723, in read_string
    self.read_file(sfile, source)
  File "/usr/lib/python3.8/configparser.py", line 718, in read_file
    self._read(f, source)
  File "/usr/lib/python3.8/configparser.py", line 1082, in _read
    raise MissingSectionHeaderError(fpname, lineno, line)
configparser.MissingSectionHeaderError: File contains no section headers.

I wanted to look from the bottom to find out which f caused it and what it was. when I added print and reran it, the code was correct.
image

@yejue
Copy link
Author

yejue commented Jan 11, 2025

My System Info:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.6 LTS"

My Package Info:

pydantic               2.10.5              
pydantic-core          2.27.2              
pydantic-settings      2.6.1   

@hramezani
Copy link
Member

I couldn't reproduce the problem. can you try it in a fresh env?

@yejue
Copy link
Author

yejue commented Jan 13, 2025

I realized that I also made a mistake in the above code. I tried to directly print f.name, which might have caused an AttributeError. As a result, the code skipped from this position. Then, somewhere, this error was caught, so the code could run. So, I did the following operation in _read and directly threw an error.

image

I found that, just as I thought, there was a place in metadata.py that caught this error.

d0d14f18bf588b3641bb681ce43ee1d

@yejue
Copy link
Author

yejue commented Jan 13, 2025

I couldn't reproduce the problem. can you try it in a fresh env?

Yes, there seem to be some strange issues with my environment. When I tried to create a new environment using virtualenv, it showed me an AttributeError.

root@ubuntu:~/pythonenvs# virtualenv testenv
22222222222222222222222222
AttributeError: '_io.StringIO' object has no attribute 'name'

By the way, this is one of my devices with the ARM architecture. I've never encountered related problems on other devices.

@hramezani
Copy link
Member

I am not sure what the source of the problem is.

let's close the problem. but feel free to reopen it when you have more information about the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants