Python 导入在控制台中工作,但不在pyCharm中工作

Python 导入在控制台中工作,但不在pyCharm中工作,python,pycharm,Python,Pycharm,我有一个python项目,有很多导入,比如: from src.main.fr.some.module import someclass 虽然效果很好,但我的同事们希望进口产品是: from fr.some.module import someclass 然后我更改了我的virtualenv的activate脚本中的PYTHONPATH,如下所示: export PYTHONPATH="/home/giffon/Documents/wopmars/src/main:/home/giffon/

我有一个python项目,有很多导入,比如:

from src.main.fr.some.module import someclass
虽然效果很好,但我的同事们希望进口产品是:

from fr.some.module import someclass
然后我更改了我的
virtualenv
activate
脚本中的
PYTHONPATH
,如下所示:

export PYTHONPATH="/home/giffon/Documents/wopmars/src/main:/home/giffon/Documents/wopmars/src/test"
并将所有
src.main.fr.some.module
替换为
fr.some.module

然后我在控制台中尝试了我的代码,结果很好(请注意,我正在打印代码开头的
PYTHONPATH
,并且
/home/giffon/Documents/wopmars/src/main
与预期一样)

但是,由于我使用pycharm,我希望IDE考虑到我的更改。问题来了

我在某个地方读到,我应该通过执行以下操作来修改解释器Python路径:

