Ynjxsjmh
Ynjxsjmh

Reputation: 30042

AttributeError: partially initialized module x has no attribute y (most likely due to a circular import)

I want to use pandas to process a csv file. The main job is to duplicate a column, so I name the script file as copy.py.

import pandas as pd


df = pd.read_csv('latex.csv')

However, when I execute the file, it gets the error

$ python copy.py
Traceback (most recent call last):
  File "~/sourcecode/rime-math/copy.py", line 1, in <module>
    import pandas as pd
    import pandas as pd
  File "~/.local/lib/python3.10/site-packages/pandas/__init__.py", line 50, in <module>
    from pandas.core.api import (
  File "~/.local/lib/python3.10/site-packages/pandas/core/api.py", line 48, in <module>
    from pandas.core.groupby import (
  File "~/.local/lib/python3.10/site-packages/pandas/core/groupby/__init__.py", line 1, in <module>
    from pandas.core.groupby.generic import (
  File "~/.local/lib/python3.10/site-packages/pandas/core/groupby/generic.py", line 73, in <module>
    from pandas.core.frame import DataFrame
  File "~/.local/lib/python3.10/site-packages/pandas/core/frame.py", line 129, in <module>
    from pandas.core import (
  File "~/.local/lib/python3.10/site-packages/pandas/core/generic.py", line 122, in <module>
    from pandas.core.describe import describe_ndframe
  File "~/.local/lib/python3.10/site-packages/pandas/core/describe.py", line 37, in <module>
    from pandas.core.reshape.concat import concat
  File "~/.local/lib/python3.10/site-packages/pandas/core/reshape/concat.py", line 45, in <module>
    from pandas.core.internals import concatenate_managers
  File "~/.local/lib/python3.10/site-packages/pandas/core/internals/__init__.py", line 17, in <module>
    from pandas.core.internals.concat import concatenate_managers
  File "~/.local/lib/python3.10/site-packages/pandas/core/internals/concat.py", line 3, in <module>
    import copy
  File "~/sourcecode/rime-math/copy.py", line 4, in <module>
    df = pd.read_csv('latex.csv')
AttributeError: partially initialized module 'pandas' has no attribute 'read_csv' (most likely due to a circular import)

Upvotes: 2

Views: 18881

Answers (1)

Ynjxsjmh
Ynjxsjmh

Reputation: 30042

The problem is that the name of your script file is collision with the file one of your module wants to import.

Put attention on the last 5 lines of the traceback since it is near line where error occurs:

  File "~/.local/lib/python3.10/site-packages/pandas/core/internals/concat.py", line 3, in <module>
    import copy
  File "~/sourcecode/rime-math/copy.py", line 4, in <module>
    df = pd.read_csv('latex.csv')
AttributeError: partially initialized module 'pandas' has no attribute 'read_csv' (most likely due to a circular import)

Look at the first 2 lines, it means pandas module need to import copy module at somewhere.

According to The Module Search Path, Python interpreter will search copy module in built-in modules, current directory, PYTHONPATH etc. in order.

Apparently there is no built-in modules named copy in Python, so the interpreter will then look at the files under current directory and find there is copy.py.

The content of copy.py is just one line: df = pd.read_csv(). It means we need to use pandas module. However, we are just on the way importing pandas. Pandas here is only partially initialized, that's why you see that information in backtrace.

To solve this is easy, rename the script file copy.py to some others is ok.

Upvotes: 8

Related Questions