推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
DongDongXie
V2EX  ›  Python

Python ImportError: No module named common.io_util

  •  
  •   DongDongXie · Nov 1, 2018 · 6217 views
    This topic created in 2938 days ago, the information mentioned may be changed or developed.
    文件目录大概如下所示:
    ---_init_.py
    ---common
    ------io_util.py
    ------_init_.py
    ---config
    ------util.py
    ------_init_.py
    ---data_pip
    ------_init_.py
    ------importance_analysis
    ----------plot.py
    ----------_init_.py
    
    然后在 plot.py 里面导入:
    from common.io_util import parse_line,
    from config.util import *
    

    执行 plot.py 报错 ImportError: No module named common.io_util,试过网上说的好几种方法都不行,请问怎么解决

    11 replies  •  2018-11-01 19:38:52 +08:00
    arrow8899
    arrow8899
    Nov 1, 2018
    1
    你的 common 没在 path 里面。
    办法 1:把根目录加入 sys.path 中
    办法 2:从根目录运行 python ./data_pip/importance_analysis/plot.py
    anonymous256
    anonymous256
    Nov 1, 2018
    2
    按照你的方式组织了一遍, 没问题啊.
    是不是因为你把 __init__ (双下划线), 写成 _init_ (单下划线了).....
    myyou
    myyou
    Nov 1, 2018
    3
    __init__.py 是双下划线
    DongDongXie
    DongDongXie
    OP
    Nov 1, 2018
    4
    @myyou 我这里写成单下划线_了,实际上我这边已经写成__了
    DongDongXie
    DongDongXie
    OP
    Nov 1, 2018
    5
    @anonymous256 我这里写成单下划线_了,实际上我这边已经写成__了
    fzzff
    fzzff
    Nov 1, 2018
    6
    试试 在导入前 import sys
    sys.path.append(common_path) 或者 from plot.common.io_util import parse_line
    fzzff
    fzzff
    Nov 1, 2018
    7
    还有一种可能 pip uninstall common
    ytmsdy
    ytmsdy
    Nov 1, 2018
    8
    _init_.py 应该修改为 __init__.py
    然后在 __init__.py 中写
    from .ou_util import parse_line

    plot.py 中写入
    from common import parse_line,

    试试这样。
    foxyier
    foxyier
    Nov 1, 2018
    9
    在 common 的__init__.py 加入 import sys sys.path.append(".")试试
    quere
    quere
    Nov 1, 2018❤️1
    10
    在 pycharm 里面 选中那个包,右键 Mark Directory as -----》 Sources Root ,让这个文件变为一个包
    shm7
    shm7
    Nov 1, 2018 via iPhone
    11
    上面就是我的意思
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1814 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 15:58 · PVG 23:58 · LAX 07:58 · JFK 10:58
    ♥ Do have faith in what you're doing.