文件>设置>Projet:wopmars>Projet解释器>“控制盘”> 更多…>“显示所选解释器的路径(WopMars解释器 选中“>+>”浏览到/home/giffon/Documents/wopmars/src/main”> 确定>确定>应用>确定

然后我执行与上面相同的代码:

/home/giffon/virtualenvs/WopMars/bin/python3 /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/parsing/Parser.py
PRINTING THE PYTHONPATH
/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/parsing
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages/pyparsing-2.1.4-py3.4.egg
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages/cycler-0.10.0-py3.4.egg
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages/pytz-2016.4-py3.4.egg
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages/python_dateutil-2.5.3-py3.4.egg
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages/six-1.10.0-py3.4.egg
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages/numpy-1.11.0-py3.4-linux-x86_64.egg
/home/giffon/Documents/wopmars/src/test
/home/giffon/Documents/wopmars/src/main
/home/giffon/virtualenvs/WopMars/lib/python3.4
/home/giffon/virtualenvs/WopMars/lib/python3.4/plat-x86_64-linux-gnu
/home/giffon/virtualenvs/WopMars/lib/python3.4/lib-dynload
/usr/lib/python3.4
/usr/lib/python3.4/plat-x86_64-linux-gnu
/home/giffon/virtualenvs/WopMars/lib/python3.4/site-packages



Traceback (most recent call last):
  File "/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/parsing/Parser.py", line 12, in <module>
    from fr.tagc.wopmars.framework.management.DAG import DAG
ImportError: No module named 'fr.tagc.wopmars.framework.management.DAG'
注意:在
.profile
.bashrc
中更改
PYTHONPATH
会得到相同的结果


注2:如果我不导出PYTHONPATH,控制台会给我与pycharm相同的错误

由于祖兰的评论,我解决了我的问题

TLDR:

src/main/
目录放在pycharm解释器的
src/test/
目录之前


在pycharm和console中使用python解释器的-v选项,我得到了以下输出:

控制台输出:

# /home/giffon/Documents/wopmars/src/main/fr/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/main/fr/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/main/fr/__pycache__/__init__.cpython-34.pyc'
import 'fr' # <_frozen_importlib.SourceFileLoader object at 0x7f384ce4b160>
# /home/giffon/Documents/wopmars/src/main/fr/tagc/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/main/fr/tagc/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/main/fr/tagc/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc' # <_frozen_importlib.SourceFileLoader object at 0x7f384ce4b358>
# /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc.wopmars' # <_frozen_importlib.SourceFileLoader object at 0x7f384ce4b400>
# /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc.wopmars.framework' # <_frozen_importlib.SourceFileLoader object at 0x7f384ce4b4a8>
# /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/management/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/management/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/management/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc.wopmars.framework.management' # <_frozen_importlib.SourceFileLoader object at 0x7f384ce4b550>
#/home/giffon/Documents/wopmars/src/main/fr/uuu-pycache\uuuu/u-init\uuu.cpython-34.pyc matches/home/giffon/Documents/wopmars/src/main/fr/u-init\uu.py
#来自“/home/giffon/Documents/wopmars/src/main/fr/\uuuu pycache\uuuuu/\uuuu init\uuuu.cpython-34.pyc”的代码对象
输入“fr”#
#/home/giffon/Documents/wopmars/src/main/fr/tagc/uuu-pycache\uuuuuu/uu-init\uuuuu.cpython-34.pyc matches/home/giffon/Documents/wopmars/src/main/fr/tagc/u-init\uuuuu.py
#来自“/home/giffon/Documents/wopmars/src/main/fr/tagc/\uuuuuu pycache\uuuuuu/\uuuuu init\uuuu.cpython-34.pyc”的代码对象
输入'fr.tagc'#
#/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/\uuuuu-pycache\uuuuu/\uu-init\uuuu.cpython-34.pyc matches/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/\uu-init\uu.py
#来自“/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/\uuuu pycache\uuuuu/\uuu init\uuuu.cpython-34.pyc”的代码对象
输入“fr.tagc.wopmars”
#/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/\uuuuu-pycache\uuuuuu/\uu-init\uuuuuu.cpython-34.pyc matches/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/wopmars/framework/\uu-init\uuuuuu.py
#来自“/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/\uuuuuu pycache\uuuuuu/\uuuu init\uuuu.cpython-34.pyc”的代码对象
导入'fr.tagc.wopmars.framework'#
#/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/management/\uuuuu-pycache\uuuuuuu/\uuu-init\uuuuuuuuuu.cpython-34.pyc matches/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/wopmars/framework/management/\uuuu-init\uuuuuuuuu
#来自“/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/management/_-pycache\u_/_-init\u_.cpython-34.pyc”的代码对象
导入“fr.tagc.wopmars.framework.management”
Pycharm输出:

# /home/giffon/Documents/wopmars/src/test/fr/tagc/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/test/fr/tagc/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/test/fr/tagc/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc' # <_frozen_importlib.SourceFileLoader object at 0x7fb1532422e8>
# /home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc.wopmars' # <_frozen_importlib.SourceFileLoader object at 0x7fb153242390>
# /home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc.wopmars.framework' # <_frozen_importlib.SourceFileLoader object at 0x7fb153242438>
# /home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/management/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/management/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/management/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc.wopmars.framework.management' # <_frozen_importlib.SourceFileLoader object at 0x7fb1532424e0>
Traceback (most recent call last):
  File "/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/parsing/Parser.py", line 12, in <module>
    from fr.tagc.wopmars.framework.management.DAG import DAG
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked
ImportError: No module named 'fr.tagc.wopmars.framework.management.DAG'
#/home/giffon/Documents/wopmars/src/test/fr/tagc/uuu-pycache\uuuuuuuu/uuu-init\uuuuuu.cpython-34.pyc matches/home/giffon/Documents/wopmars/src/test/fr/tagc/uu-init\uuuuu.py
#来自“/home/giffon/Documents/wopmars/src/test/fr/tagc/\uuuuuu pycache\uuuuuu/\uuuuu init\uuuu.cpython-34.pyc”的代码对象
输入'fr.tagc'#
#/home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/\uuuuuu-pycache\uuuuu/\uuu-init\uuuu.cpython-34.pyc matches/home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/\uu-init\uu.py
#来自“/home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/\uuuuu pycache\uuuuu/\uuuu init\uuuu.cpython-34.pyc”的代码对象
输入“fr.tagc.wopmars”
#/home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/\uuuuu-pycache\uuuuuu/\uu-init\uuuuu.cpython-34.pyc matches/home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/wopmars/framework/\uu-init\uuu.py
#来自“/home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/\uuuuuu pycache\uuuuuu/\uuuu init\uuuu.cpython-34.pyc”的代码对象
导入'fr.tagc.wopmars.framework'#
#/home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/management/\uuuuu-pycache\uuuuuuu/\uuu-init\uuuuuuuu.cpython-34.pyc matches/home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/wopmars/framework/management/\uuu-init\uuuuuuuuuuuu
#来自“/home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/management/_-pycache\u_/_-init\u_.cpython-34.pyc”的代码对象
导入“fr.tagc.wopmars.framework.management”
回溯(最近一次呼叫最后一次):
文件“/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/parsing/Parser.py”,第12行,在
从fr.tagc.wopmars.framework.management.DAG导入DAG
文件“”,第2237行,在“查找”和“加载”中
文件“”,第2224行,在“查找”和“加载”中解锁
ImportError:没有名为'fr.tagc.wopmars.framework.management.DAG'的模块
您可能已经注意到两个输出之间的细微差别:控制台解释器在
wopmars/src/main/
中搜索,而pycharm解释器在
wopmars/src/test/
中搜索

实际上,如果我仔细看一下打印的
PYTHONPATH
,它们是不同的:

  • 在控制台中,
    /home/giffon/Documents/wopmars/src/main
    出现在列表的
    /home/giffon/Documents/wopmars/src/test
    之前
  • 在Pycharm中,情况正好相反
然后,我将
/home/giffon/Documents/wopmars/src/test
放在pycharm中的
/home/giffon/Documents/wopmars/src/main
之后,它工作得非常好。我认为python解释器开始在
src/test/
中找到模块名的第一部分,然后他没有找到结尾,并且在没有查看其他路径的情况下引发错误。

看看这个:非常感谢,I f
# /home/giffon/Documents/wopmars/src/main/fr/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/main/fr/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/main/fr/__pycache__/__init__.cpython-34.pyc'
import 'fr' # <_frozen_importlib.SourceFileLoader object at 0x7f384ce4b160>
# /home/giffon/Documents/wopmars/src/main/fr/tagc/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/main/fr/tagc/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/main/fr/tagc/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc' # <_frozen_importlib.SourceFileLoader object at 0x7f384ce4b358>
# /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc.wopmars' # <_frozen_importlib.SourceFileLoader object at 0x7f384ce4b400>
# /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc.wopmars.framework' # <_frozen_importlib.SourceFileLoader object at 0x7f384ce4b4a8>
# /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/management/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/management/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/management/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc.wopmars.framework.management' # <_frozen_importlib.SourceFileLoader object at 0x7f384ce4b550>
# /home/giffon/Documents/wopmars/src/test/fr/tagc/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/test/fr/tagc/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/test/fr/tagc/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc' # <_frozen_importlib.SourceFileLoader object at 0x7fb1532422e8>
# /home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc.wopmars' # <_frozen_importlib.SourceFileLoader object at 0x7fb153242390>
# /home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc.wopmars.framework' # <_frozen_importlib.SourceFileLoader object at 0x7fb153242438>
# /home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/management/__pycache__/__init__.cpython-34.pyc matches /home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/management/__init__.py
# code object from '/home/giffon/Documents/wopmars/src/test/fr/tagc/wopmars/framework/management/__pycache__/__init__.cpython-34.pyc'
import 'fr.tagc.wopmars.framework.management' # <_frozen_importlib.SourceFileLoader object at 0x7fb1532424e0>
Traceback (most recent call last):
  File "/home/giffon/Documents/wopmars/src/main/fr/tagc/wopmars/framework/parsing/Parser.py", line 12, in <module>
    from fr.tagc.wopmars.framework.management.DAG import DAG
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked
ImportError: No module named 'fr.tagc.wopmars.framework.management.DAG'