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
fingerstyle
V2EX  ›  Python

引用父级目录中的模块报错,求解

  •  
  •   fingerstyle · 2018-01-04 19:27:13 +08:00 · 1573 次点击
    这是一个创建于 2275 天前的主题,其中的信息可能已经有所发展或是发生改变。
    目录结构:
    -- src
    |-- mod1.py
    |-- mod2
    | |-- mod2.py
    |-- sub
    | |-- test2.py
    |-- test1.py

    test2.py 中导入模块 mod2 的调用方式如下:
    import sys
    sys.path.append("..")
    from mod2.mod2 import *

    运行后报错:
    Traceback (most recent call last):
    File "test2.py", line 3, in <module>
    from mod2.mod2 import *
    ImportError: No module named mod2

    python 版本 2.6.6

    找了一些资料没有查到跟我一样的问题,求大家帮忙看下是哪里的问题,多谢。
    4 条回复    2018-01-05 10:41:19 +08:00
    casparchen
        1
    casparchen  
       2018-01-04 19:44:43 +08:00 via iPhone
    whx20202
        2
    whx20202  
       2018-01-04 19:51:31 +08:00
    目测 1L 说的对,如果你没有那个文件的话
    fingerstyle
        3
    fingerstyle  
    OP
       2018-01-05 09:59:51 +08:00
    回复一楼,mod2 目录里面有__init__.py 这个文件
    fingerstyle
        4
    fingerstyle  
    OP
       2018-01-05 10:41:19 +08:00
    刚试了一下,如果在 test1.py 中导入 mod2 模块就可以正常运行。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3270 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:03 · PVG 22:03 · LAX 07:03 · JFK 10:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.