[Tutor] Import from project's lib directory?

leam hall leamhall at gmail.com
Thu Aug 28 21:13:42 CEST 2014


On Thu, Aug 28, 2014 at 2:19 PM, Danny Yoo <dyoo at hashcollision.org> wrote:
> On Thu, Aug 28, 2014 at 11:03 AM, leam hall <leamhall at gmail.com> wrote:
>> python 2.4.3 on Red Hat Linux.
>>
>> I'm starting a project and want to break the scripts into "input",
>> "output", and "ddl". I'd like to have a "lib" library directory for
>> local modules. The library directory would be at the same level as the
>> others.
>>
>> How do I get the library modules?
>>
>>   import '../lib/mymodule'
>
>
> You're looking for relative imports:
>
>     https://docs.python.org/2.5/whatsnew/pep-328.html
>
> So for your case, perhaps:
>
>     from ..lib import mymodule
>
> Alternatively, perhaps refer to the modules by toplevel package
> instead of relative path?


Fails on both Python 2.4 and 2.6 with either ".lib" or "..lib".

### code
#!/usr/bin/env python

from ..lib import mymodule
####

In python/ddl, referencing ../lib/mymodule.py

 ./import_test.py
Traceback (most recent call last):
  File "./import_test.py", line 3, in <module>
    from .lib import mymodule
ValueError: Attempted relative import in non-package


 ls ../lib
mymodule.py


-- 
Mind on a Mission


More information about the Tutor mailing list