推荐学习书目
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 · Jan 4, 2018 · 2476 views
    This topic created in 3067 days ago, the information mentioned may be changed or developed.
    目录结构:
    -- 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 replies    2018-01-05 10:41:19 +08:00
    casparchen
        1
    casparchen  
       Jan 4, 2018 via iPhone
    whx20202
        2
    whx20202  
       Jan 4, 2018
    目测 1L 说的对,如果你没有那个文件的话
    fingerstyle
        3
    fingerstyle  
    OP
       Jan 5, 2018
    回复一楼,mod2 目录里面有__init__.py 这个文件
    fingerstyle
        4
    fingerstyle  
    OP
       Jan 5, 2018
    刚试了一下,如果在 test1.py 中导入 mod2 模块就可以正常运行。。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3137 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 03:13 · PVG 11:13 · LAX 20:13 · JFK 23:13
    ♥ Do have faith in what you're doing.