不知道您使用的是哪个版本,但这可能会对您有所帮助...
__init__.py:
# 1. run main from within the package
# Python2
'''
>>> from main import main_func
main: from helper import helper_func
>>> main_func()
helping
'''
# Python3
'''
>>> from main import main_func
main: from helper import helper_func
>>> main_func()
helping
'''
# XXX: Notice that both of the above are the same
# However, note the differences below
# 2. import main_func for use outside this package
# Python2
'''
>>> from test_setup import main_func
test_setup.main: from helper import helper_func
test_setup: from main import main_func
>>> main_func()
helping
'''
# Python3
'''
>>> from test_setup import main_func
# Failed
ImportError:0 No module named 'main' in module test_setup
ImportError:0 No module named 'helper' in module test_setup.main
# Succeeded
test_setup.main: from .helper import helper_func
test_setup: from .main import main_func
>>> main_func()
helping
'''
try:
from main import main_func
print("%s: from main import main_func"%(__name__))
except ImportError as exc:
print("ImportError:0 %s in module %s"%(exc,__name__))
try:
from .main import main_func
print("%s: from .main import main_func"%(__name__))
except ImportError as exc:
print("ImportError:1 %s in module %s"%(exc,__name__))
from test_setup.main import main_func
print("%s: from test_setup.main import main_func"%(__name__))
main.py:
try:
from helper import helper_func
print("%s: from helper import helper_func"%(__name__))
except ImportError as exc:
print("ImportError:0 %s in module %s"%(exc,__name__))
try:
from .helper import helper_func
print("%s: from .helper import helper_func"%(__name__))
except ImportError as exc:
print("ImportError:1 %s in module %s"%(exc,__name__))
from test_setup.helper import helper_func
print("%s from test_setup.helper import helper_func"%(__name__))
def main_func():
helper_func()
if __name__ == '__main__':
main_func()
为澄清而编辑:
__init__.py:
"""Init for main module"""
print("__init__.__doc__ = %s"%(__doc__))
# -----------------------------------------------------------------------------
from os import path
import sys
print("sys.executable = %s"%(sys.executable))
print("sys.argv[0] = %s"%(sys.argv[0]))
main_prg_path = path.abspath(path.dirname(__file__))
print("main_prg_path = %s"%(main_prg_path))
if getattr(sys, 'frozen', False):
# When being bundled by pyinstaller, paths are different
print("Running as pyinstaller bundle!", sys.argv[0])
main_prg_path = path.abspath(path.dirname(sys.argv[0]))
sys.path.append(main_prg_path)
print("main_prg_path = %s"%(main_prg_path))
# Append other directories needed for main program
#sys.path.append(os.path.join(main_prg_path, 'utils'))
# -----------------------------------------------------------------------------
from main import main_func
main.py:
"""Main module"""
print("main.__doc__ = %s"%(__doc__))
# -----------------------------------------------------------------------------
# Running in python
# inside: from main import main_func
# outside: from test_setup import main_func
# Running in terminal
# inside: python -m main
# outside: python -m test_setup/main
# -----------------------------------------------------------------------------
from os import path
import sys
print("sys.executable = %s"%(sys.executable))
print("sys.argv[0] = %s"%(sys.argv[0]))
main_prg_path = path.abspath(path.dirname(__file__))
print("main_prg_path = %s"%(main_prg_path))
if getattr(sys, 'frozen', False):
# When being bundled by pyinstaller, paths are different
print("Running as pyinstaller bundle!", sys.argv[0])
main_prg_path = path.abspath(path.dirname(sys.argv[0]))
sys.path.append(main_prg_path)
print("main_prg_path = %s"%(main_prg_path))
# Append other directories needed for main program
#sys.path.append(os.path.join(main_prg_path, 'utils'))
# -----------------------------------------------------------------------------
from helper import helper_func
def main_func():
helper_func()
# -----------------------------------------------------------------------------
if __name__ == '__main__':
print("running as main: %s"%(__name__))
main_func()
else:
print("running as file: %s"%(__file__))
main_func()
helper.py:
"""Helper module"""
print("helper.__doc__ = %s"%(__doc__))
__author__ = "Your Name"
__author_email__ = "Your Email"
__version__ = "0.0.1"
__date__ = "24 Jan 2021"
def helper_func():
print("Author = %s\nEmail = %s\nVersion = %s\nDate = %s"%(
__author__, __author_email__, __version__, __date__))
@rhz
编辑:添加更改以显示正在发生的事情和sys.path.append 的简单用法示例