[Scons-dev] Python 3 issue
RW
garlicbready at googlemail.com
Sun Jun 25 20:05:45 EDT 2017
Just to follow
instead of using exec_module I think this might work with relative imports
instead
```
spec = importlib.util.spec_from_file_location(absolute_path,
absolute_path)
module = spec.loader.load_module(spec.name)
```
I'll see if it works with tools in different places before doing a pull
request
Many Thanks
Richard
On 26 June 2017 at 00:10, RW <garlicbready at googlemail.com> wrote:
> Just to comment on this one
> it looks like others have had the same problem as well
> https://github.com/datafolklabs/cement/issues/386
> https://stackoverflow.com/questions/38802908/importing-
> module-with-current-directory-imports
>
> I suspect this might be a wider issue with python not supporting relative
> imports when using exec_module from importlib
>
> To simplify the problem a bit without hacking scons I tried creating a
> small lump of code within site_scons\site_tools (of the example code posted
> so far) to see if there's a workaround. Not managed to find one so far
>
>
> test1.py
> ```
> import os
> import importlib.util
>
> #import relative
>
> def path_import(absolute_path):
> '''implementation taken from https://docs.python.org/3/
> library/importlib.html#importing-a-source-file-directly'''
> spec = importlib.util.spec_from_file_location(absolute_path,
> absolute_path)
> module = importlib.util.module_from_spec(spec)
> spec.loader.exec_module(module)
> return module
>
> def main():
> root = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
> modpath = os.path.join(root, 'relative', '__init__.py')
> print(modpath)
> path_import(modpath)
>
> if __name__ == '__main__':
> main()
> ```
>
>
> On 23 June 2017 at 19:03, Bill Deegan <bill at baddogconsulting.com> wrote:
>
>> O.k so relative imports should work with both py3 and py2, but likely our
>> tool import logic is breaking it with py3.
>> (the logic is different for py3 https://bitbucket.org/scons/sc
>> ons/src/e2d9abca56a519930e5846a3634c85074fcbba47/src/engine/
>> SCons/Tool/__init__.py?at=default&fileviewer=file-view-
>> default#__init__.py-164)
>>
>> Can we live with absolute imports until we resolve the issue?
>>
>> -Bill
>>
>> On Fri, Jun 23, 2017 at 10:53 AM, Bill Deegan <bill at baddogconsulting.com>
>> wrote:
>>
>>> This change:
>>> https://bitbucket.org/bdbaddog/scons-russel-example/commits/
>>> 48899a710f9afbec891307addd6ae5b9c08d53c9?at=master
>>>
>>> Will make it work in py2 and py3.
>>>
>>> Any reason we can't live with that?
>>>
>>> -Bill
>>>
>>> On Fri, Jun 23, 2017 at 10:38 AM, Bill Deegan <bill at baddogconsulting.com
>>> > wrote:
>>>
>>>> Russel,
>>>>
>>>> I've created a git repo in bitbucket to facilitate talking about this
>>>> issue.
>>>>
>>>> https://bitbucket.org/bdbaddog/scons-russel-example
>>>>
>>>> And given you permissions.
>>>> The second commit should be the same as your zip file.
>>>> Going forward, it's far easier to create a throwaway repo and share it
>>>> than to pass zip files back and forth..
>>>>
>>>> -Bill
>>>>
>>>>
>>>> On Fri, Jun 23, 2017 at 7:14 AM, Russel Winder <russel at winder.org.uk>
>>>> wrote:
>>>>
>>>>> Python 3 changed the way imports worked, absolute imports became the
>>>>> norm. However relative imports were retain via extra syntax. This has,
>>>>> I believe been backported to Python 2.7.
>>>>>
>>>>> The summary of the situation is that to enable tools as packages as
>>>>> well as tools as modules, you have to use relative imports of generate
>>>>> and exists in __init__.py. This used to work fine. A very minor change
>>>>> of syntax means it all still works on Python 2. However on Python 3
>>>>> something weird happens. This is a SCons thing, because if you do the
>>>>> equivalent it pure Python is works fine with either 2 or 3.
>>>>>
>>>>> So given the attached project:
>>>>>
>>>>> > python /home/users/russel/Repositories/Mercurial/Forks/SCons/src/sc
>>>>> ript/scons.py
>>>>> scons: Reading SConscript files ...
>>>>> __init__ loaded.
>>>>> generate called.
>>>>> scons: done reading SConscript files.
>>>>> scons: Building targets ...
>>>>> scons: `.' is up to date.
>>>>> scons: done building targets.
>>>>> 546 lavaine:~/Progs/OddsByLanguage/SCons/RelativeImports
>>>>> |> python3 /home/users/russel/Repositorie
>>>>> s/Mercurial/Forks/SCons/src/script/scons.py
>>>>> scons: Reading SConscript files ...
>>>>> SystemError: Parent module 'relative' not loaded, cannot perform
>>>>> relative import:
>>>>> File "/home/users/russel/Progs/OddsByLanguage/SCons/RelativeImports/SConstruct",
>>>>> line 1:
>>>>> env = Environment(tools=['relative'])
>>>>> File "/home/users/russel/Repositories/Mercurial/Forks/SCons/src/engine/SCons/Environment.py",
>>>>> line 982:
>>>>> apply_tools(self, tools, toolpath)
>>>>> File "/home/users/russel/Repositories/Mercurial/Forks/SCons/src/engine/SCons/Environment.py",
>>>>> line 107:
>>>>> env.Tool(tool)
>>>>> File "/home/users/russel/Repositories/Mercurial/Forks/SCons/src/engine/SCons/Environment.py",
>>>>> line 1788:
>>>>> tool = SCons.Tool.Tool(tool, toolpath, **kw)
>>>>> File "/home/users/russel/Repositories/Mercurial/Forks/SCons/src/engine/SCons/Tool/__init__.py",
>>>>> line 115:
>>>>> module = self._tool_module()
>>>>> File "/home/users/russel/Repositories/Mercurial/Forks/SCons/src/engine/SCons/Tool/__init__.py",
>>>>> line 228:
>>>>> spec.loader.exec_module(module)
>>>>> File "<frozen importlib._bootstrap_external>", line 697:
>>>>>
>>>>> File "<frozen importlib._bootstrap>", line 222:
>>>>>
>>>>> File "/home/users/russel/Progs/OddsByLanguage/SCons/RelativeImpor
>>>>> ts/site_scons/site_tools/relative/__init__.py", line 3:
>>>>> from .thingy import generate, exists
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Russel.
>>>>> ============================================================
>>>>> =================
>>>>> Dr Russel Winder t: +44 20 7585 2200 voip:
>>>>> sip:russel.winder at ekiga.net
>>>>> 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel at winder.org.uk
>>>>> London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
>>>>> _______________________________________________
>>>>> Scons-dev mailing list
>>>>> Scons-dev at scons.org
>>>>> https://pairlist2.pair.net/mailman/listinfo/scons-dev
>>>>>
>>>>>
>>>>
>>>
>>
>> _______________________________________________
>> Scons-dev mailing list
>> Scons-dev at scons.org
>> https://pairlist2.pair.net/mailman/listinfo/scons-dev
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://pairlist2.pair.net/pipermail/scons-dev/attachments/20170626/28e5242e/attachment-0001.html>
More information about the Scons-dev
mailing list