2

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)
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
  • 1
    This looks like a frequently asked question, I clicked into nearly 10 similar questions but found none of them even posts the backtrace. I post this question mainly to help people analyse the backtrace. When I first encountered this error, I look back to the 5 fewer lines code and then wonder if my almost 300 Pandas answers have become yesterday memory. – Ynjxsjmh Feb 07 '22 at 16:34

1 Answers1

7

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.

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
  • Since this is a working answer for this problem you should accept it. Anyway I'll save it for when I'll start working with pandas, to be sure to not run into one of this similar ones which doesn't have an answer. +1 – FLAK-ZOSO Feb 07 '22 at 16:52
  • 2
    @FLAK-ZOSO Thanks, there is rule that OP's answer is only able to be accepted after two days. In this question, though it is related with pandas, I mainly want to talk about how to solve the common **partially initialized module** error so I didn't include pandas tag or the detail error in the question title. Actually as described in question's comment, I want to tell poster to look at the backtrace and analyse the backtrace. – Ynjxsjmh Feb 08 '22 at 01:14