mirror of
https://github.com/kennethreitz-archive/pyinstaller.git
synced 2026-06-05 15:40:17 +00:00
Ticket #69: correctly support relative imports within __init__ files
git-svn-id: http://svn.pyinstaller.org/trunk@678 8dd32b29-ccff-0310-8a9a-9233e24343b1
This commit is contained in:
@@ -0,0 +1 @@
|
||||
from .baz import *
|
||||
@@ -0,0 +1 @@
|
||||
from ..baz import *
|
||||
@@ -0,0 +1,2 @@
|
||||
def say_hello_please():
|
||||
print "Hello World!"
|
||||
@@ -0,0 +1,5 @@
|
||||
import relimp2.bar
|
||||
import relimp2.bar.bar2
|
||||
|
||||
relimp2.bar.say_hello_please()
|
||||
relimp2.bar.bar2.say_hello_please()
|
||||
@@ -0,0 +1,17 @@
|
||||
# -*- mode: python -*-
|
||||
|
||||
__testname__ = 'test-relative-import2'
|
||||
|
||||
a = Analysis([os.path.join(HOMEPATH,'support/_mountzlib.py'),
|
||||
__testname__ + '.py'],
|
||||
)
|
||||
pyz = PYZ(a.pure)
|
||||
exe = EXE(pyz,
|
||||
a.scripts,
|
||||
a.binaries,
|
||||
a.zipfiles,
|
||||
name = os.path.join('dist', __testname__, __testname__ +'.exe'),
|
||||
debug=False,
|
||||
strip=False,
|
||||
upx=False,
|
||||
console=1 )
|
||||
@@ -391,6 +391,7 @@ class ImportManager:
|
||||
contexts = [None]
|
||||
else: # level != 0
|
||||
importernm = globals.get('__name__', '')
|
||||
ispkg = hasattr(_sys_modules_get(importernm), '__path__')
|
||||
debug('importernm %s' % importernm)
|
||||
if level < 0:
|
||||
# behaviour up to Python 2.4 (and default in Python 2.5)
|
||||
@@ -400,11 +401,14 @@ class ImportManager:
|
||||
# relative import, do not try absolute
|
||||
if not importernm:
|
||||
raise RuntimeError("Relative import requires package")
|
||||
importernm = _string_split(importernm, '.')[:-level]
|
||||
importernm = _string_join('.', importernm)
|
||||
# level=1 => current package
|
||||
# level=2 => previous package => drop 1 level
|
||||
if level > 1:
|
||||
importernm = _string_split(importernm, '.')[:-level+1]
|
||||
importernm = _string_join('.', importernm)
|
||||
contexts = []
|
||||
if importernm:
|
||||
if hasattr(_sys_modules_get(importernm), '__path__'):
|
||||
if ispkg:
|
||||
# If you use the "from __init__ import" syntax, the package
|
||||
# name will have a __init__ in it. We want to strip it.
|
||||
if importernm[-len(".__init__"):] == ".__init__":
|
||||
|
||||
@@ -490,12 +490,17 @@ class ImportTracker:
|
||||
contexts = [None]
|
||||
elif level > 0:
|
||||
# relative import, do not try absolute
|
||||
contexts = [string.join(string.split(importernm, '.')[:-level], '.')]
|
||||
if self.ispackage(importernm):
|
||||
level -= 1
|
||||
if level > 0:
|
||||
importernm = string.join(string.split(importernm, '.')[:-level], ".")
|
||||
contexts = [importernm]
|
||||
importernm = None
|
||||
|
||||
_all = None
|
||||
|
||||
assert contexts
|
||||
|
||||
# so contexts is [pkgnm, None] or just [None]
|
||||
if nmparts[-1] == '*':
|
||||
del nmparts[-1]
|
||||
|
||||
Reference in New Issue
Block a user