V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2018-11-01 11:15:43 +08:00 · 4629 次点击
    这是一个创建于 1975 天前的主题,其中的信息可能已经有所发展或是发生改变。
    文件目录大概如下所示:
    ---_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 条回复    2018-11-01 19:38:52 +08:00
    arrow8899
        1
    arrow8899  
       2018-11-01 11:34:43 +08:00
    你的 common 没在 path 里面。
    办法 1:把根目录加入 sys.path 中
    办法 2:从根目录运行 python ./data_pip/importance_analysis/plot.py
    anonymous256
        2
    anonymous256  
       2018-11-01 11:35:27 +08:00
    按照你的方式组织了一遍, 没问题啊.
    是不是因为你把 __init__ (双下划线), 写成 _init_ (单下划线了).....
    myyou
        3
    myyou  
       2018-11-01 11:35:56 +08:00
    __init__.py 是双下划线
    DongDongXie
        4
    DongDongXie  
    OP
       2018-11-01 11:52:44 +08:00
    @myyou 我这里写成单下划线_了,实际上我这边已经写成__了
    DongDongXie
        5
    DongDongXie  
    OP
       2018-11-01 11:52:54 +08:00
    @anonymous256 我这里写成单下划线_了,实际上我这边已经写成__了
    fzzff
        6
    fzzff  
       2018-11-01 12:30:28 +08:00
    试试 在导入前 import sys
    sys.path.append(common_path) 或者 from plot.common.io_util import parse_line
    fzzff
        7
    fzzff  
       2018-11-01 12:31:20 +08:00
    还有一种可能 pip uninstall common
    ytmsdy
        8
    ytmsdy  
       2018-11-01 12:34:11 +08:00
    _init_.py 应该修改为 __init__.py
    然后在 __init__.py 中写
    from .ou_util import parse_line

    plot.py 中写入
    from common import parse_line,

    试试这样。
    foxyier
        9
    foxyier  
       2018-11-01 15:30:00 +08:00
    在 common 的__init__.py 加入 import sys sys.path.append(".")试试
    quere
        10
    quere  
       2018-11-01 18:02:47 +08:00   ❤️ 1
    在 pycharm 里面 选中那个包,右键 Mark Directory as -----》 Sources Root ,让这个文件变为一个包
    shm7
        11
    shm7  
       2018-11-01 19:38:52 +08:00 via iPhone
    上面就是我的意思
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3549 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 04:52 · PVG 12:52 · LAX 21:52 · JFK 00:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.