为什么我每个目录下都加了__init__.py 还是一直报 no module name ** - V2EX
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
crytis
V2EX    Python

为什么我每个目录下都加了__init__.py 还是一直报 no module name **

  •  
  •   crytis 2016-10-13 18:31:30 +08:00 5169 次点击
    这是一个创建于 3288 天前的主题,其中的信息可能已经有所发展或是发生改变。

    ** 是我自己的代码. 跟代码有关系吗?需要贴代码吗

    13 条回复    2016-10-17 07:44:04 +08:00
    Chyroc
        1
    Chyroc  
       2016-10-13 19:00:15 +08:00 via iPhone
    贴代码
    crytis
        2
    crytis  
    OP
       2016-10-13 20:26:07 +08:00
    # coding=utf-8

    import sys
    import xml.etree.cElementTree as ET

    from flask import Flask, request

    from src import RedPacketsService
    from src.lib.WXBizMsgCrypt import WXBizMsgCrypt
    from src.WXUtils import WXUtils

    app = Flask(__name__)
    wxutils = WXUtils()


    @app.route('/auth', methods=["GET", "POST"])
    def auth():
    pass


    if __name__ == '__main__':
    try:
    app.run(host="0.0.0.0", port=8080)
    except IOError:
    print "error"

    @Chyroc
    crytis
        3
    crytis  
    OP
       2016-10-13 20:26:54 +08:00
    @Chyroc 这是目录结构
    ├─.idea
    │ └─inspectionProfiles
    ├─lib
    ├─py
    │ └─model
    ├─sql
    └─src
    ├─lib
    └─model
    crytis
        4
    crytis  
    OP
       2016-10-13 20:27:29 +08:00
    python src\Weixin.py


    Traceback (most recent call last):
    File "src\Weixin.py", line 8, in <module>
    from src import RedPacketsService
    ImportError: No module named src
    eric6356
        5
    eric6356  
       2016-10-13 20:29:55 +08:00 via iPhone
    建议你研究一下 python import 的时候都会从哪些地方 import
    crytis
        6
    crytis  
    OP
       2016-10-13 20:46:24 +08:00
    @eric6356 sys.path append 了也不行
    crytis
        7
    crytis  
    OP
       2016-10-13 20:46:54 +08:00
    但是为什么在 pycharm 里运行就没有问题
    KingsWay
        8
    KingsWay  
       2016-10-14 00:59:49 +08:00
    我也发现过这个问题,一般情况是,直接命令行里运行最外层的那个 python 文件,就没问题。
    如果进入某个子目录执行某个文件,就会报和你一样的错误。
    应该是 python 代码里引入自定义包的相对路径问题。

    最后一个提示:使用 ipython 运行就不会遇到这个问题
    zhibin
        9
    zhibin  
       2016-10-14 10:50:21 +08:00   1
    我遇到过,你这样:
    import sys
    print sys.path

    然后你就明白了,参考:

    https://docs.python.org/2/tutorial/modules.html

    When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

    the directory containing the input script (or the current directory).
    PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
    the installation-dependent default.

    btw:
    你 append 怎么写的? append src 上一级的绝对或相对路径 应该是没问题的。
    crytis
        10
    crytis  
    OP
       2016-10-14 15:48:59 +08:00
    @zhibin 解决了 困扰了一天啊 好不智能 append 了包的上一级目录就行了 唉
    NxnXgpuPSfsIT
        11
    NxnXgpuPSfsIT  
       2016-10-16 22:47:44 +08:00
    @crytis 你的主文件入口是需要在包里面还是在包外面?
    crytis
        12
    crytis  
    OP
       2016-10-17 00:36:34 +08:00 via iPhone
    @NxnXgpuPSfsIT 包里面
    NxnXgpuPSfsIT
        13
    NxnXgpuPSfsIT  
       2016-10-17 07:44:04 +08:00 via Android
    如果我没有理解错的话,是在 src 包里的一个文件作为主入口,调用另一个 src 包里的文件了。
    那直接用相对引用就可以了, import .RedPacketsService
    如果需要调用的是 lib 里的东西,那么这样, import ..lib.somefile.py
    如果目录层数更多,建议做成包从外部调用。
    例如现在整个 lib 是个只从外部调用的包,那么 lib/d1/d2/d3/file.py 就可以这样调用 lib/somefile.py , import lib.somefile
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5445 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 08:03 PVG 16:03 LAX 01:03 JFK 04:03
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86