1PYTHON setup.py build_ext --inplace 2PYTHON -c "import my_test_package as p; assert not p.__file__.rstrip('co').endswith('.py'), p.__file__; p.test()" 3PYTHON -c "import my_test_package.a as a; a.test()" 4PYTHON -c "import my_test_package.another as p; assert not p.__file__.rstrip('co').endswith('.py'), p.__file__; p.test()" 5PYTHON -c "import my_test_package.another.a as a; a.test()" 6 7######## setup.py ######## 8 9from Cython.Build.Dependencies import cythonize 10from distutils.core import setup 11 12setup( 13 ext_modules = cythonize(["my_test_package/**/*.py"]), 14) 15 16######## my_test_package/__init__.py ######## 17 18# cython: set_initial_path=SOURCEFILE 19 20initial_path = __path__ 21initial_file = __file__ 22 23try: 24 from . import a 25 import_error = None 26except ImportError as e: 27 import_error = e 28 import traceback 29 traceback.print_exc() 30 31def test(): 32 print "FILE: ", initial_file 33 print "PATH: ", initial_path 34 assert initial_path[0].endswith('my_test_package'), initial_path 35 assert initial_file.endswith('__init__.py'), initial_file 36 assert import_error is None, import_error 37 38######## my_test_package/another/__init__.py ######## 39 40# cython: set_initial_path=SOURCEFILE 41 42initial_path = __path__ 43initial_file = __file__ 44 45try: 46 from . import a 47 import_error = None 48except ImportError as e: 49 import_error = e 50 import traceback 51 traceback.print_exc() 52 53def test(): 54 print "FILE: ", initial_file 55 print "PATH: ", initial_path 56 assert initial_path[0].endswith('another'), initial_path 57 assert initial_file.endswith('__init__.py'), initial_file 58 assert import_error is None, import_error 59 60######## my_test_package/a.py ######## 61 62# cython: set_initial_path=SOURCEFILE 63 64initial_file = __file__ 65 66try: 67 initial_path = __path__ 68except NameError: 69 got_name_error = True 70else: 71 got_name_error = False 72 73def test(): 74 assert initial_file.endswith('a.py'), initial_file 75 assert got_name_error, "looks like __path__ was set at module init time: " + initial_path 76 77######## my_test_package/another/a.py ######## 78 79# cython: set_initial_path=SOURCEFILE 80 81initial_file = __file__ 82 83try: 84 initial_path = __path__ 85except NameError: 86 got_name_error = True 87else: 88 got_name_error = False 89 90def test(): 91 assert initial_file.endswith('a.py'), initial_file 92 assert got_name_error, "looks like __path__ was set at module init time: " + initial_path 93