简体   繁体   English

安装 Anaconda 后 Pycharm 中的 csv 导入错误

[英]csv Import error in Pycharm after installing Anaconda

I need to install Anaconda for another project while I use Pycharm regularly.当我经常使用 Pycharm 时,我需要为另一个项目安装 Anaconda。 However, after I installed Anaconda, if I hit the run button on Pycharm for a python file (I'm using python 3.7.4), there is an import error.但是,在我安装了 Anaconda 之后,如果我在 Pycharm 上点击运行按钮获取一个 python 文件(我使用的是 python 3.7.4),则会出现导入错误。 There was no problem before Anaconda was installed for the same code.相同代码安装Anaconda之前没有问题。

To illustrate, under the parent folder I created a folder named "io" (which is the same as built-in module) and the python file named "testandtest.py".为了说明,在父文件夹下,我创建了一个名为“io”的文件夹(与内置模块相同)和名为“testandtest.py”的python文件。 The directory structure is like this:目录结构是这样的:

  • test_IO_error/ test_IO_error/
    • init .py初始化.py
    • io/输入/
      • init .py初始化.py
    • testandtest.py测试和测试.py

The code of "testandtest.py" is as below: “testandtest.py”的代码如下:

import csv
def foo():
    print ("This is a test!")

After I hit "run" button for testandtest.py in Pycharm, there is an error message:在 Pycharm 中点击 testandtest.py 的“运行”按钮后,出现错误消息:

Fatal Python error: init_sys_streams: can't initialize sys standard streams AttributeError: module 'io' has no attribute 'OpenWrapper' Current thread 0x00046ccc (most recent call first): Process finished with exit code -1073740791 (0xC0000409)致命的 Python 错误:init_sys_streams:无法初始化 sys 标准流 AttributeError:模块“io”没有属性“OpenWrapper”当前线程 0x00046ccc(最近一次调用):进程已完成,退出代码为 -1073740791(0xC0000409)

I assume this problem is because when importing csv , some files in csv module needs to import the built-in module io , but instead import the io module in the current folder, which of course does not have an attribute 'OpenWrapper'.我假设这个问题是因为在导入csvcsv模块中的某些文件需要导入内置模块io ,而是导入当前文件夹中的 io 模块,这当然没有属性'OpenWrapper'。 But isn't it the case that python 3 is using absolute import always?但是,python 3 不是总是使用绝对导入吗?

There is no problem if I run the function foo in console of Pycharm.如果我在 Pycharm 的控制台中运行函数foo没有问题。

This problem only happened when run the code using the "run" button in Pycharm software.此问题仅在使用 Pycharm 软件中的“运行”按钮运行代码时发生。

After searching many answers on line.网上搜了很多答案。 I find a fix.我找到了解决办法。

This is obviously a module shadowing issue.这显然是一个模块阴影问题。 However, I don't want to change the folder name from io to another one.但是,我不想将文件夹名称从io更改为另一个。

the fix is: go to Run --> Edit Configurations then uncheck修复方法是:转到运行 --> 编辑配置,然后取消选中

  • Add content roots to PYTHONPATH将内容根添加到 PYTHONPATH
  • Add sources roots to PYTHONPATH将源根添加到 PYTHONPATH

Similar Problem was fixed before. 类似问题之前已修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM