PyCharm, some packages cannot import Pandas, ImportError: C extension: StringIO not built

痴心易碎 提交于 2020-01-03 15:34:26

问题


I am having a very strange error and cannot resolve it.

I have a project with the following directory structure:

ptouch/
    ptouch/
        __init__.py
        ptouch.py
        io.py
    tests/
        __init__.py
        tests.py

I am using PyCharm community edition, and Anaconda python distribution.

The file: ptouch.py contains the following code:

__author__ = 'foo'
import pandas as pd
df = pd.DataFrame()

Executing this file gives the following error:

C:\Anaconda\python.exe ~/ptouch.py
Traceback (most recent call last):
  File "~/ptouch.py", line 2, in <module>
    import pandas as pd
File "C:\Anaconda\lib\site-packages\pandas\__init__.py", line 13, in <module>
  "extensions first.".format(module))
ImportError: C extension: StringIO not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace' to build the C extensions first.

However, executing tests.py gives no errors and uses pandas with no problem.

The file tests.py contains the following code:

from unittest import TestCase
import pandas as pd
class Tests(TestCase):
    def test_pickle(self):
        d = pd.DataFrame(np.random.rand(50, 10))
        self.fail()

I have tried uninstalling and reinstalling pandas with both conda and pip to no avail. Creating a new project seems to be able to load pandas without issue. I cannot find any package specific settings that could result in this error.

Is there some reason why some packages or run configurations could be trying to run pandas from source or something? When the code is absolutely stripped out?


回答1:


It turns having a file named 'io.py' in the same package as something importing pandas breaks things badly.

Don't name things io.py



来源:https://stackoverflow.com/questions/31014146/pycharm-some-packages-cannot-import-pandas-importerror-c-extension-stringio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!