Pycharm 无法在执行时导入我的模块(该目录存在于源列表中,我可以从命令行执行它)

Pycharm cannot import my modules while executing (the directory is present in the sources list and I can execute it just fine from the command line)

它向我抛出这个错误:

/usr/bin/python -u /opt/pycharm-community-4.5.1/helpers/pydev/pydev_run_in_console.py 58137 38816 /path/to/my/module.py
/usr/lib/python/site-packages/IPython/external/path.py:32: DeprecationWarning: the md5 module is deprecated; use hashlib instead
  import sys, warnings, os, fnmatch, glob, shutil, codecs, md5
/usr/lib/python/site-packages/IPython/iplib.py:58: DeprecationWarning: the sets module is deprecated
  from sets import Set
Running /path/to/my/module.py
Traceback (most recent call last):
  File "/opt/pycharm-community-4.5.1/helpers/pydev/pydev_run_in_console.py", line 69, in <module>
    globals = run_file(file, None, None)
  File "/opt/pycharm-community-4.5.1/helpers/pydev/pydev_run_in_console.py", line 29, in run_file
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/path/to/my/module.py", line 13, in <module>
    import my.module.name
ImportError: No module named my.module.name

Process finished with exit code 1
Couldn't connect to console process.

如果我在 BASH 终端上 运行 它,它执行得很好。它实际上也从 PyCharm 开始执行得很好,但我不确定发生了什么变化,我突然开始看到这种情况发生了。此外,它没有在编辑器中显示有关丢失模块的任何错误,这表明至少编辑器可以在其搜索的路径中看到这些包。 PYTHONPATH也有这个目​​录。

从 运行 的环境变量中删除 PYTHONPATH 解决了这个问题。

修复步骤:

  1. 运行 -> 运行...
  2. 编辑配置
  3. Select 运行 受影响的配置
  4. 单击环境变量文本字段旁边标有 ... 的按钮
  5. 从那里显示的环境变量列表中删除 PYTHONPATH

我仍然不明白发生了什么,但是在删除它之前检查这个变量显示添加了多次相同的路径列表。这看起来很奇怪,所以我删除了这个变量并尝试了,它起作用了!