[Tutor] Import from project's lib directory?

Danny Yoo dyoo at hashcollision.org
Thu Aug 28 21:27:05 CEST 2014


>
> 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


The word "package" has a technical meaning.  Here's a description:

    https://docs.python.org/2/tutorial/modules.html#packages

Can you use packages to structure your code directory?  You'll need
"__init__.py" files in the directories that are to be treated as
packages.


If you can structure your program into packages, then intra-package
imports should work:

    https://docs.python.org/2/tutorial/modules.html#intra-package-references


More information about the Tutor mailing list