如何通过shell来执行脚本main.py,还是需要对__init__.py文件进行加载包?

当前程序目录结构是这样的

http://img3.mukewang.com/643f9920000151b711841040.jpg

main.py的代码:

#! /usr/bin/env python# encoding:utf-8from A import call_a
def main():    call_a()if __name__ == '__main__':
    main()

A.py文件的代码:

#! /usr/bin/env python# encoding:utf-8from B import call_b

def call_a():
	print "call_a"
	call_b()

B.py文件代码:

#! /usr/bin/env python# encoding:utf-8def call_b():
    print "Call_b"

当我通过bash命令运行脚本:

bogon:tests nooper$ python innermodules/main.py 
Traceback (most recent call last):
  File "innermodules/main.py", line 4, in <module>    from A import call_aImportError: No module named Abogon:tests nooper$

是不能识别模块的加载路径,但是能通过IDE-pycharm运行。


慕容708150
浏览 120回答 2
2回答

拉莫斯之舞

import&nbsp;sys sys.path.append('..')将父目录加入import路径

杨__羊羊

可以直接通过pycharm来修改import的路径,具体pycharm版本不一样,对应按钮位置不一样,因此仅以文字说明。file->Settings(Ctrl+Shift+s)->Project Setting->Project Interpreter->选择你的python版本->Interpreter Path->添加你这里的话添加如下目录即可**\tests即project的根目录。如果你在tests下有文件夹test2,其下有文件c.py。要引用c文件的c2在你的main.py里面加入:from test2.c import c2路径是以你添加的路径的相对路径。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python