** 是我自己的代码. 跟代码有关系吗?需要贴代码吗
![]() | 1 Chyroc 2016-10-13 19:00:15 +08:00 via iPhone 贴代码 |
![]() | 2 crytis OP # 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 |
![]() | 3 crytis OP |
![]() | 4 crytis OP |
![]() | 5 eric6356 2016-10-13 20:29:55 +08:00 via iPhone 建议你研究一下 python import 的时候都会从哪些地方 import |
![]() | 7 crytis OP 但是为什么在 pycharm 里运行就没有问题 |
![]() | 8 KingsWay 2016-10-14 00:59:49 +08:00 我也发现过这个问题,一般情况是,直接命令行里运行最外层的那个 python 文件,就没问题。 如果进入某个子目录执行某个文件,就会报和你一样的错误。 应该是 python 代码里引入自定义包的相对路径问题。 最后一个提示:使用 ipython 运行就不会遇到这个问题 |
9 zhibin 2016-10-14 10:50:21 +08:00 ![]() 我遇到过,你这样: 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 上一级的绝对或相对路径 应该是没问题的。 |
![]() | 11 NxnXgpuPSfsIT 2016-10-16 22:47:44 +08:00 @crytis 你的主文件入口是需要在包里面还是在包外面? |
![]() | 12 crytis OP @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 |