Importing from another directory

∥☆過路亽.° 提交于 2019-12-05 20:20:44

One way is to make each of your myapp_service*_start.py files add myapp/ directory to sys.path.

For example, drop a file called import_me.py into myapp_service1/ with code that appends the "one up" directory (relative to importing file) to sys.path:

import os
import sys
import inspect
this_dir = os.path.dirname(inspect.getfile(inspect.currentframe()))
src_dir = os.path.join(this_dir, '..')
sys.path.insert(0, src_dir)

Then, in your myapp_service1_start.py you can do something like:

import import_me
from common import myapp_common1
from common import myapp_common2

Of course, be sure to make common directory a Python package by dropping a (possibly empty) __init__.py file into it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!